﻿function compareLink()	{
	var x = document.getElementById("compareDiv").style.display;
	
	if (x == "none")	{
		document.getElementById("compareDiv").style.display ="";
		document.getElementById("compLink").innerHTML = "Single product ReviewRave...";
	}	else	{
		document.getElementById("compareDiv").style.display = "none";
		document.getElementById("compLink").innerHTML = "Compare vs. another product ReviewRave...";

	}
}

function hideAllDivs()	{
	document.getElementById("searchProductsDiv1").style.display ="none";
		//document.getElementById("searchProductsDiv2").style.display ="none";
		document.getElementById("thechart").style.display = "none";
		document.getElementById("legend").style.display = "none";		
		document.getElementById("fetchingReviews").style.display = "none";		
		
		document.getElementById("fetchingActualReviews").style.display = "none";
		document.getElementById("reviewsDiv").style.display = "none";	


}

function review()	{
		if (newProductSearchText == false)	return;
		newProductSearchText = false;
  		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)		 {
			 alert ("Browser does not support HTTP Request");
			 return;
		} 		
   	    var objectItemold = document.getElementById("loader1");
	    objectItemold.style.display = "";
		
		hideAllDivs();
			

		var product1 = (document.getElementById("productName").value);
		var url="searchProducts.php?id=1&p="+encodeURIComponent(product1);
		xmlHttp.onreadystatechange=reviewCallback;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);	   	    

		document.getElementById("ASIN1").value = -1;
		document.getElementById("ASIN2").value = 0;
		
		var x = document.getElementById("compareDiv").style.display;
	
		if (false && x != "none")	{
				var product2 = (document.getElementById("productName2").value);
				document.getElementById("ASIN2").value = -1;
				xmlHttp=GetXmlHttpObject();
				var url="searchProducts.php?id=2&p="+encodeURIComponent(product2);
				xmlHttp.onreadystatechange=reviewCallback2;
				xmlHttp.open("GET",url,true);
				xmlHttp.send(null);	   	   
		   	    var objectItemold = document.getElementById("loader2");
			    objectItemold.style.display = "";
		}
}

function reviewCallback() 	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")	 {
			if (xmlHttp.responseText!=null ) {
				var objectItemold = document.getElementById("loader1");
			    objectItemold.style.display = "none";

				var id = xmlHttp.responseText;
				if (id.indexOf('<li>')==-1 && id.indexOf('no product reviews found matching')==-1)	{
					// one and only product was found!
					chooseASIN(1, document.getElementById("productName").value, id)
				}	else	{
					document.getElementById("searchProductsDiv1").innerHTML = id;
					document.getElementById("searchProductsDiv1").style.display ="";
				}
			}						
	} 
}

function reviewCallback2() 	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")	 {
			if (xmlHttp.responseText!=null ) {
				var objectItemold = document.getElementById("loader2");
			    objectItemold.style.display = "none";

				var id = xmlHttp.responseText;
				document.getElementById("searchProductsDiv2").innerHTML = id;
				document.getElementById("searchProductsDiv2").style.display ="";
			}						
	} 
}


function GetXmlHttpObject()		{
	var xmlHttp=null;
	try		 {
		 // Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
	 }	catch (e)			 {
		 //Internet Explorer
		 try	 {	
			  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		 }	 catch (e)	 {
			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		 }
	 }
	return xmlHttp;
}
function chooseASIN(id, title, asin)	{
	hideAllDivs();
	if (id == 1)	{
		document.getElementById("ASIN1").value = asin;
		document.getElementById("productName").value = title.replace(/\+/g, " ");
		var asin2 = document.getElementById("ASIN2").value;	
		
		document.getElementById("searchProductsDiv1").style.display = "none";
		document.getElementById("productnamespan").innerHTML = title.replace(/\+/g, " ");
	
		showAMZandContinue(asin, title);
	
	}	else	{
		document.getElementById("ASIN2").value = asin;	
		document.getElementById("productName2").value = title;	
		var asin1 = document.getElementById("ASIN1");				
		if (asin1 != -1)	drawChart(asin1,asin);			
	}
}

function 	showAMZandContinue(asin, title)	{
	
	xmlHttp=GetXmlHttpObject();

	var url="amz.php?asin="+encodeURIComponent(asin)+"&title="+encodeURIComponent(title);
	xmlHttp.onreadystatechange=showAMZandContinueCallback;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function showAMZandContinueCallback() 	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")	 {
				document.getElementById("amz").innerHTML = xmlHttp.responseText;
				
				var ASIN1 = document.getElementById("ASIN1").value;
				var ASIN2 = document.getElementById("ASIN2").value;				
				
				drawChart(ASIN1, ASIN2)		
		}
}


