/* The following function clears and closes an itemStatusDiv_ */

function closeItemStatusDiv(itemid){

	document.getElementById('itemStatusDiv_'+itemid).innerHTML = "";

	document.getElementById('itemStatusDiv_'+itemid).style.visibility = 'hidden';

	document.getElementById('itemStatusDiv_'+itemid).style.display = 'none';

}



/* The following function creates an XMLHttpRequest object... */

function createRequestObject(){

	var request_o; //declare the variable to hold the object.

	//var browser = navigator.appName; //find the browser name

	//if(browser == "Microsoft Internet Explorer"){

	//	/* Create the object using MSIE's method */

	//	request_o = new ActiveXObject("Microsoft.XMLHTTP");

	//}else{

	//	/* Create the object using other browser's method */

	//	request_o = new XMLHttpRequest();

	//}

	//return request_o; //return the object

	/* You can get more specific with version information by using 

	parseInt(navigator.appVersion)

	Which will extract an integer value containing the version 

	of the browser being used.

	*/

	

	if (window.XMLHttpRequest) { // Mozilla, Safari,...

		request_o = new XMLHttpRequest();

        	if (request_o.overrideMimeType) {

			request_o.overrideMimeType('text/xml');

		}

	} else if (window.ActiveXObject) { // IE

        	try {

                	request_o = new ActiveXObject("Msxml2.XMLHTTP");

            	} catch (e) {

	                try {

	                    request_o = new ActiveXObject("Microsoft.XMLHTTP");

	                } catch (e) {}

		}

    	}

    	

    	if (!request_o) {

		alert('Giving up :( Cannot create an XMLHTTP instance');

		return false;

	} else {

    		return request_o; //return the object

    	}

}

/* ------------------------------------------------------------------------------------------------

   Favourites functions
   
   --------------------------------------------------------------------------------------------- */

function addToFavourites(itemid){

	/* Create the request. The first argument to the open function is the method (POST/GET),

		and the second argument is the url... 

		document contains references to all items on the page

		We can reference document.form_category_select.select_category_select and we will

		be referencing the dropdown list. The selectedIndex property will give us the 

		index of the selected item. 

	*/

	

	//display the status div

	document.getElementById('itemStatusDiv_'+itemid).style.visibility = 'visible';

	document.getElementById('itemStatusDiv_'+itemid).style.display = 'block';

	

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	

	var url = "../../AJAXscripts/addtofavourites.php?itemid="+itemid;

	

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleAddToFavouritesStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	

	http.open('get', url, true);

	

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}



/* Function called to handle the list that was returned from the internal_request.php file.. */

function handleAddToFavouritesStateChange(http,itemid){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;

		

		/* And now we want to change the product_categories <div> content.

		we do this using an ability to get/change the content of a page element 

		that we can find: innerHTML. */

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = response;

		

	} else {

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = "Saving to your favourites";

	}

}

/* ------------------------------------------------------------------------------------------------

   Email to Friend functions
   
   --------------------------------------------------------------------------------------------- */

function emailToFriend(parameters, itemid, linkurl){

	/* Create the request. The first argument to the open function is the method (POST/GET),

		and the second argument is the url... 

		document contains references to all items on the page

		We can reference document.form_category_select.select_category_select and we will

		be referencing the dropdown list. The selectedIndex property will give us the 

		index of the selected item. 

	*/

	

	//display the status div

	document.getElementById('itemStatusDiv_'+itemid).style.visibility = 'visible';

	document.getElementById('itemStatusDiv_'+itemid).style.display = 'block';

	

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	

	var url = "../../AJAXscripts/emailtofriend.php";

	if(parameters+"" == ""){
		url += "?itemid="+itemid;
                if (linkurl+"" != "") url += "&linkurl="+linkurl
	} else {
		url += "?"+parameters
                if (linkurl+"" != "") url += "&linkurl="+linkurl
	}
	

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleEmailToFriendStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	

	http.open('get', url, true);

	

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}



