/*
Author: Addam M. Driver
Date: 10/31/2006
*/

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 starratingmade=false;

// 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("ratingSaved").innerHTML;
		}
	}
}

function getimage()
{
//alert('hi');
var x= window.location.href;
  var i = 0+x.indexOf('imagename_');
  x=x.substr(i+10,100);
//  alert(x);
  i = 0+x.indexOf('_');
  x=x.substr(0,i);
 // alert(x);
  return x;
}

// When you actually rate something //
function rateIt(me,rndid,itemid){
	if(!rated){
		document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML + " :: "+me.title;
		preSet = me;
		rated=1;
		var imagename = getimage(); 
	//	alert(imagename);
		sendRate(me,imagename);
		rating(me);
	}
}

function handleStarResult(param){

   var res = param.replace('<data>','');
   res = res.replace('</data>','');
   res = res.replace('<?xml version="1.0" encoding="UTF-8"?>','');
	
   if (res=="1") {
	   s=" star.";
   }
   else
   {
	   s=" stars.";
   }
  // alert("Thank you for rating this photograph. Value selected: " + res + s + 
//		 "");
   document.getElementById("ratetext").innerHTML = "Thank you";
   starratingmade=true;
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel,imagename){
	//alert("Thankyou for rating this item. You selected: "+sel.title);
		strURL = '/portfolio/saverate.aspx?imagename=' + imagename + '&rating=' + sel.title ;
		strSubmit = ''; // not passing params
		strResultFunc = 'handleStarResult';
	//	alert(strURL);
		s = xmlhttpPost(strURL, strSubmit, strResultFunc) ;
}

function xmlhttpPost(strURL, strSubmit, strResultFunc) {

        // the URL for the processing page on the server, 
		// the query-string formatted data to submit, and 
		// name of the JavaScript function that will process the response from the server (to invoke later through eval).

        var xmlHttpReq = false;
        
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
                xmlHttpReq = new XMLHttpRequest();
				try {
                   xmlHttpReq.overrideMimeType('text/xml');
				   }
				catch (err)
		           {
					   a=1;
				   }
				}
        // IE
        else if (window.ActiveXObject) {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
		
	    xmlHttpReq.open('POST', strURL, true);
        xmlHttpReq.setRequestHeader('Content-Type', 
		     'application/x-www-form-urlencoded');
        xmlHttpReq.onreadystatechange = function() {
                if (xmlHttpReq.readyState == 4) {
                        eval(strResultFunc + '(xmlHttpReq.responseText);');
						// status = xmlHttpReq.status; // get return status 404 500 etc
                }
        }
        xmlHttpReq.send(strSubmit);
}
