/*
	Author: Rajendra Vyas
	Date:	28-July-2008
	Module:	Adlabs Movie Rating Stars

	Update Date : 2/2/2010
	Purpose : Rajendra Vyas Modify the code for browser compability(ie and mozila) in his notice period on date 2/2/2010
	Changes 
		1: xml parsing
		2: CreateTableMozila() method
*/

var imgBigStarArr=new Array();
imgBigStarArr[0]="images/readreview/red_star_big.gif";
imgBigStarArr[1]="images/readreview/white_star_big.gif";
imgBigStarArr[2]="images/readreview/half_red_star.gif";

var rateTheMovieOneTime=false;


// start -- Rajendra Vyas Modify the code for browser compability on date 2/2/2010
var b_type = "ie";
if(window.showHelp)
{
	b_type = "ie"; 
	if(!window.attachEvent) 
		b_type += "mac";
}
if (document.createElementNS) 
	b_type = "dom";
if (navigator.userAgent.indexOf("safari")>-1) 
	b_type = "safari";
if (window.opera) 
	b_type = "opera"; 
// end -- Rajendra Vyas Modify the code for browser compability on date 2/2/2010

function truncateUptoN(floatVal,decPlaces) 
{
	try
	{		
		var numStr=floatVal.toString();
		var decPos = numStr.indexOf(".");			
		if (decPos == -1 )
		{
			return numStr + ".000";			
		}
		else
		{
			// add N zeros to get at least N digits at the 
			// end of the string in case it ends at the decimal			
			for(var i=0;i<decPlaces;i++)
			{
				numStr+="0";
			}			
			return numStr.substr(0,decPos+decPlaces+1);			
		}
	}
	catch(e)
	{
		alert("Method truncateUptoN ::: " + e);
		return floatVal.toString();
	}
}

function setSmallRedStarts(floatStarNum)
{
	try
	{
		var numStr=floatStarNum.toString();
		var valArr = floatStarNum.split(".");	
		var fullStar=parseInt(valArr[0]);
		var halfStar=parseInt(valArr[1]);
		//alert(halfStar);
		for(var i=1;i<=5;i++)
		{
			if(i<=fullStar)
			{
				document.getElementById('Image'+(3+i)+'2').src="images/readreview/red_star_big.gif";
			}
			else
			{
				if((halfStar  >= 25 && halfStar < 50) && (i==(fullStar+1)))
				{
					document.getElementById('Image'+(3+i)+'2').src="images/readreview/half_red_star.gif";				
				}
				else
				{
					if(halfStar > 50 && (i==(fullStar+1)))
					{
						document.getElementById('Image'+(3+i)+'2').src="images/readreview/red_star_big.gif";				
					}
					else
					{
						document.getElementById('Image'+(3+i)+'2').src="images/readreview/white_star_big.gif";
					}
				}
			}
		}
	}
	catch(e)
	{
		alert("Execption=="+e);
	}
}


function setRedStarts(starNum)
{
	if(rateTheMovieOneTime==false)
	{
		for(var i=1;i<=starNum;i++)
		{
			document.getElementById('Image'+(3+i)+'1').src="images/readreview/red_star_big.gif";
			//alert(document.getElementById('Image+i+1').src);
		}
	}
}
function setWhiteStarts(starNum)
{
	if(rateTheMovieOneTime==false)
	{
		for(var i=1;i<=starNum;i++)
		{
			document.getElementById('Image'+(3+i)+'1').src="images/readreview/white_star_big.gif";
			//alert(document.getElementById('Image+i+1').src);
		}
	}
}

function rateTheMovie(starNum)
{
	if(rateTheMovieOneTime==true)
	{
			alert("You can rate this movie only once");
			return false;
	}
	else
	{
		setRedStarts(starNum);
		if(starNum!=-1)
		{
			rateTheMovieOneTime=true;
			alert("Thanks for Rating.");
		}
		var movieName=document.getElementById("movieName");
		var errorFlag=false;
		if(movieName)
		{
			if(movieName.value!="")
			{
				makeRequest('movieRating.asp', '?movieName='+movieName.value+'&stars='+starNum+'&'+Math.random(),1);
			}
			else
				errorFlag=true;
		}
		else
		{
			errorFlag=true;
		}
		if(errorFlag)
		{
			alert(" Movie Name is not specified. \n Please contact to website admin \n Thank you !!!");
			return false;
		}
	}
}