function getEmailToFriend(obj) {

	/* assembles the querystring for submitting an item to be emailed to a friend via AJAX

	*/

	var poststr = "action=" + encodeURI( document.getElementById("action").value ) +

        	"&itemid=" + encodeURI( document.getElementById("itemid").value ) +

        	"&emailTo=" + encodeURI( document.getElementById("emailTo").value ) +

                "&UserEmail=" + encodeURI( document.getElementById("UserEmail").value ) +

                "&linkurl=" + encodeURI( document.getElementById("linkurl").value ) +

                "&message=" + encodeURI( document.getElementById("message").value );

      emailToFriend(poststr,document.getElementById("itemid").value,document.getElementById("linkurl").value);

}



/* Function called to handle the list that was returned from the internal_request.php file.. */

function handleEmailToFriendStateChange(http,itemid){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;

		

		/* And now we want to change the product_categories <div> content.

		we do this using an ability to get/change the content of a page element 

		that we can find: innerHTML. */

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = response;
		

	} else {

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = "Sending Email";

	}

}

/* ------------------------------------------------------------------------------------------------

   Item Clicked functions - Records the number of times a link is clicked
   
   --------------------------------------------------------------------------------------------- */

function itemClicked(itemid){

	/* Create the request. The first argument to the open function is the method (POST/GET),

		and the second argument is the url... 

		document contains references to all items on the page

		We can reference document.form_category_select.select_category_select and we will

		be referencing the dropdown list. The selectedIndex property will give us the 

		index of the selected item. 

	*/

	

	//display the status div

	//document.getElementById('itemStatusDiv_'+itemid).style.visibility = 'visible';

	//document.getElementById('itemStatusDiv_'+itemid).style.display = 'block';

	

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	

	var url = "../../AJAXscripts/itemclicked.php?itemid="+itemid;

	

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleItemClickedStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	

	http.open('get', url, true);

	

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}



/* Function called to handle the list that was returned from the internal_request.php file.. */

function handleItemClickedStateChange(http,itemid){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;

		

		/* And now we want to change the product_categories <div> content.

		we do this using an ability to get/change the content of a page element 

		that we can find: innerHTML. */

		//document.getElementById('itemStatusDiv_'+itemid).innerHTML = response;

		

	} else {

		//document.getElementById('itemStatusDiv_'+itemid).innerHTML = "Saving to your favourites";

	}

}

/* ------------------------------------------------------------------------------------------------

   Comments functions
   
   --------------------------------------------------------------------------------------------- */

function showComments(itemid){

	/* Create the request. The first argument to the open function is the method (POST/GET),

		and the second argument is the url... 

		document contains references to all items on the page

		We can reference document.form_category_select.select_category_select and we will

		be referencing the dropdown list. The selectedIndex property will give us the 

		index of the selected item. 

	*/

	

	//display the status div

	document.getElementById('itemStatusDiv_'+itemid).style.visibility = 'visible';

	document.getElementById('itemStatusDiv_'+itemid).style.display = 'block';

	

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	

	var url = "../../AJAXscripts/showcomments.php?itemid="+itemid;

	

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleShowCommentsStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	

	http.open('get', url, true);

	

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}



function handleShowCommentsStateChange(http,itemid){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;

		/* And now we want to change the product_categories <div> content.

		we do this using an ability to get/change the content of a page element 

		that we can find: innerHTML. */

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = response;

		

	} else {

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = "Fetching comments...";

	}

}



function getAddComment(obj) {

      var poststr = "action=" + encodeURI( document.getElementById("action").value ) +

                    "&itemid=" + encodeURI( document.getElementById("itemid").value ) +

                    "&title=" + encodeURI( document.getElementById("title").value ) +

                    "&comment=" + encodeURI( document.getElementById("comment").value );

      addToComments(poststr,document.getElementById("itemid").value);

}



function addToComments(parameters,itemid){

	/* Create the request. The first argument to the open function is the method (POST/GET),

		and the second argument is the url... 

		document contains references to all items on the page

		We can reference document.form_category_select.select_category_select and we will

		be referencing the dropdown list. The selectedIndex property will give us the 

		index of the selected item. 

	*/

	

	//display the status div

	document.getElementById('itemStatusDiv_'+itemid).style.visibility = 'visible';

	document.getElementById('itemStatusDiv_'+itemid).style.display = 'block';

	

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	

	var url = "../../AJAXscripts/addtocomments.php?"+parameters;

	

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleAddToCommentsStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	http.open('get', url, true);

	/* this posting seems to cause a crash of firefox

	http.open('POST', url, true);

	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

	http.setRequestHeader("Content-length", parameters.length);

	http.setRequestHeader("Connection", "close");

	http.send(parameters);

	*/

	http.send(null);

}



