/**
 * ajax.js
 *
 */

//global variables
var req;
var reqa;
var reqBook
var which;
var pollingTimeout = 5000;
var tryPolling = 0;
var maxtry = 3;
var divIdCount;
var lastdivIdCount;
var quickResultView;


/**
 * Get the contents of the URL via an Ajax call
 * url - to get content from (e.g. /struts-ajax/sampleajax.do?ask=COMMAND_NAME_1) 
 * nodeToOverWrite - when callback is made
 * nameOfFormToPost - which form values will be posted up to the server as part 
 *					of the request (can be null)
*/
function retrieveURL(url, rtype) {
	if (window.XMLHttpRequest) { // Non-IE browsers
    	req = new XMLHttpRequest();
      	try {
      		if(req){
      			req.onreadystatechange = processStateChange;
       			req.open(rtype, url, true); 
       			req.send(null);
       		}
      	} catch (e) {
     		document.getElementById('bodydivs').innerHTML = showErrorPage();
      	}
   	} else if (window.ActiveXObject) { // IE
    	req = new ActiveXObject("Microsoft.XMLHTTP");
      	try{
      		if (req) {
      			req.onreadystatechange = processStateChange;
        		req.open(rtype, url, true);
        		req.send();
        	}
     	}catch(E){
    		document.getElementById('bodydivs').innerHTML = showErrorPage();
    	}	
   	}
}

function retrieveSearchInBookURL(url, rtype, divIdNum, quickResultInBook) {
	divIdCount = divIdNum;
	quickResultView = quickResultInBook;
	if (window.XMLHttpRequest) { // Non-IE browsers
    	reqBook = new XMLHttpRequest();
      	try {
      		if(reqBook){
      			reqBook.onreadystatechange = processStateChangeForSearchInBook;
       			reqBook.open(rtype, url, true); 
       			reqBook.send(null);
       		}
      	} catch (e) {
     		document.getElementById('bodydivs').innerHTML = showErrorPage();
      	}
   	} else if (window.ActiveXObject) { // IE
    	reqBook = new ActiveXObject("Microsoft.XMLHTTP");
      	try{
      		if (reqBook) {
      			reqBook.onreadystatechange = processStateChangeForSearchInBook;
        		reqBook.open(rtype, url, true);
        		reqBook.send();
        	}
     	}catch(E){
    		document.getElementById('bodydivs').innerHTML = showErrorPage();
    	}	
   	}
}

function processStateChangeForSearchInBook() 
{
	try 
	{
		if (reqBook.readyState == 4) 
		{ 
		  if(quickResultView)
		  {
				if (reqBook.status == 200) 
				{ 
					var responseText = reqBook.responseText;
					if(responseText.length>8000){
						document.getElementById('searchResultInBookText'+divIdCount).innerHTML = showErrorPage('bookview');
					}else{
						document.getElementById('searchResultInBookText'+divIdCount).innerHTML = responseText;
						
					}
					if(lastdivIdCount>=0 && document.getElementById('searchResultInBook'+lastdivIdCount) && divIdCount!=lastdivIdCount){
						document.getElementById('searchResultInBook'+lastdivIdCount).style.display = 'none';
						document.getElementById('searchResultInBookText'+lastdivIdCount).innerHTML = ' ';
					}
					document.getElementById('searchResultInBook'+divIdCount).style.display = 'block';
					lastdivIdCount = divIdCount;
				} 
				else 
				{
					document.getElementById('searchResultInBookText'+divIdCount).innerHTML = showErrorPage('bookview');
				}
				document.getElementById('loadingDiv').style.display = "none";
		  }else{
				if (reqBook.status == 200) 
				{ // OK response
					var responseText = reqBook.responseText;
					if(responseText.length>8000){
						document.getElementById('bodydivs').innerHTML = responseText;
					}else{
						document.getElementById('bodydivs').innerHTML = responseText;
					}
				} 
				else 
				{
					document.getElementById('bodydivs').innerHTML = showErrorPage();
				}
				document.getElementById('loadingDiv').style.display = "none";		  
		  }
		}
		else
		{
			document.getElementById('loadingDiv').style.display = "block";
		}
	} 
	catch(e) 
	{
		document.getElementById('loadingDiv').style.display = "none";
		document.getElementById('bodydivs').innerHTML = showErrorPage();
	}		
}

function closeSearchInBookWindow(divIdNum)
{
	if (document.getElementById("searchResultInBook"+divIdNum) != null)
	{
		document.getElementById("searchResultInBook"+divIdNum).style.display = 'none';
	}	
}