//var http_request = false;
function makeRequest(url, parameters,fnType,strDivID) 
{
  var http_request = false;
  //alert(url + parameters);
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/xml');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
  }
  if(fnType==1)
	  http_request.onreadystatechange = function()
										{
										  if (http_request.readyState == 4) 
										  {
											 if (http_request.status == 200) 
											 {
												  alertContents(http_request);
										 	 }
											 else 
											 {
												alert('There was a problem with the request.');
											 }
										  }
									    }
  else
	  http_request.onreadystatechange = function()
										{
										  if (http_request.readyState == 4) 
										  {
											 if (http_request.status == 200) 
											 {
												  getMovieRatingsInfo(http_request,strDivID);
										 	 }
											 else 
											 {
												alert('There was a problem with the request.');
											 }
										  }
									    }	  

  http_request.open('GET', url + parameters, true);
  http_request.send(null);
}

function alertContents(http_request) 
{
  		try
		{
			var xmldoc = http_request.responseXML;

			var strMovieName;
			var intTotalRating;
			var intTotalStar;
			//alert("xmDoc="+xmldoc);
			if(b_type=="ie") // Rajendra Vyas Modify the code for browser compability on date 2/2/2010
			{
				nodes=xmldoc.selectNodes("/Adlabs/movie/*");					
				//alert(nodes.length);
				strMovieName = nodes.item(0).firstChild.text;
				intTotalRating = nodes.item(1).firstChild.text;
				intTotalStar = nodes.item(2).firstChild.text;
			}
			else
			{
				//nodes=xmldoc.selectNodes("/Adlabs/movie/*");	
				/*nodes=xmldoc.getElementsByTagName("/Adlabs/movie/*");	
				alert(xmldoc.getElementsByTagName("/Adlabs/movie/name/"));
				alert(xmldoc.getElementsByTagName("movie")[0].getElementsByTagName("name")[0].firstChild.nodeValue);
				alert(xmldoc.getElementsByTagName("movie")[0].getElementsByTagName("totalRevie")[0].firstChild.nodeValue);
				alert(xmldoc.getElementsByTagName("movie")[0].getElementsByTagName("totalStars")[0].firstChild.nodeValue);*/
				strMovieName = xmldoc.getElementsByTagName("movie")[0].getElementsByTagName("name")[0].firstChild.nodeValue;
				intTotalRating = xmldoc.getElementsByTagName("movie")[0].getElementsByTagName("totalRevie")[0].firstChild.nodeValue;
				intTotalStar = xmldoc.getElementsByTagName("movie")[0].getElementsByTagName("totalStars")[0].firstChild.nodeValue;
			}
			var movieStarsRating=truncateUptoN(parseFloat(parseFloat(intTotalStar)/parseFloat(intTotalRating)),2);
			//alert("Movie="+strMovieName+" TR="+intTotalRating+" TS="+intTotalStar+" TMS="+movieStarsRating);
			var totalRatings=document.getElementById("totalRatings");
			if(totalRatings)
			{
				totalRatings.innerHTML=intTotalRating;
				setSmallRedStarts(movieStarsRating);
			}
		}
		catch(e)
		{
			alert("Exception = "+e);
		}
	 
}


