var xml = "";
var message = "";
function CRgetDataFromServer(id, url) {
	// Fetch the element pointed to by the id. If it exists, we destroy it so we can create a new one.
	oScript = document.getElementById(id);
	
	// Point at the script tag, if it exists
	var head = document.getElementsByTagName("head").item(0);
	 // Destroy the tag, if it exists
	if (oScript) {
	// Destory object
	head.removeChild(oScript);
	}   // Create the new script tag
	oScript = document.createElement("script");
	
	// Setup the src attribute of the script tag
	oScript.setAttribute("src", url);
	
	// Set the id attribute of the script tag
	oScript.setAttribute("id",id);
	
	// Create the new script tag which causes the proxy to be called
	head.appendChild(oScript);
}
function CRparseComments()	{
	ohandle = document.getElementById("cr_comments_container");
	if (window.ActiveXObject){
		var doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
		doc.loadXML(xml);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else	{
		var parser=new DOMParser();
		var doc=parser.parseFromString(xml,"text/xml");
	}
	var x=doc.documentElement;
	
	//comment
	ohandle.innerHTML = "";
	for(var i = 0; i < x.childNodes.length; i++) {
		ratingHTML = "User rating: ";
        ratingHTML += "<span>";
        var rating = x.childNodes[i].childNodes[3].childNodes[0].nodeValue;
        if(rating == 0)	{
        	ratingHTML += "<span class='cr_user_rateinfo'>(user did not rate)</span>";
        }
        else	{
        	for(j=1;j<=5;j++)	{
                if(j <= rating)	{
                    ratingHTML += '<img src="http://www.citricle.com/rateback/images/star_full.jpg" align="absmiddle" />&nbsp;';
                }
                else	{
                    if((j-rating) <= 0.5)	{
                        ratingHTML += '<img src="http://www.citricle.com/rateback/images/star_half.jpg" align="absmiddle" />&nbsp;';
                    }
                    else	{
                        ratingHTML += '<img src="http://www.citricle.com/rateback/images/star_empty.jpg" align="absmiddle" />&nbsp;';
                    }
                }
            }
        }
        ratingHTML += "</span>";
        ohandle.innerHTML += "<p><span class='cr_user'>Comment by <b>" + x.childNodes[i].childNodes[0].childNodes[0].nodeValue + "</b> - " + x.childNodes[i].childNodes[1].childNodes[0].nodeValue +" </span><br /><br />"+ decodeURIComponent(x.childNodes[i].childNodes[2].childNodes[0].nodeValue) + "<br /><br />" + ratingHTML + "</p>";
    }
}
function CRnoComments()	{
	ohandle = document.getElementById("cr_comments_container");
    ohandle.innerHTML = "<p>There are no comments yet</p>";
}
function CRgetComments() {
	setEvents();
	var CRcurrentDate = new Date();
	CRcurrentTimestamp = CRcurrentDate.getTime();
	CRgetDataFromServer("script_get_comments","http://www.citricle.com/rateback/get_server.php?company_id=197&company_code=9ca983d6ea2b9160f22eb4bded35b7f2&get=comments&time=" + CRcurrentTimestamp) ;
}
function CRputComments(name, email, message, rating) {
	// Fetch the element pointed to by the id. If it exists, we destroy it so we can create a new one.
	oScript = document.getElementById("script2");
	var CRcurrentDate = new Date();
	CRcurrentTimestamp = CRcurrentDate.getTime();
	url = "http://www.citricle.com/rateback/put_server.php?company_id=197&company_code=9ca983d6ea2b9160f22eb4bded35b7f2&time="+ CRcurrentTimestamp;	
	
	// Point at the script tag, if it exists
	var head = document.getElementsByTagName("head").item(0);
	 // Destroy the tag, if it exists
	if (oScript) {
	// Destory object
	head.removeChild(oScript);
	}   // Create the new script tag
	oScript = document.createElement("script");
	
	// Setup the src attribute of the script tag
	name = encodeURIComponent(name);
	email = encodeURIComponent(email);
	message = encodeURIComponent(message);
	completeUrl = url + "&name=" + name + "&email=" + email + "&rating=" + rating + "&message=" + message;


	oScript.setAttribute("src", completeUrl);
	
	// Set the id attribute of the script tag
	oScript.setAttribute("id","script2");
	
	// Create the new script tag which causes the proxy to be called
	head.appendChild(oScript);	
}
function CRcheckEmail(emailString) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailString)){
		return true;
	}
}
function CRdoPost()	{
	var error = false;
	if(document.getElementById('cr_name').value == "" || document.getElementById('cr_name').value == "Name *" )	{
		error = true;
		document.getElementById('cr_name').className='cr_input_comment_error';
	}
	if(!CRcheckEmail(document.getElementById('cr_email').value))	{
		error = true;
		document.getElementById('cr_email').className='cr_input_comment_error';
	}
	if(document.getElementById('cr_message').value == "" || document.getElementById('cr_message').value == "Message *" )	{
		error = true;
		document.getElementById('cr_message').className='cr_textarea_comment_error';
	}
	
	if(error == false)	{
		//document.getElementById('cr_comments_container').innerHTML='';
		xml='';
		CRputComments(document.getElementById('cr_name').value, document.getElementById('cr_email').value, document.getElementById('cr_message').value, document.getElementById("cr_rating").value);
		
		//clear the fields
		document.getElementById('cr_name').value = "Name *";
		document.getElementById('cr_email').value = "Email *";
		document.getElementById('cr_message').value = "Message *";
        document.getElementById('cr_rating').value = 0;
        document.getElementById('cr_counter').value = 1000;
	}
	else	{
		document.getElementById('cr_message_box').innerHTML = "Please fill in all the fields !";
	}
}