function handleAddToCommentsStateChange(http,itemid){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;
                    
                // 12/21/2008 Added the new comment count to the returned response and need to split it out
		var responsearr = response.split("|");

		/* And now we want to change the product_categories <div> content.

		we do this using an ability to get/change the content of a page element 

		that we can find: innerHTML. */

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = responsearr[0];
                
                // 12/21/2008 If there is a count returned, letup date it on the page
                if (responsearr[1]+"" != ""){
                    document.getElementById('commentscount_'+itemid).innerHTML = responsearr[1];   
                }

		

	} else {

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = "Adding comment...";

	}

}

/* ------------------------------------------------------------------------------------------------

   Login and Logout functions
   
   --------------------------------------------------------------------------------------------- */

function showLogin(){

	/* Create the request. The first argument to the open function is the method (POST/GET),

		and the second argument is the url... 

		document contains references to all items on the page

		We can reference document.form_category_select.select_category_select and we will

		be referencing the dropdown list. The selectedIndex property will give us the 

		index of the selected item. 

	*/

	

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	

	var url = "../../AJAXscripts/showlogin.php";

	

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleShowLoginStateChange(http); };

	//http.onreadystatechange = handleStateChange(); 

	

	http.open('get', url, true);

	

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}



function handleShowLoginStateChange(http){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;

		response = response + "<p><a href='#'>Tell A Friend</a><span>&nbsp;</span></p>";

		/* And now we want to change the product_categories <div> content.

		we do this using an ability to get/change the content of a page element 

		that we can find: innerHTML. */

		document.getElementById('loginStatusDiv').innerHTML = response;

		

	} else {

		document.getElementById('loginStatusDiv').innerHTML = "Getting login form...";
	}

}



//function tryLogin(obj){
//
//	/* Create the request. The first argument to the open function is the method (POST/GET),
//
//		and the second argument is the url... 
//
//		document contains references to all items on the page
//
//		We can reference document.form_category_select.select_category_select and we will
//
//		be referencing the dropdown list. The selectedIndex property will give us the 
//
//		index of the selected item. 
//
//	*/
//
//	
//
//	var poststr = "action=" + encodeURI( document.getElementById("action").value ) +
//
//                    "&loginusername=" + encodeURI( document.getElementById("loginusername").value ) +
//
//                    "&loginpassword=" + encodeURI( document.getElementById("loginpassword").value );
//
//	/* The variable http will hold our new XMLHttpRequest object. */
//
//	var http = createRequestObject();
//
//	
//
//	var url = "../../AJAXscripts/trylogin.php?" + poststr;
//
//	
//
//	/* Define a function to call once a response has been received. This will be our
//
//		handleProductCategories function that we define below. */
//
//	http.onreadystatechange = function() { handleShowLoginStateChange(http); };
//
//	//http.onreadystatechange = handleStateChange(); 
//
//	
//
//	http.open('get', url, true);
//
//	
//
//	/* Send the data. We use something other than null when we are sending using the POST
//
//		method. */
//
//	http.send(null);
//
//}
//
//
//
//function handleTryLoginStateChange(http){
//
//	/* Make sure that the transaction has finished. The XMLHttpRequest object 
//
//		has a property called readyState with several states:
//
//		0: Uninitialized
//
//		1: Loading
//
//		2: Loaded
//
//		3: Interactive
//
//		4: Finished */
//
//	if(http.readyState == 4){ //Finished loading the response
//
//		/* We have got the response from the server-side script,
//
//		let's see just what it was. using the responseText property of 
//
//		the XMLHttpRequest object. */
//
//		var response = http.responseText;
//
//		response = response + "<p><a href='#'>Tell A Friend</a><span>&nbsp;</span></p>";
//		
//
//		/* And now we want to change the product_categories <div> content.
//
//		we do this using an ability to get/change the content of a page element 
//
//		that we can find: innerHTML. */
//
//		document.getElementById('loginStatusDiv').innerHTML = response;
//
//	} else {
//
//		document.getElementById('loginStatusDiv').innerHTML = "Logging in...";
//	}
//
//}