function getMovieRatingsInfo(http_request,strDivID) 
{
//  if (http_request.readyState == 4) 
//  {
//	 if (http_request.status == 200) 
//	 {
		try
		{
			var xmldoc = http_request.responseXML;
			/*nodes=xmldoc.selectNodes("/Adlabs/movie/*");	
			//nodes=xmldoc.getElementsByTagName("/Adlabs/movie/*");	
			//alert(nodes.length);
			var strMovieName = nodes.item(0).firstChild.text;
			var intTotalRating = nodes.item(1).firstChild.text;
			var intTotalStar = nodes.item(2).firstChild.text;*/

			var strMovieName;
			var intTotalRating;
			var intTotalStar;
			//alert("xmDoc="+xmldoc);
			if(b_type=="ie")  // Rajendra Vyas Modify the code for browser compability on date 2/2/2010
			{
				nodes=xmldoc.selectNodes("/Adlabs/movie/*");					
				//alert(nodes.length);
				strMovieName = nodes.item(0).firstChild.text;
				intTotalRating = nodes.item(1).firstChild.text;
				intTotalStar = nodes.item(2).firstChild.text;
			}
			else
			{
				strMovieName = xmldoc.getElementsByTagName("movie")[0].getElementsByTagName("name")[0].firstChild.nodeValue;
				intTotalRating = xmldoc.getElementsByTagName("movie")[0].getElementsByTagName("totalRevie")[0].firstChild.nodeValue;
				intTotalStar = xmldoc.getElementsByTagName("movie")[0].getElementsByTagName("totalStars")[0].firstChild.nodeValue;
			}


			var movieStarsRating=truncateUptoN(parseFloat(parseFloat(intTotalStar)/parseFloat(intTotalRating)),2);			
			var reviewCount=intTotalRating;			
			try
			{
					//var strTmp=intTotalRating+"="+intTotalStar;
					//CreateTable(movieStarsRating,strTmp,5);

					if(b_type=="ie") 
						CreateTable(movieStarsRating,reviewCount,5,strDivID);
					else
						CreateTableMozila(movieStarsRating,reviewCount,5,strDivID);
					//completeFlag=true;
			}
			catch(e)
			{
				alert("Execption=="+e);
			}			
		}
		catch(e)
		{
			alert("Exception = "+e);
		}
//	 }
//	 else 
//	 {
//		alert('There was a problem with the request.');
//	 }
//  }
}


function displayMovieRatingsDetails(strMovieName,strDivID)
{
	//alert("Movie Name="+strMovieName+" , divID="+strDivID);
	try
	{
		var errorFlag=false;		
		if(strMovieName!="")
		{
			makeRequest('movieRating.asp', '?movieName='+strMovieName+'&stars=-1&'+Math.random(),2,strDivID);
		}
		else
			errorFlag=true;
		
		if(errorFlag)
		{
			alert(" Movie Name is not specified. \n Please contact to website admin \n Thank you !!!");
			return false;
		}
	}
	catch(e)
	{
		alert("Exception => " + e);
	}
}

function wait(msecs)
{
	var start = new Date().getTime();
	var cur = start
	while(cur - start < msecs)
	{
		cur = new Date().getTime();
	}
} 



function CreateTableMozila(movieStarsRating,totalReview,colCount,strDivID)
{
	var numStr=movieStarsRating.toString();
	var valArr = numStr.split(".");	
	var fullStar=parseInt(valArr[0]);
	var halfStar=parseInt(valArr[1]);
	
	//alert("fullStar="+fullStar);
	//alert("halfStar="+halfStar);

	var srcHolder=document.getElementById(strDivID);
	if((srcHolder != null))
	{
		srcHolder.innerHTML = "";
		var srcTable = document.createElement("table");
		var DEFAULT_WIDTH = "270";
		//var DEFAULT_HEIGHT = 100;

		//srcTable.border = 1;
		//srcTable.borderColor = "Black";
		srcTable.cellspacing="0";
		srcTable.cellpadding="0";
	//	srcTable.height = DEFAULT_HEIGHT;
		srcTable.width = DEFAULT_WIDTH;
		var tmpRow = null;
		var tmpCell = null;
		srcHolder.appendChild(srcTable);
		//tmpRow = AppendRow(srcTable)
		tmpRow = document.createElement("TR");
		srcTable.appendChild(tmpRow);
		for(i=1; i<=colCount; i++)
		{
				//tmpCell = AppendCell(tmpRow);
				tmpCell = document.createElement("TD");
				tmpRow.appendChild(tmpCell);
				tmpCell.width="19";
				tmpCell.align="center";
				img = document.createElement("IMG");
				if(i<=fullStar)
				{
					img.src="images/readreview/red_star.gif";
				}
				else
				{
					if((halfStar  >= 25 && halfStar < 50) && (i==(fullStar+1)))
					{
						img.src="images/readreview/half_red_star.gif";
					}
					else
					{
						if(halfStar > 50 && (i==(fullStar+1)))
						{
							img.src="images/readreview/red_star.gif";
						}
						else
						{
							img.src="images/readreview/white_star.gif";						
						}
					}
				}
				img.width="18";
				img.height="16";
				tmpCell.appendChild(img);
				//tmpCell.innerText = i;
				tmpCell = null;
		}
		//tmpCell = AppendCell(tmpRow);
		tmpCell = document.createElement("TD");
		tmpRow.appendChild(tmpCell);
		
		tmpCell.width="6";
		//tmpCell.align="center";
		tmpCell.innerText =" ";
		tmpCell = null;
		//tmpCell = AppendCell(tmpRow);
		tmpCell = document.createElement("TD");
		tmpRow.appendChild(tmpCell);
		tmpCell.width="95";
		//tmpCell.align="center";
		tmpCell.innerHTML = "Guests' Reviews : ";
		tmpCell.className="innertxt"
		//tmpCell.setAttribute('class','innertxt');
		tmpCell = null;
//		tmpCell = AppendCell(tmpRow);
		tmpCell = document.createElement("TD");
		tmpRow.appendChild(tmpCell);
		tmpCell.width="43";
		//tmpCell.align="center";
		tmpCell.className="redtxt";
		tmpCell.innerHTML = totalReview;
		tmpCell = null;
		tmpRow = null;		
	}
}