function CRrefreshComments()	{
	document.getElementById('cr_comments_container').innerHTML='';
	xml='';
	CRgetComments();
}

function CRdoFormFocus(obj) {
	if(obj.value==obj.defaultValue) obj.value='';
}

function CRdoFormBlur(obj) {
	if(obj.value=='') obj.value=obj.defaultValue;
}
function CRtextCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) 
		field.value = field.value.substring(0, maxlimit);
	else 
		countfield.value = maxlimit - field.value.length;
}
function	CRanimateRating(cursorLocation)	{
	document.getElementById('cr_stars').innerHTML = "";
	for(i=1;i<=5;i++)	{
		if(i < cursorLocation)	{
			document.getElementById('cr_stars').innerHTML += '<img src="http://www.citricle.com/rateback/images/star_full.jpg" onmouseover="CRanimateRating(' + i + ');" class="with_cursor" />&nbsp;';
		}
		else if(i == cursorLocation)	{
			document.getElementById('cr_stars').innerHTML += '<img src="http://www.citricle.com/rateback/images/star_full.jpg" class="with_cursor" onclick="CRdoRate(' + i + ');" />&nbsp;';
		}
		else	{
			document.getElementById('cr_stars').innerHTML += '<img src="http://www.citricle.com/rateback/images/star_empty.jpg" onmouseover="CRanimateRating(' + i + ');" class="with_cursor" />&nbsp;';
		}

	}
}
function	CRanimateCommentRating(cursorLocation)	{
	//document.getElementById('cr_comment_stars').innerHTML = '<span style="float: left;">Your rating:&nbsp;</span>';
	for(i=1;i<=5;i++)	{
    	var starId = 'cr_comment_star_' + i;
    	var el = document.getElementById(starId)
		if(i <= cursorLocation)	{
        	el.className = "cr_star_full";
		}
		else	{
			el.className = "cr_star_empty";
		}

	}
}
function CRsetCommentVote(cursorLocation)	{
	document.getElementById("cr_rating").value = cursorLocation;
}
function CRgetCommentVote()	{
	CRanimateCommentRating(commentCurrentVote)
}
function CRgetRating()	{
	var CRcurrentDate = new Date();
	CRcurrentTimestamp = CRcurrentDate.getTime();
	
	// Fetch the element pointed to by the id. If it exists, we destroy it so we can create a new one.
	oScript = document.getElementById("script_get_rating");
	
	// Point at the script tag, if it exists
	var head = document.getElementsByTagName("head").item(0);
	 // Destroy the tag, if it exists
	if (oScript) {
		// Destory object
		head.removeChild(oScript);
	}   
	// Create the new script tag
	oScript = document.createElement("script");
	
	// Setup the src attribute of the script tag
	oScript.setAttribute("src", "http://www.citricle.com/rateback/get_server.php?company_id=197&company_code=9ca983d6ea2b9160f22eb4bded35b7f2&get=rating&time=" + CRcurrentTimestamp);
	
	// Set the id attribute of the script tag
	oScript.setAttribute("id","script1");
	
	// Create the new script tag which causes the proxy to be called
	head.appendChild(oScript);
}

