var sMax;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;
var currentRating = 0; // Is the initial Rating that is set for the contentItem

// Rollover for image Stars //
function rating(num){

	sMax = 0;	// Isthe maximum number of stars
	for(n=0; n<num.parentNode.childNodes.length; n++){
		if(num.parentNode.childNodes[n].nodeName == "A"){
			sMax++;	
		}
	}
	
	if(!rated){
		s = num.id.replace("_", ''); // Get the selected star
		a = 0;
		for(i=1; i<=sMax; i++){		
			if(i<=s){
				document.getElementById("_"+i).className = "on";
				document.getElementById("rateStatus").innerHTML = num.title;	
				holder = a+1;
				a++;
			}else{
				document.getElementById("_"+i).className = "";
			}
		}
	}
}



// For when you roll out of the the whole thing //
function off(me){
	if(!rated){
		if(!preSet){	
			for(i=1; i<=sMax; i++){		
				document.getElementById("_"+i).className = "";
				document.getElementById("rateStatus").innerHTML = me.parentNode.title;
			}
		}else{
			rating(preSet);
			document.getElementById("rateStatus").innerHTML = document.getElementById("ratingState").innerHTML;
		}
		setStarRatingForContentItem(currentRating);
	}
	
}

// When you actually rate something //
function rateIt(me){
	if(!rated){
		document.getElementById("ratingState").innerHTML = "Your Rating";
		document.getElementById("rateStatus").style.display="none";
		preSet = me;
		rated=1;
		sendRate(me);
		rating(me);
	}
}

// Send the rating information somewhere using Ajax.
function sendRate(selId){

	document.forms['voteFormprdt'].contentRating.value = selId.id.substring(1);
	var formData = getFormData('voteFormprdt');
	// encode for Spry
	formData = encodeURI(formData);
 	Spry.Utils.loadURL('POST','/en_GB/insert-product-rating', true,resFunc,{postData: formData,headers:{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}});
   
	}

function resFunc(request) {

   var result = request.xhRequest.responseText; 
   document.getElementById("totalVotes").innerHTML ="&nbsp;"+result+ "";
}

function setStarRatingForContentItem(contentRating) {
	var ratingNode = document.getElementById('contentRating');
	var currentCount = 0;
	for(n=0; n<ratingNode.childNodes.length; n++){
		if(ratingNode.childNodes[n].nodeName == "A"){
			if (currentCount < contentRating) {
				ratingNode.childNodes[n].className = "on";
				currentCount++;	
			}
		}
	currentRating = contentRating; // Keeping the Rating of the contentItem in a global variable
	}
}	

function getFormData(formId) {
	var formData = "";
	//Get the form values
 	formElements=document.forms[formId].elements;
 	//loop through the array , building up the formData
 	for ( var i=formElements.length-1; i>=0; --i ){
 		//we escape each value
 		formData= formData + escape(formElements[i].name)+"="+escape(formElements[i].value)+"&";
 	}
 	formData = formData.substring(0,formData.length-1);
 	return formData;
} 

	function ratingNotLogin()
	{
		document.getElementById("loginRatingState").style.display = "block";
		document.getElementById("loginRatingState").innerHTML = "You must first login to rate this product.";
	}
	
	function offLogin()
	{
		document.getElementById("loginRatingState").innerHTML = "";
		document.getElementById("loginRatingState").style.display = "none";
	}

	function ratingLoginNotAllow()
	{
		document.getElementById("loginRatingState").innerHTML = "You have already rated this product.";
		document.getElementById("loginRatingState").style.display = "block";		
	}
// Product rating start here 