function retrieveURLForState(url, rtype) {
	if (window.XMLHttpRequest) { // Non-IE browsers
    	req = new XMLHttpRequest();
        try {
         	if(req){
         		req.onreadystatechange = processStateChangeForState;
          		req.open(rtype, url, true); 
          		req.send(null);
          	}
         } catch (e) {
        	alert(serverCommunicationMessage + " " +e);
         }
  	} else if (window.ActiveXObject) { // IE
        req = new ActiveXObject("Microsoft.XMLHTTP");
        try{
         	if (req) {
        		req.onreadystatechange = processStateChangeForState;
           		req.open(rtype, url, true);
           		req.send();
           	}
        }catch(E){
       		alert(serverCommunicationMessage + " " +e);
       	}	
  	}
}
/*
 * Set as the callback method for when XmlHttpRequest State Changes 
 * used by retrieveUrl
 */
function processStateChanges() {
	if (req.readyState == 4) { // Complete
    	if (req.status == 200) { // OK response
    	  	document.getElementById('1').innerHTML = req.responseText;
     	} else {
        	alert(serverResponseMessage + " " + req.statusText);
	   	}
   	}
}



/**
 * Get the contents of the URL via an Ajax call
 * url - to get content from (e.g. /struts-ajax/sampleajax.do?ask=COMMAND_NAME_1) 
 * nodeToOverWrite - when callback is made
 * nameOfFormToPost - which form values will be posted up to the server as part 
 *					of the request (can be null)
*/
function submitPreCheckout(url, rtype) {
	if (window.XMLHttpRequest) { // Non-IE browsers
    	req = new XMLHttpRequest();
      	try {
      		if(req){
      			req.onreadystatechange = processStateChangeForPreCheckout;
       			req.open(rtype, url, true); 
       			req.send(null);
       		}
      	} catch (e) {
      	 alert("error"); 
     		//document.getElementById('bodydivs').innerHTML = showErrorPage();
      	}
   	} else if (window.ActiveXObject) { // IE
    	req = new ActiveXObject("Microsoft.XMLHTTP");
      	try{
      		if (req) {
      			req.onreadystatechange = processStateChangeForPreCheckout;
        		req.open(rtype, url, true);
        		req.send();
        	}
     	}catch(E){
     	alert("error");
    		//document.getElementById('bodydivs').innerHTML = showErrorPage();
    	}	
   	}
}



/*
 * Set as the callback method for when XmlHttpRequest State Changes 
 * used by retrieveUrl
 */
function processStateChangeForPreCheckout() {
	if (req.readyState == 4) { // Complete
    	if (req.status == 200) { // OK response
				var responsetxt = req.responseText;
				if(responsetxt.match("<")){
				  document.getElementById('bodydivs').innerHTML = responsetxt;    	
				}else{
				  document.getElementById("control").value = responsetxt;
					document.getElementById("dotpaymail").name = "email";
				  var expCheckoutForm = document.getElementById("checkoutForm");
				  expCheckoutForm.action = paymentGatewayURL; // this variable is initialized in "shoppingcart.jsp"
				  expCheckoutForm.submit();
				}
    	 //alert(req.responseText);
     	} else {
     	alert("error");
        	//alert(serverResponseMessage + " " + req.statusText);
	   	}
   	}
}


 
/**  
 * gets the contents of the form as a URL encoded String
 * suitable for appending to a url
 * @param formName to encode
 * @return string with encoded form values , beings with &
 */ 
function getFormAsString(formName){
   
	returnString ="";
 	 
 	 //Get the form values	
 	 formElements=document.forms[formName].elements;
 	
 	 //loop through the array , building up the url	
 	 for ( var i=formElements.length-1; i>=0; --i ){
 		 //we escape (encode) each value
 		 returnString=returnString+"?"+escape(formElements[i].name)+"="+escape(formElements[i].value);
 	 }

 	 //return the values
 	 return returnString; 
}
 
/**
 * Splits the text into <span> elements
 * @param the text to be parsed
 * @return array of <span> elements - this array can contain nulls
 */
function splitTextIntoSpan(textToSplit)
{
	//Split the document
   	returnElements=textToSplit.split("</span>")
 	
 	//Process each of the elements 	
 	for ( var i=returnElements.length-1; i>=0; --i ){
 		
 		//Remove everything before the 1st span
 	 	spanPos = returnElements[i].indexOf("<span>");		
 		
 	 	//if we find a match , take out everything before the span
 	 	if(spanPos>0){
 		 	 subString=returnElements[i].substring(spanPos);
 		 	 returnElements[i]=subString;
 	 	} 
 	}
 	return returnElements;
}