function CRloadRating(rating, count)	{
	document.getElementById('cr_stars').innerHTML = "";
	document.getElementById('cr_product_rating').innerHTML = rating.toFixed(2)  ;
	
	
    document.getElementById('cr_rating_count').innerHTML = count + " ratings";
            if(document.getElementById('cr_product_rating_message').innerHTML == "&nbsp;")	{
        	CRratingMessage("scroll below to rate");
        }
        for(i=1;i<=5;i++)	{
            if(i <= rating)	{
                document.getElementById('cr_stars').innerHTML += '<img src="http://www.citricle.com/rateback/images/star_full.jpg" class="with_cursor" />&nbsp;';
            }
            else	{
                if((i-rating) <= 0.5)	{
                    document.getElementById('cr_stars').innerHTML += '<img src="http://www.citricle.com/rateback/images/star_half.jpg" class="with_cursor" />&nbsp;';
                }
                else	{
                    document.getElementById('cr_stars').innerHTML += '<img src="http://www.citricle.com/rateback/images/star_empty.jpg" class="with_cursor" />&nbsp;';
                }
            }
        }
		}

function CRdoRate(rating)	{
	// Fetch the element pointed to by the id. If it exists, we destroy it so we can create a new one.
	oScript = document.getElementById("script2");
	var CRcurrentDate = new Date();
	CRcurrentTimestamp = CRcurrentDate.getTime();
	url = "http://www.citricle.com/rateback/put_server.php?company_id=197&company_code=9ca983d6ea2b9160f22eb4bded35b7f2&time="+ CRcurrentTimestamp ;
	
	// Point at the script tag, if it exists
	var head = document.getElementsByTagName("head").item(0);
	 // Destroy the tag, if it exists
	if (oScript) {
	// Destory object
	head.removeChild(oScript);
	}   // Create the new script tag
	oScript = document.createElement("script");
	
	// Setup the src attribute of the script tag
	completeUrl = url + "&rating=" + rating;

	//document.write(completeUrl);

	oScript.setAttribute("src", completeUrl);
	
	// Set the id attribute of the script tag
	oScript.setAttribute("id","script2");
	
	// Create the new script tag which causes the proxy to be called
	head.appendChild(oScript);	
}

function CRmessagePosted(approval_needed)	{
	var el = document.getElementById("cr_message_box");
    if(approval_needed)	{
    	el.innerHTML = "Comment posted! Awaiting moderation!";
    }
    else	{
    	el.innerHTML = "Comment posted!";
    }
}

function CRratingMessage(message)	{
	var el = document.getElementById("cr_product_rating_message");
    el.innerHTML = message;
}
function setEvents()	{
	document.getElementById("cr_comment_stars").onmouseout = function()	{ 
        CRanimateCommentRating(document.getElementById("cr_rating").value); 
    }
}
function CRgetAll()	{
    CRgetRating();
    CRgetComments();
}

document.write('<link rel="stylesheet" href="http://www.citricle.com/rateback/get_css.php?company_id=197&amp;company_code=9ca983d6ea2b9160f22eb4bded35b7f2" type="text/css" />');
//CRgetAll();