function logout(){

	/* Create the request. The first argument to the open function is the method (POST/GET),

		and the second argument is the url... 

		document contains references to all items on the page

		We can reference document.form_category_select.select_category_select and we will

		be referencing the dropdown list. The selectedIndex property will give us the 

		index of the selected item. 

	*/

	

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	

	var url = "../../AJAXscripts/logout.php";

	

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleLogoutStateChange(http); };

	//http.onreadystatechange = handleStateChange(); 

	

	http.open('get', url, true);

	

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}



function handleLogoutStateChange(http){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;

		

		/* And now we want to change the product_categories <div> content.

		we do this using an ability to get/change the content of a page element 

		that we can find: innerHTML. */

		document.getElementById('loginStatusDiv').innerHTML = response;
		window.location="/index.php";

		

	} else {

		document.getElementById('loginStatusDiv').innerHTML = "Logging out...";

	}

}

/* ------------------------------------------------------------------------------------------------

   Recommendation functions
   
   --------------------------------------------------------------------------------------------- */

function showRecommendations(itemid){

	/* Create the request. The first argument to the open function is the method (POST/GET),

		and the second argument is the url... 

		document contains references to all items on the page

		We can reference document.form_category_select.select_category_select and we will

		be referencing the dropdown list. The selectedIndex property will give us the 

		index of the selected item. 

	*/

	

	//display the status div

	document.getElementById('itemStatusDiv_'+itemid).style.visibility = 'visible';

	document.getElementById('itemStatusDiv_'+itemid).style.display = 'block';

	

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	

	var url = "../../AJAXscripts/showrecommendations.php?itemid="+itemid;

	

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleShowRecommendationStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	

	http.open('get', url, true);

	

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}



function handleShowRecommendationStateChange(http,itemid){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;

		response = response.split('|');

		/* And now we want to change the product_categories <div> content.

		we do this using an ability to get/change the content of a page element 

		that we can find: innerHTML. */

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = response[1];
                //document.getElementById('recommend_'+itemid).innerHTML = "<img src='/images/recommendIconSmallFull.gif' style='cursor:pointer;cursor:hand;' onclick='showRecommendations($itemid);' alt='Recommended'/>("+response[0]+")";
                document.getElementById('recommend_'+itemid).innerHTML = "Recommend (<strong>"+response[0]+"</strong>)";
		

	} else {

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = "Fetching Recommendations...";

	}

}

function UpdateRecommendCount(itemid){
        
	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	

	var url = "../../AJAXscripts/addrecommendation.php?itemid="+itemid;

	

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleShowRecommendationStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	

	http.open('get', url, true);

	

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}

/* ------------------------------------------------------------------------------------------------

   Sidebar functions
   
   --------------------------------------------------------------------------------------------- */

function sidebarClicked(itemid){

	/* Create the request. The first argument to the open function is the method (POST/GET),

		and the second argument is the url... 

		document contains references to all items on the page

		We can reference document.form_category_select.select_category_select and we will

		be referencing the dropdown list. The selectedIndex property will give us the 

		index of the selected item. 

	*/

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	

	var url = "../../AJAXscripts/sidebarclicked.php?itemid="+itemid;

	

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleSidebarClickedStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	

	http.open('get', url, true);

	

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}



/* Function called to handle the list that was returned from the internal_request.php file.. */

function handleSidebarClickedStateChange(http,itemid){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;

		

		/* And now we want to change the product_categories <div> content.

		we do this using an ability to get/change the content of a page element 

		that we can find: innerHTML. */

		//document.getElementById('itemStatusDiv_'+itemid).innerHTML = response;

		

	} else {

		//document.getElementById('itemStatusDiv_'+itemid).innerHTML = "Saving to your favourites";

	}

}

/* ------------------------------------------------------------------------------------------------

   Ratings functions
   
   --------------------------------------------------------------------------------------------- */