/*
 * Replace html elements in the existing (ie viewable document)
 * with new elements (from the ajax requested document)
 * WHERE they have the same name AND are <span> elements
 * @param newTextElements (output of splitTextIntoSpan)
 *					in the format <span id=name>texttoupdate
 */
function replaceExistingWithNewHtml(newTextElements){
   //loop through newTextElements
 	 for ( var i=newTextElements.length-1; i>=0; --i ){
     //check that this begins with <span
 		 if(newTextElements[i].indexOf("<span")>-1){
 		   //get the name - between the 1st and 2nd quote mark
 			 startNamePos=newTextElements[i].indexOf('"')+1;
 			 endNamePos=newTextElements[i].indexOf('"',startNamePos);
 			 name=newTextElements[i].substring(startNamePos,endNamePos);
 		
 		   //get the content - everything after the first > mark
 			 startContentPos=newTextElements[i].indexOf('>')+1;
 			 content=newTextElements[i].substring(startContentPos);
 			
 			 //Now update the existing Document with this element
 		   //check that this element exists in the document
	 		 if(document.getElementById(name)){
	 			document.getElementById(name).innerHTML = content;
	 		 } else {
	 			 alert("Element:"+name+"not found in existing document");
	 		 }
 		 }
	}
}

var orig;				
function showProgress(name, imgsrc)
{
	prg = " <div style='width:100%; margin-top:200px; text-align:center'><img src="+imgsrc+"alt=\"\" title=\"\"></div>";
	progressViewer = document.getElementById(name);
	orig=progressViewer.innerHTML;
	progressViewer.innerHTML = prg;
}

function hideProgress(name)
{
	progressViewer = document.getElementById(name);
	//progressViewer.innerHTML = orig;
}
	
function retrieveLeftMenu(url, rtype) {
	if (window.XMLHttpRequest) { // Non-IE browsers
    	reqa = new XMLHttpRequest();
      	try {
      		if(reqa){
	      		reqa.onreadystatechange = processMenuStateChange;
	       		reqa.open(rtype, url, true); 
	       		reqa.send(null);
       		}
      	} catch (e) {
     		alert(serverCommunicationMessage + " " +e);
      	}
   	} else if (window.ActiveXObject) { // IE
      	reqa = new ActiveXObject("Microsoft.XMLHTTP");
      	try{
      		if (reqa) {
	      		reqa.onreadystatechange = processMenuStateChange;
	        	reqa.open(rtype, url, true);
	        	reqa.send();
        	}
     	}catch(E){
    		alert(serverCommunicationMessage + " " +e);
    	}	
   	}
}
	
function processMenuStateChange() {
	
	if (reqa.readyState == 4) { // Complete
		if (reqa.status == 200) { // OK response
			try{
				
			}catch(e){
				alert(e.message);
			}
			document.getElementById('menudivs').innerHTML = reqa.responseText;
 			//hideProgress("bodydivs");
			document.getElementById('tagCloudComp').innerHTML = "";
		} else {
  			alert(serverResponseMessage + " " + reqa.statusText);
		}
	}
}
 		
function updateToHomeLeft(refVal)
{
   retrieveLeftMenu("updateLeftHomeMenu.do?refreshBookTile="+refVal,"GET");
}
	
function updateToBookLeft()
{
	//progressViewer = document.getElementById(name);
	//progressViewer.innerHTML = orig;
}	

function keyPressCheck(e, formName){
    
	if(e.type=='keypress'){
		var unicode=e.keyCode? e.keyCode : e.charCode;
		if(unicode==13){
 			if (formName == 'searchForm') {
				startSearch();
			} else if (formName == 'saveSearch') {
				saveSearches();
			} else {
				submitsearch(formName, 'GET');
			}
  			return false;
   		}
   	}
}

function encodeHTML(text) {
	
	if (typeof(text) != "string")
		text = text.toString();
		text = text.replace(
		/&/g, "&amp;").replace(
		/"/g, "&quot;").replace(
		/</g, "&lt;").replace(
		/>/g, "&gt;").replace(
		/\+/g, "&#43;");

	return text;
}

function encode(obj) {
	var unencoded = obj;
	return escape(unencoded);
}

function decode(obj) {
	var encoded = obj;
	return unescape(encoded.replace(/\+/g,  " "));
}
   
