var hwin = null;
var rwin = null;
var awin = null;

//opens the moreinfo window whenever the more button is clicked
function openMoreInfoWin(wintitle,action)
{
	document.frmDescriptions.txtAction.value = action;
	document.frmDescriptions.txtWinTitle.value = wintitle;
	var features = "height=400,width=450,scrollbars=yes,titlebar=no,menubar=no";
	var winName = "more";
	hwin = openInSameWindow(hwin,winName,features);
	hwin.moveTo(300,200);
	hwin.focus();
	document.frmDescriptions.method = "Post";
	document.frmDescriptions.action = "More.asp";
	document.frmDescriptions.target = winName;
	document.frmDescriptions.submit();
}

//opens the reviews window from the product page
function openReviewsWin(wintitle,action)
{
	document.frmDescriptions.txtAction.value = action;
	document.frmDescriptions.txtWinTitle.value = wintitle;
	var features = "height=400,width=450,scrollbars=yes,titlebar=no,menubar=no";
	var winName = "reviews";
	rwin = openInSameWindow(rwin,winName,features);
	rwin.moveTo(300,200);
	rwin.focus();
	document.frmDescriptions.method = "Post";
	document.frmDescriptions.action = "More.asp";
	document.frmDescriptions.target = winName;
	document.frmDescriptions.submit();	
}

//opens the awards window from the product page
function openAwardsWin(wintitle,action)
{
	document.frmDescriptions.txtAction.value = action;
	document.frmDescriptions.txtWinTitle.value = wintitle;
	var features = "height=400,width=450,scrollbars=yes,titlebar=no,menubar=no";
	var winName = "awards";
	awin = openInSameWindow(rwin,winName,features);
	awin.moveTo(300,200);
	awin.focus();
	document.frmDescriptions.method = "Post";
	document.frmDescriptions.action = "More.asp";
	document.frmDescriptions.target = winName;
	document.frmDescriptions.submit();	
}

//this function opens content in the same window if its already opened
function openInSameWindow(owin,name,sfeatures)
{
	if (owin==null)
		{
			owin = window.open ('',name,sfeatures);
		}
		else
		{
		if (owin.closed)
			owin = window.open ('',name,sfeatures);
		}
	return owin;
}

//validate the search screen
function ValidateSearch(oSearchForm,sDelimiter,iMinLengthForTextFields)
{
//For additional search parameter restrictions, please refer to the SCHOOLS version of this file
	if ((trim(oSearchForm.txtTitle.value)=="") && 
		(trim(oSearchForm.txtISBN.value)=="") && 
		(trim(oSearchForm.txtAuthor.value)=="") &&
		(oSearchForm.cboLanguage.value=="") &&
		(oSearchForm.cboFormat.value==""))
		
		{
			alert ("Please select at least one search criteria.")
			return false;
		}
		else
		{
			//ADR - Added special characters checks to ISBN field
			if (trim(oSearchForm.txtISBN.value)!="")
			{

				if (!(validateSpecialChars(trim(oSearchForm.txtISBN.value), "ISBN")))
				{
					return false;
				}

			}
		
			if (trim(oSearchForm.txtTitle.value)!="")
			{
				if (!(validateSpecialChars(trim(oSearchForm.txtTitle.value), "Title")))
				{
					return false;
				}

				if (!(checkLength(oSearchForm.txtTitle.value,iMinLengthForTextFields, "Title")))
				{
					return false;
				}
			}
			
			if (trim(oSearchForm.txtAuthor.value)!="")
			{

				if (!(validateSpecialChars(trim(oSearchForm.txtAuthor.value), "Author / Editor")))
				{
					return false;
				}

				if (!(checkLength(oSearchForm.txtAuthor.value,iMinLengthForTextFields, "Author / Editor")))
				{
					return false;
				}
			}
			
			//check if ISBN is entered and any other fields have been selected.
			if ((trim(oSearchForm.txtISBN.value) != "") &&
					((trim(oSearchForm.txtTitle.value) != "") || 
						(trim(oSearchForm.txtAuthor.value) != "") ||
						(oSearchForm.cboLanguage.value != "") ||
						(oSearchForm.cboFormat.value != "")))
			    {
					if (!(confirm("You have entered an ISBN. This will override all other fields.\nDo you wish to continue?")))
					{
						return false;
					}
					else
					{
						overRideFields(oSearchForm, "i");
					}
			    }			
			    			
						


    			return populateSearchCriteria(oSearchForm,sDelimiter);
		}
}


//validate the search screen
function ValidatePenguinSearch(oSearchForm,sDelimiter,iMinLengthForTextFields)
{
	if (oSearchForm.cboTopicArea.value=="")
		
		{
			alert ("Please select a level.")
			return false;
		}
		else
		{
   			return populateSearchCriteria(oSearchForm,sDelimiter);
		}
}