function CreateTable(movieStarsRating,totalReview,colCount,strDivID)
{
	var numStr=movieStarsRating.toString();
	var valArr = numStr.split(".");	
	var fullStar=parseInt(valArr[0]);
	var halfStar=parseInt(valArr[1]);
	
	//alert("fullStar="+fullStar);
	//alert("halfStar="+halfStar);

	var srcHolder=document.getElementById(strDivID);
	if((srcHolder != null)  && (srcHolder.canHaveChildren))
	{
		srcHolder.innerHTML = "";
		var srcTable = document.createElement("table");
		var DEFAULT_WIDTH = "270";
		//var DEFAULT_HEIGHT = 100;

		//srcTable.border = 1;
		//srcTable.borderColor = "Black";
		srcTable.cellspacing="0";
		srcTable.cellpadding="0";
	//	srcTable.height = DEFAULT_HEIGHT;
		srcTable.width = DEFAULT_WIDTH;
		var tmpRow = null;
		var tmpCell = null;
		srcHolder.appendChild(srcTable);
		tmpRow = AppendRow(srcTable)
		//tmpRow = document.createElement("TR");
		//srcTable.appendChild(tmpRow);
		for(i=1; i<=colCount; i++)
		{
				tmpCell = AppendCell(tmpRow);
				//tmpCell = document.createElement("TD");
				//tmpRow.appendChild(tmpCell);
				tmpCell.width="19";
				tmpCell.align="center";
				img = document.createElement("IMG");
				if(i<=fullStar)
				{
					img.src="images/readreview/red_star.gif";
				}
				else
				{
					if((halfStar  >= 25 && halfStar < 50) && (i==(fullStar+1)))
					{
						img.src="images/readreview/half_red_star.gif";
					}
					else
					{
						if(halfStar > 50 && (i==(fullStar+1)))
						{
							img.src="images/readreview/red_star.gif";
						}
						else
						{
							img.src="images/readreview/white_star.gif";						
						}
					}
				}
				img.width="18";
				img.height="16";
				tmpCell.appendChild(img);
				//tmpCell.innerText = i;
				tmpCell = null;
		}
		tmpCell = AppendCell(tmpRow);
		//tmpCell = document.createElement("TD");
		//tmpRow.appendChild(tmpCell);
		
		tmpCell.width="6";
		//tmpCell.align="center";
		tmpCell.innerText =" ";
		tmpCell = null;
		tmpCell = AppendCell(tmpRow);
		//tmpCell = document.createElement("TD");
		//tmpRow.appendChild(tmpCell);
		tmpCell.width="95";
		//tmpCell.align="center";
		tmpCell.innerText = "Guests' Reviews : ";
		tmpCell.className="innertxt"
		//tmpCell.setAttribute('class','innertxt');
		tmpCell = null;
		tmpCell = AppendCell(tmpRow);
		//tmpCell = document.createElement("TD");
		//tmpRow.appendChild(tmpCell);
		tmpCell.width="43";
		//tmpCell.align="center";
		tmpCell.className="redtxt";
		tmpCell.innerText = totalReview;
		tmpCell = null;
		tmpRow = null;		
	}
}

function AppendRow(srcTable)
{
	if(srcTable != null)
	{
		return srcTable.insertRow();
	}
	else
	{
		alert("Error while creating table. Cause: Container Table is null!");
	}
}

function AppendCell(srcRow)
{
	if(srcRow != null)
	{
		return srcRow.insertCell();
	}
	else
	{
		alert("Error while creating table. Cause: Container row is null!");
	}
}