function updateSearchTileToHome(){
	return "<form action=\"search.do\" method=\"get\" id=\"searchForm\" style=\"margin-top:0px; margin-bottom:0px;\"><table><tr><td><strong>rightMenu.label.search</strong> :</td><td><input type=\"text\" id=\"searchTerm\" name=\"searchTerm\" class=\"searchText\" size=\"50\" maxlength=\"200\" value=\"${sessionScope.searchCriteria.allwords}\" onkeypress=\"return keyPressCheck(event, 'searchForm');\"/><input type=\"hidden\" id=\"optionValue\" name=\"optionValue\" value=\"2\" /><input type=\"hidden\" name=\"home\" id=\"home\" value=\"true\" /></td><td><img src=\"publisher/images/search_button.gif\" id=\"searchImg\" border=\"0\" alt=\"Search\" title= \"Search\" onclick=\"javascript:startSearch()\"></td><td><a href=\"advancesearchHome.do\" class=\"leftlinks\">rightMenu.label.advancedsearch</a></td></tr></table></form>";
}

function updateSearchTileToHome_withoutsearchbox(){
	return "<form action=\"search.do\" method=\"get\" id=\"searchForm\" style=\"margin-top:0px; margin-bottom:0px;\"><table><tr><td><input type=\"text\" id=\"searchTerm\" name=\"searchTerm\" class=\"searchText\" size=\"25\" maxlength=\"200\" value=\"${sessionScope.searchCriteria.allwords}\" onkeypress=\"return keyPressCheck(event, 'searchForm');\"/><input type=\"hidden\" id=\"optionValue\" name=\"optionValue\" value=\"2\" /><input type=\"hidden\" name=\"home\" id=\"home\" value=\"true\" /></td><td><img src=\"publisher/images/search_button.gif\" alt=\"Search\" title= \"Search\" id=\"searchImg\" border=\"0\" alt=\"\" title= \"\" onclick=\"javascript:startSearch()\"></td><td><a href=\"advancesearchHome.do\" class=\"leftlinks\">rightMenu.label.advancedsearch</a></td></tr></table></form>";
}
	
function getErrorPage(){
    return "<table width=\"100%\" height=\"100%\"  border=\"0\" cellpadding=\"5\" cellspacing=\"0\"><tr><td height=\"10\" colspan=\"2\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"7\" background=\"publisher/images/headingbg.gif\"><img src=\"publisher/images/headingleft1.gif\" width=\"7\" height=\"36\" alt=\"\" title=\"\" /></td><td width=\"50%\" style=\"background-image:url(publisher/images/headingbg.gif); background-repeat:repeat-x;\" class=\"tabheading\">category</td><td width=\"50%\" align=\"right\" class=\"tabheading\" style=\"background-image:url(publisher/images/headingbg.gif); background-repeat:repeat-x;\">&nbsp;</td><td width=\"7\" align=\"right\" background=\"publisher/images/headingbg.gif\"><img src=\"publisher/images/headingright1.gif\" alt=\"\" title=\"\"width=\"7\" height=\"36\" /></td><td>&nbsp;</td></tr></table></td></tr><tr><td colspan=\"2\" valign=\"top\"><table cellspacing=\"0\" cellpadding=\"3\" width=\"100%\" border=\"0\"><tbody><tr><td align=\"middle\">&nbsp;</td></tr><tr><td align=\"middle\"><TABLE cellSpacing=0 cellPadding=0 width=328 border=0><TBODY><TR><TD vAlign=bottom align=center background=\"publisher/images/signin_box1.gif\" height=33><TABLE cellSpacing=0 cellPadding=0 width=\"94%\" border=0><TBODY><TR><TD class=welcometext align=left height=25><strong>simpleSearch.label.emptyheading</strong></TD></TR></TBODY></TABLE></TD></TR><TR><TD align=center background=\"publisher/images/signin_box2.gif\"><TABLE cellSpacing=0 cellPadding=4 width=\"92%\" border=0><TBODY><TR></TR><TR><TD class=textelevenarial align=center><TABLE width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"0\"><TR><TD width=\"12%\" rowspan=\"2\" align=\"center\" valign=\"top\" class=textelevenarial><img src=\"publisher/images/icon_alert.gif\" alt=\"alert\" title=\"alert\"/></TD><TD width=\"88%\" class=\"textelevenarial\">simpleSearch.label.emptymessage</TD></TR></TABLE></TD></TR><TR><TD class=textelevenarial align=center></TD></TR></TBODY></TABLE></TD></TR><TR><TD align=center background=\"publisher/images/signin_box3.gif\" height=11></TD></TR></TBODY></TABLE></td></tr></tbody></table></td></tr></table>";
}
  