//checks the minimum length of text input in the search
function checkLength(vValue,iLen,sFldName)
{
	vValue = trim(vValue); // ADR - added trim statement
	if (vValue.length < iLen)
		{
			alert (sFldName + " search field should be at least " + iLen + " characters long and without any leading or trailing spaces.\nPlease modify the value to satisfy this criteria.")
			return false;
		}
		else
			return true;
}


//overrides selection based on whether ISBN or Series is selected
function overRideFields(oForm, sFlag)
{	
	oForm.txtTitle.value = "" 
	oForm.txtAuthor.value = "" 
	oForm.cboLanguage.options[0].selected = true;
	oForm.cboFormat.options[0].selected = true;
}

//populates search criteria after validations
function populateSearchCriteria(oForm,sSeperator)
{
	oForm.txtHiddenISBN.value = '';
	var sCriteria = "";
	var sISBN ="";
	
	if (oForm.txtTitle != null)
	{
	    if (oForm.txtTitle.value != "")
		    sCriteria += oForm.txtTitle.value + sSeperator;
	}
		
	if (oForm.txtISBN != null)	sISBN = oForm.txtISBN.value;
	
	if (sISBN != "")
	{
		sCriteria += sISBN  + sSeperator;

		if (sISBN.length ==10)
		{
			sISBN = ConvertISBN10ToISBN13(oForm.txtISBN.value);
		}
		oForm.txtHiddenISBN.value = sISBN;
	}

	if (oForm.txtAuthor != null)
	    {
    	    if (oForm.txtAuthor.value != "") sCriteria += oForm.txtAuthor.value + sSeperator;
		}

	if (oForm.cboTopicArea != null)
	{
	    if (oForm.cboTopicArea.value != "") sCriteria += getComboText(oForm.cboTopicArea) + sSeperator;
	}	
		
	if (oForm.cboLanguage != null)
	{
	    if (oForm.cboLanguage.value != "") sCriteria += getComboText(oForm.cboLanguage) + sSeperator;
	}	
	
	if (oForm.cboFormat != null)
	{
	    if (oForm.cboFormat.value != "") sCriteria += getComboText(oForm.cboFormat) + sSeperator;		
    }

	//strip the last seperator
	sCriteria = sCriteria.substr(0,(sCriteria.length - sSeperator.length));
	oForm.txtSelectedSearchCriteria.value=sCriteria;
	return true;
}

function ConvertISBN10ToISBN13(isbn10)
{
	//Convert a 10 digit ISBN to 13 digit version.
		var isbn13 ="";
		var itotal = 0;
		var iWeight = 0;
		
		var iRem
		var iISBNrem =  isbn10.substr(0,9)
		var checkDigit
	
		if (isbn10.length==10 && IsNumeric(iISBNrem) != false)
		{
			isbn13 = '978' + isbn10.substr(0,9)
	
			for (i = 0; i <= 11; i++)
			{
				if( (i % 2) == 0)
				{
				// mod val 0
					iWeight =  (parseInt(isbn13.substr(i,1)) *1)
				}
				else
				{
				// mod val 0
					iWeight =  (parseInt(isbn13.substr(i,1)) * 3)
				}
				itotal = itotal + iWeight;
			}
			
			iRem = parseInt(itotal % 10)
			checkDigit = 10 - iRem

			if (checkDigit == 10) 
				checkDigit = 0;

			isbn13 = isbn13 + checkDigit;
		}
		else
		{isbn13 = isbn10;}
		
		return isbn13;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


//retrieves the text from the selected values in the dropdowns
function getComboText(oCombo)
{
	return oCombo.options[oCombo.selectedIndex].text;
}

//trims values
function trim(sText) 
{
  return sText.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

//next / previous validation in search results / topic area pages
function navigate(action)
{
	if (action == 'p')
	{
		document.frmSearchResults.Page.value = parseInt(document.frmSearchResults.thisPage.value,10)-1;
	}
	else
	{
		document.frmSearchResults.Page.value = parseInt(document.frmSearchResults.thisPage.value,10)+1;
	}
	document.frmSearchResults.submit();
}

//client side validation functions for valid characters using the same regular expression used for server side validation
function validateSpecialChars(sValue, sFieldName)
{
	//regular expression should be same as in Utils.asp.
	//if any change is made to this expression, make corresponding changes in utils.asp 
	//ADR - modified this regexp to replace / with \/ to negate special character
	var oRegExp =  /[^A-Za-z0-9': \-,.()&!\/]/g; 
	
	var sAlertText = "You have entered invalid characters in the " + sFieldName + " field.\n";
	sAlertText += "The following characters are allowed :\n";
	sAlertText += "Alphanumerics and spaces\n-\n'\n:\n,\n(\n)\n&\n!\n/\n";
	sAlertText += "Please modify the value to satisfy this criteria.";
	
	if (oRegExp.test(sValue))
	{
		alert(sAlertText);
		return false;
	}
	else
		return true;
	
}