/* Script for click on tab for product detail tab have been added here from jsp page*/
/* product_detail_tabs.jsp */
function showDiv(myDiv, myTab) {
	
	document.getElementById("prdtTab1").style.display = "none";
	document.getElementById("prdtTab2").style.display = "none";
	document.getElementById("prdtTab3").style.display = "none";
	document.getElementById("prdtTab4").style.display = "none";
	document.getElementById("prdtTab5").style.display = "none";
	document.getElementById(myDiv).style.display = "block";
	if(myTab == "prdtTab_01_id") {
		if(document.getElementById("prdtTab_01_id")!=null)
			document.getElementById("prdtTab_01_id").className = "prdtTab_01_mo";
		if(document.getElementById("prdtTab_02_id")!=null)
			document.getElementById("prdtTab_02_id").className = "prdtTab_02";
		if(document.getElementById("prdtTab_03_id")!=null)
			document.getElementById("prdtTab_03_id").className = "prdtTab_03";
		if(document.getElementById("prdtTab_04_id")!=null)
			document.getElementById("prdtTab_04_id").className = "prdtTab_04";		
		if(document.getElementById("prdtTab_05_id")!=null)
        	document.getElementById("prdtTab_05_id").className = "prdtTab_05";				
	}
	if(myTab == "prdtTab_02_id") {
		if(document.getElementById("prdtTab_01_id")!=null)
			document.getElementById("prdtTab_01_id").className = "prdtTab_01";
		if(document.getElementById("prdtTab_02_id")!=null)
			document.getElementById("prdtTab_02_id").className = "prdtTab_02_mo";
		if(document.getElementById("prdtTab_03_id")!=null)
			document.getElementById("prdtTab_03_id").className = "prdtTab_03";
		if(document.getElementById("prdtTab_04_id")!=null)
			document.getElementById("prdtTab_04_id").className = "prdtTab_04";		
		if(document.getElementById("prdtTab_05_id")!=null)
        	document.getElementById("prdtTab_05_id").className = "prdtTab_05";				
	}
	if(myTab == "prdtTab_03_id") {
		if(document.getElementById("prdtTab_01_id")!=null)
        	document.getElementById("prdtTab_01_id").className = "prdtTab_01";
		if(document.getElementById("prdtTab_02_id")!=null)
        	document.getElementById("prdtTab_02_id").className = "prdtTab_02";
		if(document.getElementById("prdtTab_03_id")!=null)
	        document.getElementById("prdtTab_03_id").className = "prdtTab_03_mo";
		if(document.getElementById("prdtTab_04_id")!=null)
			document.getElementById("prdtTab_04_id").className = "prdtTab_04";	  
		if(document.getElementById("prdtTab_05_id")!=null)
        	document.getElementById("prdtTab_05_id").className = "prdtTab_05";			      
    }
    if(myTab == "prdtTab_04_id") {
		if(document.getElementById("prdtTab_01_id")!=null)
        	document.getElementById("prdtTab_01_id").className = "prdtTab_01";
		if(document.getElementById("prdtTab_02_id")!=null)
        	document.getElementById("prdtTab_02_id").className = "prdtTab_02";
		if(document.getElementById("prdtTab_03_id")!=null)
        	document.getElementById("prdtTab_03_id").className = "prdtTab_03";
		if(document.getElementById("prdtTab_04_id")!=null)
        	document.getElementById("prdtTab_04_id").className = "prdtTab_04_mo";
		if(document.getElementById("prdtTab_05_id")!=null)
        	document.getElementById("prdtTab_05_id").className = "prdtTab_05";        	
    }
	if(myTab == "prdtTab_05_id") 
	{
		if(document.getElementById("prdtTab_01_id")!=null)
       		document.getElementById("prdtTab_01_id").className = "prdtTab_01";
		if(document.getElementById("prdtTab_02_id")!=null)
        	document.getElementById("prdtTab_02_id").className = "prdtTab_02";       		
		if(document.getElementById("prdtTab_04_id")!=null)
        	document.getElementById("prdtTab_04_id").className = "prdtTab_04";        	
		if(document.getElementById("prdtTab_03_id")!=null)
			document.getElementById("prdtTab_03_id").className = "prdtTab_03";
	    if(document.getElementById("prdtTab_05_id")!=null)
        	document.getElementById("prdtTab_05_id").className = "prdtTab_05_mo";        
	}       
    
}
/* Script for click on tab for product detail tab have been added here from jsp page*/