function getBookViewErrorPage(){
    return "<table width=\"100%\" height=\"100%\"  border=\"0\" cellpadding=\"5\" cellspacing=\"0\"><tr><td colspan=\"2\" valign=\"top\"><table cellspacing=\"0\" cellpadding=\"3\" width=\"100%\" border=\"0\"><tbody><tr><td align=\"middle\"><TABLE cellSpacing=0 cellPadding=0 width=328 border=0><TBODY><TR><TD vAlign=bottom align=center background=\"publisher/images/signin_box1.gif\" height=33><TABLE cellSpacing=0 cellPadding=0 width=\"94%\" border=0><TBODY><TR><TD class=welcometext align=left height=25><strong>simpleSearch.label.emptyheading</strong></TD></TR></TBODY></TABLE></TD></TR><TR><TD align=center background=\"publisher/images/signin_box2.gif\"><TABLE cellSpacing=0 cellPadding=4 width=\"92%\" border=0><TBODY><TR></TR><TR><TD class=textelevenarial align=center><TABLE width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"0\"><TR><TD width=\"12%\" rowspan=\"2\" align=\"center\" valign=\"top\" class=textelevenarial><img src=\"publisher/images/icon_alert.gif\" alt=\"alert\" title=\"alert\"/></TD><TD width=\"88%\" class=\"textelevenarial\">simpleSearch.label.emptymessage</TD></TR></TABLE></TD></TR><TR><TD class=textelevenarial align=center></TD></TR></TBODY></TABLE></TD></TR><TR><TD align=center background=\"publisher/images/signin_box3.gif\" height=11></TD></TR></TBODY></TABLE></td></tbody></table></td></tr></table>";
}
  
String.prototype.replaceAll = function(v1,v2)
{
    var temp = this;
    var i = temp.indexOf(v1);
    while(i > -1)
    {
	  	temp = temp.replace(v1, v2);
  		i = temp.indexOf(v1, i + v2.length + 1);
    }
    return temp;
}

function startPolling(pollingInterval){
    if(pollingInterval!=null){
    	pollingTimeout =  pollingInterval;
    }
    var t=setTimeout("doPolling('poll.do?pollreq=working','GET')", pollingTimeout);
}


function doPolling(url, rtype){ 
  if (window.XMLHttpRequest) { // Non-IE browsers
    reqa = new XMLHttpRequest();
    try {
      if(reqa){
        reqa.onreadystatechange = processPollingStateChange;
        reqa.open(rtype, url, true); 
        reqa.send(null);
      }
    } catch (e) {
	tryPolling = tryPolling+1;
	if(tryPolling<=maxtry){
		startPolling(pollingTimeout);
	}else{
	  //alert("Server is gone away");
	}
    }
  } else if (window.ActiveXObject) { // IE
    reqa = new ActiveXObject("Microsoft.XMLHTTP");
    try{
      if (reqa) {
        reqa.onreadystatechange = processPollingStateChange;
        reqa.open(rtype, url, true);
        reqa.send();
      }
    }catch(E){
	tryPolling = tryPolling+1;
	if(tryPolling<=maxtry){
		startPolling(pollingTimeout);
	}else{
	  //alert("Server is gone away");
	}
    }	 
  }
}

function processPollingStateChange() {
	
	if (reqa.readyState == 4) { // Complete
		if (reqa.status == 200) { // OK response
			try{
				
			}catch(e){
				alert(e.message);
			}
			startPolling(pollingTimeout);
			//document.getElementById('menudivs').innerHTML = reqa.responseText;
 			//hideProgress("bodydivs");  			
		} else {
		        tryPolling = tryPolling+1;
		        if(tryPolling<=maxtry){
                          startPolling(pollingTimeout);
		        }else{
		          //alert("Server is gone away");
		        }
  			
		}
	}
}
var varisbn="";
function showChapters(url, rtype,isbn) {	
		varisbn=isbn;

		rtype="GET";
	if (window.XMLHttpRequest) { // Non-IE browsers
    	req = new XMLHttpRequest();
      	try {
      		if(req){
      			req.onreadystatechange = chapterDetail;
       			req.open(rtype, url, true); 
       			req.send(null);
       		}
      	} catch (e) {
			alert(e);     		
      	}
   	} else if (window.ActiveXObject) { // IE
    	req = new ActiveXObject("Microsoft.XMLHTTP");
      	try{
      		if (req) {
      			req.onreadystatechange = chapterDetail;
        		req.open(rtype, url, true);
        		req.send();
        	}
     	}catch(E){    		
    	}	
   	}
}