function drawChart(ASIN1, ASIN2)	{
	
	var objectItemold = document.getElementById("fetchingReviews");
    objectItemold.style.display = "";
	
	xmlHttp=GetXmlHttpObject();
	var product1 = (document.getElementById("productName").value);

	var url="fetchReviews.php?asin1="+encodeURIComponent(ASIN1)+"&asin2="+encodeURIComponent(ASIN2)+"&p="+encodeURIComponent(product1);
	xmlHttp.onreadystatechange=analyzeData;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function analyzeData() 	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")	 {
				var objectItemold = document.getElementById("fetchingReviews");
			    objectItemold.style.display = "none";
			    
			    var objectItemold = document.getElementById("analizingReviews");
			    objectItemold.style.display = "";
				document.getElementById("productreviewnum").innerHTML = xmlHttp.responseText;
				
				var ASIN1 = document.getElementById("ASIN1").value;
				var ASIN2 = document.getElementById("ASIN2").value;				
				
				xmlHttp=GetXmlHttpObject();

				var url="createChartData.php?asin1="+encodeURIComponent(ASIN1)+"&asin2="+encodeURIComponent(ASIN2)+"&PlotPoints=10";
				xmlHttp.onreadystatechange=reallyDrawChart;
				xmlHttp.open("GET",url,true);
				xmlHttp.send(null);
		}
}

function checkDefaultValue()	{
	if	(document.getElementById("productName").value == "Sharp Aquos LC42D72U 42\" 1080p LCD HDTV")	{
			document.getElementById("productName").value = "";		
			document.getElementById("productName").style.color = "#000000";
	}
	newProductSearchText = true;
}


function reallyDrawChart() 	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")	 {
			if (xmlHttp.responseText!=null ) {
				var objectItemold = document.getElementById("analizingReviews");
			    objectItemold.style.display = "none";

				// UPDATE the chart part..
				updateChartArea();
			}						
	} 
}

function updateChartArea()	{
	var uid = Math.round(Math.random() * 10000);
				var ASIN1 = document.getElementById("ASIN1").value;
				var ASIN2 = document.getElementById("ASIN2").value;				
				var querystr = escape("reviewmap.php?asin1="+ASIN1+"&asin2="+ASIN2+"&PlotPoints=10");
			
				var flashobj = "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' WIDTH=700 HEIGHT=423  id='charts' ALIGN=''>";
				var param1 = "<PARAM NAME=movie VALUE='charts.swf?library_path=charts_library&php_source="+querystr+"'>";
				var param2 = "<PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#ffffff><PARAM NAME=wmode VALUE=transparent>";
				var emd = "<EMBED src='charts.swf?library_path=charts_library&php_source="+querystr+"' quality=high bgcolor=#ffffff WIDTH=700 HEIGHT=423 NAME='charts' ALIGN='' swLiveConnect='true' wmode=transparent TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/go/getflashplayer'></EMBED></OBJECT>\n";
				var ht = flashobj+param1+param2+emd;	

				document.getElementById("thechart").innerHTML = ht;			
				document.getElementById("thechart").style.display = "";
				document.getElementById("legend").style.display = "";						

}



function chartClick(col,row,value,date,featureName,msg){
	// let's go hunt all the relevant reviews with searchname! (and ASIN1...)
	
	//Modalbox.show('stam text', {title: 'adar', width: 300});
	var ASIN1 = document.getElementById("ASIN1").value;
	var prodName= document.getElementById("productName").value;

	var url="fetchReviewTexts.php?asin1="+encodeURIComponent(ASIN1)+"&feature="+encodeURIComponent(featureName);
	var loadS = "Please wait.  Loading "+featureName+ " stuff...";
	var titleS = prodName + ": \"" + featureName + "\" related user reviews";
	var closeS = "Close window";
	Modalbox.show(url, {title: titleS, loadingString: loadS, width: 600, height: 500, overlayClose: true, closeString: closeS });

	return;
	
	var objectItemold = document.getElementById("fetchingActualReviews");
    objectItemold.style.display = "";
	
	var objectItemold = document.getElementById("reviewsDiv");
    objectItemold.style.display = "none";

	document.getElementById("featureName").innerHTML = featureName;

	xmlHttp=GetXmlHttpObject();
	
	
	xmlHttp.onreadystatechange=showActualReviewText;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function showActualReviewText() 	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")	 {
				var objectItemold = document.getElementById("fetchingActualReviews");
			    objectItemold.style.display = "none";
			    
				document.getElementById("reviewsDiv").innerHTML = xmlHttp.responseText;
			    var objectItemold = document.getElementById("reviewsDiv");
			    objectItemold.style.display = "";
				
		}
}





function submitenterSearch(myfield,e) {
			var keycode;
			if (window.event) keycode = window.event.keyCode;
			else if (e) keycode = e.which;
			else return true;
			
			if (keycode == 13)	   {
			   
			   	review();		
			   		   
				objectItemold = document.getElementById("submit1");
				objectItemold.focus();   
			   return false;
			   }
			else
			   return true;
		}