function showRating(itemid){
    
	/* show the ratings form

	*/

	//display the status div

	document.getElementById('itemStatusDiv_'+itemid).style.visibility = 'visible';

	document.getElementById('itemStatusDiv_'+itemid).style.display = 'block';

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	var url = "../../AJAXscripts/showratingform.php?itemid="+itemid;

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleShowRatingStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	http.open('get', url, true);

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}



function handleShowRatingStateChange(http,itemid){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = response;
		

	} else {

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = "Fetching Ratings Form...";

	}

}

function UpdateRatingCount(itemid){
        
	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	var url = "../../AJAXscripts/addtorating.php?itemid="+itemid;

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleShowRatingStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	http.open('get', url, true);

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}

function getAddRating(obj) {

      var poststr = "action=" + encodeURI( document.getElementById("action").value ) +
                    "&itemid=" + encodeURI( document.getElementById("itemid").value ) +
                    "&rating=" + encodeURI( document.getElementById("rating").value );

      addToRatings(poststr,document.getElementById("itemid").value);

}



function addToRatings(parameters,itemid){

	//display the status div

	document.getElementById('itemStatusDiv_'+itemid).style.visibility = 'visible';

	document.getElementById('itemStatusDiv_'+itemid).style.display = 'block';

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	var url = "../../AJAXscripts/addtorating.php?"+parameters;
        

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleAddToRatingsStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	http.open('get', url, true);

	http.send(null);

}



function handleAddToRatingsStateChange(http,itemid){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;
                
                // 12/21/2008 Added the new rating count to the returned response and need to split it out
		var responsearr = response.split("|");

		/* And now we want to change the product_categories <div> content.

		we do this using an ability to get/change the content of a page element 

		that we can find: innerHTML. */

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = responsearr[0];
                
                // 12/21/2008 If there is a count returned, letup date it on the page
                if (responsearr[1]+"" != ""){
                    document.getElementById('rating_'+itemid).innerHTML = responsearr[1];
                    document.getElementById('rated_'+itemid).className = "rates1";
                }

	} else {

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = "Adding rating...";

	}

}

/* ------------------------------------------------------------------------------------------------

   Social Links functions
   
   --------------------------------------------------------------------------------------------- */

function showSocialLinks(itemid){
    
	/* show the Social network links form

	*/

	//display the status div

	document.getElementById('itemStatusDiv_'+itemid).style.visibility = 'visible';

	document.getElementById('itemStatusDiv_'+itemid).style.display = 'block';

	/* The variable http will hold our new XMLHttpRequest object. */

	var http = createRequestObject();

	var url = "../../AJAXscripts/showsociallinks.php?itemid="+itemid;

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleShowSocialLinksStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	http.open('get', url, true);

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}

function handleShowSocialLinksStateChange(http,itemid){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = response;
		

	} else {

		document.getElementById('itemStatusDiv_'+itemid).innerHTML = "Fetching Social Links...";

	}

}

/* ------------------------------------------------------------------------------------------------

   Sponsor Clicked functions - Records the number of times a sponsor link is clicked
   
   --------------------------------------------------------------------------------------------- */

function sponsorClicked(itemid){

	var http = createRequestObject();

	var url = "../../AJAXscripts/sponsorclicked.php?sponsorid="+itemid;

	/* Define a function to call once a response has been received. This will be our

		handleProductCategories function that we define below. */

	http.onreadystatechange = function() { handleSponsorClickedStateChange(http,itemid); };

	//http.onreadystatechange = handleStateChange(); 

	http.open('get', url, true);

	/* Send the data. We use something other than null when we are sending using the POST

		method. */

	http.send(null);

}



/* Function called to handle the list that was returned from the internal_request.php file.. */

function handleSponsorClickedStateChange(http,itemid){

	/* Make sure that the transaction has finished. The XMLHttpRequest object 

		has a property called readyState with several states:

		0: Uninitialized

		1: Loading

		2: Loaded

		3: Interactive

		4: Finished */

	if(http.readyState == 4){ //Finished loading the response

		/* We have got the response from the server-side script,

		let's see just what it was. using the responseText property of 

		the XMLHttpRequest object. */

		var response = http.responseText;
		

	} else {

		//document.getElementById('itemStatusDiv_'+itemid).innerHTML = "Saving to your favourites";

	}

}