function deleteChapters(url, rtype,isbn,mess) {
	
		varisbn=isbn;

		var isCnfrm = confirm(mess);
		if(isCnfrm){
       rtype="GET";
		if (window.XMLHttpRequest) { // Non-IE browsers
    	req = new XMLHttpRequest();
      	try {
      		if(req){
      			req.onreadystatechange = deletenotes;
       			req.open(rtype, url, true); 
       			req.send(null);
       		}
      	} catch (e) {
			alert(e);
     		//document.getElementById('bodydivs').innerHTML = showErrorPage();
      	}
   	} else if (window.ActiveXObject) { // IE
    	req = new ActiveXObject("Microsoft.XMLHTTP");
      	try{
      		if (req) {
      			req.onreadystatechange = deletenotes;
        		req.open(rtype, url, true);
        		req.send();
        	}
     	}catch(E){
    		//document.getElementById('bodydivs').innerHTML = showErrorPage();
    	}	
   	}
	}	
}

function chapterDetail() {

	if (req.readyState == 4) { // Complete
    	if (req.status == 200) { // OK response

    	 var element =document.getElementById(varisbn);
          element.innerHTML =  req.responseText;
          element.style.display="block";
		  var selement=document.getElementById(varisbn+"S");
		   var helement=document.getElementById(varisbn+"H");
		   var stelement=document.getElementById(varisbn+"ST");

		   var htelement=document.getElementById(varisbn+"HT");
		   if(selement!=null){
			  selement.style.display="none";
			  stelement.style.display="none";
		   }
		   if(helement!=null){
			 helement.style.display="block";
 			 htelement.style.display="block";
		   }
			 
			} else {
        	alert(serverResponseMessage + " " + req.statusText);
	   	}
   	}
}

function deletenotes() {

	if (req.readyState == 4) {
    	if (req.status == 200) {

    		var element = document.getElementById("bodydivs");		
          	element.innerHTML =  req.responseText;
		} else {
        	alert(serverResponseMessage + " " + req.statusText);
	   	}
   	}
}

function hideChapters(isbn) {

   	var element=document.getElementById(isbn);
   	if(element!=null){
		element.style.display="none";
 	}

	var selement=document.getElementById(isbn+"S");
	var helement=document.getElementById(isbn+"H");
	if(selement!=null){
		selement.style.display="block";
	}
	
	if(helement!=null){
		helement.style.display="none";
	}
	
	var stelement=document.getElementById(isbn+"ST");
	var htelement=document.getElementById(isbn+"HT");
	if(stelement!=null){
		stelement.style.display="block";
	}
	
	if(htelement!=null){
		htelement.style.display="none";
	}
}

function fetchUserOptions(divCounter, end) 
{
	var userOptionsDivId;
	var priceDivId;
	
	for (var i=1; i<=end ; i++)
	{
		userOptionsDivId = "userOptionsMenu" + i;
		priceDivId = "priceMenu" + i;
		
		if (document.getElementById(priceDivId) != null) {
			document.getElementById(priceDivId).style.display='none';
		}
		
		if (i != divCounter)
		{
			if (document.getElementById(userOptionsDivId) != null) {
				document.getElementById(userOptionsDivId).style.display='none';
			}			
		}		
	}
	
	userOptionsDivId = "userOptionsMenu" + divCounter;

	if (document.getElementById(userOptionsDivId).style.display == 'none') 
	{
		document.getElementById(userOptionsDivId).style.display='block';	
	} 
	else 
	{
		document.getElementById(userOptionsDivId).style.display='none';
	}	
}

function fetchPriceDetails(divCounter, bookId, end) 
{	
	
	var userOptionsDivId;
	var priceDivId;
	this.divCounter = divCounter;

	for (var i=1; i<=end ; i++)
	{
		userOptionsDivId = "userOptionsMenu" + i;
		priceDivId = "priceMenu" + i;
		
		if (document.getElementById(userOptionsDivId) != null) {
			document.getElementById(userOptionsDivId).style.display='none';
		}
		if (i != divCounter)
		{
			if (document.getElementById(priceDivId) != null) {
				document.getElementById(priceDivId).style.display='none';
			}
		}		
	}
		var url = "priceDisplay.do?id="+bookId;		
		getPriceDetails(url, "GET");
	
}
	
function getPriceDetails(ReqUrl, callType) 
{
    try
    {
	    getXmlHTTPReqObject();
	
		xmlHttp.onreadystatechange = processPriceDetails;
		
		xmlHttp.open("GET", ReqUrl, true);
	  xmlHttp.send(null);
	}
	catch (e) 
	{
	    alert(e);
	}
}
	
function processPriceDetails() 
{			

	if (xmlHttp.readyState == 4) 
	{ 
		if (xmlHttp.status == 200) 
		{ 

			var resp =  xmlHttp.responseText;
			var respToken =  resp.split("##BRK##");
			for(i=0; i<respToken.length; i++)
			{
			  var divResp =  respToken[i].split(":::"); 
			  var priceDetailsId = "priceDetails" + divResp[0];
			  var cartButtonId = "cartButton" + divResp[0];
			  document.getElementById(priceDetailsId).innerHTML = divResp[1];	
			  //document.getElementById(cartButtonId).style.display='block';		
			}
		} 
		else 
		{
			alert(serverResponseMessage + " " + xmlHttp.statusText);
		}
	} else {
		//document.getElementById(loadingId).style.display='block';		
	}
}

function getXmlHTTPReqObject(){
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		isIE = true;
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

srch_count=0;
function openSaveSearch() 
{	
	if(srch_count==0){
		document.getElementById("saveSearch").style.display = 'block';
		document.getElementById('saveSearchMsg').style.display="none";
		document.getElementById("errorDiv").innerHTML = '';
		document.getElementById("searchTitle").value = '';
		document.getElementById("searchTitle").focus();
		srch_count=1;
	}
	else{
		document.getElementById('saveSearch').style.display='none';
		document.getElementById('saveSearchMsg').style.display='none';
		srch_count=0;
	}
}

function closeSaveSearch(obj)
{
	if (document.getElementById(obj) != null) {
		document.getElementById(obj).style.display = 'none';
	}
}

function saveSearches()
{	
	var searchTitle = trimAll(document.getElementById("searchTitle").value);
	
	if (searchTitle.length > 0)
	{
		var searchUrl = "saveSearchHome.do?requestType=Save&searchTitle=" + encodeURIComponent(searchTitle);
		saveUserSearches(searchUrl, "GET");
	} else {
		alert(searchTitleRequired);
		document.getElementById('searchTitle').focus();
	}	
}

function saveUserSearches(url, rtype) 
{
    try
    {
	    getXmlHTTPReqObject();
	
		xmlHttp.onreadystatechange = processUserSearchDetails;
		
		xmlHttp.open("GET", url, true);
	    xmlHttp.send(null);
			
	}
	catch (e) 
	{
	    alert(searchTitleMessage);
		document.getElementById('searchTitle').focus();
	}
}

function processUserSearchDetails() 
{	
	if (xmlHttp.readyState == 4) 
	{ 
		if (xmlHttp.status == 200) 
		{ 
		  	var resp =  xmlHttp.responseText;
		  	var objt = new Function("return "+resp)();
			var result = objt.Result;
		  	
		  	if (result == 'true') {
		  		document.getElementById('saveSearch').style.display="none";
				document.getElementById('saveSearchMsg').style.display='block';
				srch_count = 0;
		  	} else {
		  		alert(searchTitleMessage);
			}		  		
	    } 
	    else 
	    {
			alert(searchTitleMessage);
	    }
	}
}

function deleteSearch(searchId)
{
	var url = "saveSearchHome.do?requestType=Delete&searchId=" + searchId;
	deleteUserSearches(url, "GET");
}

function deleteUserSearches(url, rtype) 
{
    try
    {
	    getXmlHTTPReqObject();
	
		xmlHttp.onreadystatechange = deleteUserSearchDetails;
		
		xmlHttp.open("GET", url, true);
	    xmlHttp.send(null);
			
	}
	catch (e) 
	{
	    alert(e);
	}
}

function deleteUserSearchDetails() 
{	
	if (xmlHttp.readyState == 4) 
	{ 
		if (xmlHttp.status == 200) 
		{ 
		  	document.getElementById('bodydivs').innerHTML = xmlHttp.responseText;
	    } 
	    else 
	    {
			alert(e);
	    }
	}
}

function getContentClassification(url)
{
	deleteUserSearches(url, "GET");
}

function getJournalSearchResults(url)
{
	journalSearchResults(url, "GET");
}

function journalSearchResults(url, rtype) 
{
    try
    {
	    getXmlHTTPReqObject();
	
		xmlHttp.onreadystatechange = journalSearchResultsData;
		
		xmlHttp.open("GET", url, true);
	    xmlHttp.send(null);
			
	}
	catch (e) 
	{
	    alert(e);
	}
}

function journalSearchResultsData() 
{
	if (xmlHttp.readyState == 4) 
	{ 
		if (xmlHttp.status == 200) 
		{ 
		  	document.getElementById("searchBodyDiv").innerHTML = xmlHttp.responseText;
			var authorInfoDiv = document.getElementById("authorInfoDiv");
			var editorialDiv = document.getElementById("editorialDiv"); 
			var searchResultsDiv = document.getElementById("searchBodyDiv");
			var pageTopDiv = document.getElementById("pageTopLink");
			if ( editorialDiv != null ) {
				editorialDiv.style.display = "none";
			}
			if ( authorInfoDiv  != null ) {
				authorInfoDiv.style.display = "none";
			}
			searchResultsDiv.style.display = "block";
			window.scrollTo(0, pageTopDiv.offsetTop);
	    }
	    else 
	    {
			alert("some Error");
	    }
	}
}

function populateselectbox(paramtype) 
{
	var url = "shippingPlaces.do";
	
	if(paramtype == 'shiptype') {
	
		url = url + "?type=shiptypes";
		var countryid = document.getElementById('ship_country').value;
		
		if(countryid == "" ){
		  return;
		}
		
		url = url + "&country="+countryid;
		
		
	}
	if(paramtype == 'shipprices') {
		url = url + "?type=shipprices";
		var countryid = document.getElementById('ship_country').value;
		var shiptypeid = document.getElementById('ship_shippingtype').value;	
		
		if(countryid == ""  ){
		  return;
		}	
		if(shiptypeid == ""){
				document.getElementById('shippricesdiv').style.display='none';
				document.getElementById('shipchargesdiv').innerHTML='-';
				document.getElementById('shipdaysdiv').innerHTML='-';
				document.getElementById('shippricesdiverror').style.display='none';
				return;
			}		
		url = url + "&country="+countryid;
		url = url + "&shiptype="+shiptypeid;
	}
    try
    {
		getXmlHTTPReqObject();
		if(paramtype == 'shiptype') {
			xmlHttp.onreadystatechange = selectshiptypesboxdata;
		}
		if(paramtype == 'shipprices') {
			if(document.getElementById('ship_shippingtype').value == ""){
				document.getElementById('shippricesdiv').style.display='none';
				document.getElementById('shipchargesdiv').innerHTML='-';
				document.getElementById('shipdaysdiv').innerHTML='-';
				document.getElementById('shippricesdiverror').style.display='none';
				//populateselectbox('shiptype');
			}else {
				xmlHttp.onreadystatechange = populateshippricesdata;	
			}
		}
		xmlHttp.open("GET", url, true);
	    xmlHttp.send(null);
			
	}
	catch (e) 
	{
	    alert(e);
	}
}


function selectshiptypesboxdata()
{
	if (xmlHttp.readyState == 4) 
	{ 
		if (xmlHttp.status == 200) 
		{ 
			document.getElementById('shiptypesdiv').innerHTML = xmlHttp.responseText;
	    }
	    else 
	    {
			alert("some Error");
	    }
	}
}

function populateshippricesdata()
{
	if (xmlHttp.readyState == 4) 
	{ 
		if (xmlHttp.status == 200) 
		{ 
			var resp =  xmlHttp.responseText;
			if(resp == "SHIP_PRICE_NOT_FOUND"){
			document.getElementById('shippricesdiverror').style.display = 'block';
			
			}
			
			
			var respToken =  resp.split(";");
			for(i=0; i<respToken.length; i++)
			{
			  var divResp =  respToken[i].split("::"); 
			  var divid = divResp[0]+"div";
			  document.getElementById(divid).innerHTML = divResp[1];	
			  document.getElementById(divid+"1").innerHTML = divResp[1];	
			  if(  divResp[0] == 'shipcharges') {
				  var newprice = parseFloat(document.getElementById('totalpriceWithoutShip').value) + parseFloat(divResp[1].split(" ")[1]);
				  document.getElementById('totalprice').value = newprice;
				  document.getElementById('totalPriceDiv').innerHTML=divResp[1].split(" ")[0]+" " + newprice.toFixed(2);		
			  }
			}
			document.getElementById('totalPriceDiv1').innerHTML = document.getElementById('totalPriceDiv').innerHTML;
			document.getElementById('shippricesdiv').style.display = 'block';
			document.getElementById('shippricesdiverror').style.display='none';
		}
	    else 
	    {
			alert("some Error");
	    }
	}
}