
function preproc(frm,index,code,type,eqCode,iWishList, iUnits)
{ var element, retval, qty, iItemqty, iTotalqty;

	element = "document." + frm + ".qty" + index + ".value";
	qty = eval(element);
	retval=IsNumeric(qty)
	
	if(retval == 1)
	{
		alert(getMessage("iValidQuantity"));
		eval("document." + frm + ".qty" + index + ".value=iUnits");
		eval("document." + frm + ".qty" + index + ".focus()");
		return;
	}
	
	
	if (iWishList == 0){
		iItemqty = qty*1
		if ((iMaxCount != "") && (bMaxItem.toLowerCase()=="true")){
			iTotalqty = parseInt(sCartItems)
			if((iItemqty + iTotalqty)>parseInt(iMaxCount)){
				alert(getMessage("iTotalQtyExceed") +" "+ iMaxCount);
				eval("document." + frm + ".qty" + index + ".value=iUnits");
				//eval("document." + frm + ".qty" + index + ".focus()");
				return;
			}
		}
	document.frmAdditem.qty.value = qty;
	document.frmAdditem.ic.value = code;
	document.frmAdditem.type.value=type;
	document.frmAdditem.EqCode.value=eqCode;
	document.frmAdditem.action = sNonSecurePath + "includes/add_item.asp?Tp=" + sTpCatalog
	document.frmAdditem.submit();
	}
	else{
	document.frmAdditem.qty.value = qty;
	document.frmAdditem.ic.value = code;
	document.frmAdditem.EqCode.value=eqCode;
	document.frmAdditem.action = sNonSecurePath + "includes/AddWishitem.asp?Tp=" + sTpCatalog
	document.frmAdditem.submit();
	}
}
// End of the function preproc(frm,index,code,type)


function getattribute(frm,index,code,type)
{ var iIndex,matrix,arrTemp,strselected,len;
	iItemCount=index
	matrix = false;
	arrTemp = eval("arrSelItems" + iItemCount)
	strselected = eval("document.frmAdd.SelMatrixAtt1" + iItemCount + ".options[document.frmAdd.SelMatrixAtt1" + iItemCount + ".selectedIndex].value"); 
	strselected = strselected + eval("document.frmAdd.SelMatrixAtt2" + iItemCount + ".options[document.frmAdd.SelMatrixAtt2" + iItemCount + ".selectedIndex].value"); 
	
	len = document.frmAdd.elements.length;
	for(iCount=0;iCount<=parseInt(len)-1;iCount++){
		if((document.frmAdd.elements[iCount].name.substring(0,13) == "SelMatrixAtt3") && (document.frmAdd.elements[iCount].name.substring(13,14) == iItemCount))
			strselected = strselected + eval("document.frmAdd.SelMatrixAtt3" + iItemCount + ".options[document.frmAdd.SelMatrixAtt3" + iItemCount + ".selectedIndex].value"); 
	}		
	for(iIndex=0;iIndex<=arrTemp.length-1;iIndex++){
		if(arrTemp[iIndex][1]==strselected){
			code = arrTemp[iIndex][0];
			matrix = true;
			break;
		}
	}
	if(matrix){
		preproc(frm,index,code,type,"",0)
		return;
	}
	else{
		alert(getMessage("iMatrixCombNotPresent"));
		return;
	}
}
// End of the function getattribute(frm,index,code,type)

		function attribCheck(iAttrib3){
			var sAttrib1,sAttrib2,sAttrib3;
			sAttrib1=document.frmMatrix.Attrib1.options[document.frmMatrix.Attrib1.selectedIndex].value;
			sAttrib2=document.frmMatrix.Attrib2.options[document.frmMatrix.Attrib2.selectedIndex].value;
			if(sAttrib1==sAttrib2){
				alert(getMessage("iUniqueAttributes"));
				return false;
			}
			if(iAttrib3=="1"){
				sAttrib3=document.frmMatrix.Attrib3.options[document.frmMatrix.Attrib3.selectedIndex].value;
				if((sAttrib3==sAttrib1)||(sAttrib3==sAttrib2)){
					alert(getMessage("iUniqueAttributes"));
					return false;
				}
			}
			document.frmMatrix.submit();
		}
// End of the function attribCheck(iAttrib3)

function multiselect(type,attribcode3)
{ 
	var len, iIndex, attributes, qty, code, quantity, retval, attribute_codes,iTotalqty,iItemqty;

	iTotalqty=0;
	iItemqty=0;
	qty = "";
	code = "";
	attributes = "";
	attribute_codes = "";
		
	len = document.frmAdd.elements.length;
	for(iIndex=0;iIndex<=parseInt(len)-1;iIndex++){	
		if(document.frmAdd.elements[iIndex].type == "checkbox"){
			if(document.frmAdd.elements[iIndex].checked){
				arr = document.frmAdd.elements[iIndex].value.split("*");
				quantity = eval("document.frmAdd.qty" + arr[0] + ".value")
				retval=IsNumeric(quantity)
				if(retval == 1){
					alert(getMessage("iValidQuantity"));
					eval("document.frmAdd.qty" + arr[0] + ".value=1");
					eval("document.frmAdd.qty" + arr[0] + ".focus()");
					return;
				}
				qty = qty + quantity + "*";
				code = arr[1];
				attributes = attributes + arr[2] + "*";
				iItemqty=iItemqty+parseInt(quantity);
							
			}	  
		}	
	}
	if(code == ""){
		alert(getMessage("iSelectItems"));
		return;
	}

	if ((iMaxCount != "") && (bMaxItem.toLowerCase()=="true")){
		iTotalqty = parseInt(sCartItems)
		if((iItemqty + iTotalqty)>parseInt(iMaxCount)){
			alert(getMessage("iTotalQtyExceed") + " " + iMaxCount);
			return;
		}
	}
	attribute_codes = attribute_codes + (document.frmMatrix.Attrib1.selectedIndex + 1) + "*";
	attribute_codes = attribute_codes + (document.frmMatrix.Attrib2.selectedIndex + 1) + "*";
	if(attribcode3 != "") 
		attribute_codes = attribute_codes + (document.frmMatrix.Attrib3.selectedIndex + 1);
	else
		attribute_codes = attribute_codes + "";
		
	document.frmAdditem.qty.value = qty;
	document.frmAdditem.ic.value = code;
  document.frmAdditem.type.value=type;
  document.frmAdditem.attributes.value = attributes;
  document.frmAdditem.attribute_codes.value = attribute_codes;
  document.frmAdditem.submit();
}
// End of the function multiselect(type)



//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: Function IsNumeric checks whether the entered value is a 
//	@											numeric and greater than Zero
//  @		Function Name		: IsNumeric(value)									
//	@		Input Parameters: value to be tested
//	@		Return Value		: 0 - Success	  1 - Failure			
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

function IsNumeric(value)
{
	var validate=/(^\d+$)/
	if ((validate.test(value)) && (value > 0))
		return 0; 
	else
		return 1; 
}
// End of the function IsNumeric(value)

function setFormat(field){
		var i, idiff,strval;
		var m_Field = eval("document." + field);
  	if(m_Field.value.indexOf(".") < 0)
			m_Field.value = m_Field.value + ".00";
		if(m_Field.value.length-m_Field.value.indexOf(".") == 2)
			m_Field.value = m_Field.value + "0";
		else if(m_Field.value.length-m_Field.value.indexOf(".") > 3){
			m_Field.value=m_Field.value.substring(0,m_Field.value.indexOf(".") +3)		
		}
		
	  idiff=10-m_Field.value.length	
		strval=""
		for(i=0;i< 10;i++)	{
			strval=strval+ " "  
			if (i==idiff-1){
				strval=strval+m_Field.value;
				break;
			}
		}
		m_Field.value=strval;
}

// End of the function setFormat(field)

function formatValue(value){
	return Math.round(value*100)/100;
}
// End of the function formatValue(value)


function submitlist(sBc)
{
	var value,sCatText;
	value=document.frmList.lstSubCategory.options[document.frmList.lstSubCategory.selectedIndex].value;
	sCatText=document.frmList.lstSubCategory.options[document.frmList.lstSubCategory.selectedIndex].text;
	if(value != "0")
	{
		document.frmList.action = sNonSecurePath + "items.asp?Cc=" + value + "&CatName=" + sCatText + "&Bc=" + sBc + "&BrandName=" + sBrandName + "&Tp=" + sTpCatalog + "&iTpStatus=1";
		document.frmList.submit();
	}
}
// End of the function submitlist()

function ChangeCatalog()
{
  strURL = sNonSecurePath + "powersearch.asp?Tp=" + document.frmPSearch.shop.options[document.frmPSearch.shop.selectedIndex].value;
  window.location.href = strURL;
}
// End of the function ChangeCatalog()

function preprocess(iWishList, iUnits){ 
	var retval, iItemqty, iTotalqty, qty, iIndex, iRelatedQty;
	qty=document.frmAdd.qty.value;
	retval=IsNumeric(qty);
	iRelatedQty = 0;
	if(retval == 1)	{
		alert(getMessage("iValidQuantity"));
		document.frmAdd.qty.value=iUnits;
		document.frmAdd.qty.focus();
		return;
	}
	if (iWishList == 0){
		iItemqty = qty*1
//	if (iWishList == "0"){
//		iItemqty = qty*1;
		if ((iMaxCount != "") && (bMaxItem.toLowerCase()=="true")){
			iTotalqty = parseInt(sCartItems)
			if (document.frmRelated){
				if(document.frmRelated.chkItems.length > 1){
					for (iIndex = 0; iIndex < document.frmRelated.chkItems.length; iIndex++){
						if (document.frmRelated.chkItems[iIndex].checked){
							iRelatedQty =  iRelatedQty + parseInt(document.frmRelated.chkItems[iIndex].value.split(",")[1]);
						}
					}
				}
				else{
					if (document.frmRelated.chkItems.checked){
						iRelatedQty =  parseInt(document.frmRelated.chkItems.value.split(",")[1]);
					}
				}
			}
			if((iItemqty + iTotalqty + iRelatedQty)>parseInt(iMaxCount)){
				alert(getMessage("iTotalQtyExceed") + " " + iMaxCount);
				document.frmAdd.qty.value=iUnits;
				document.frmAdd.qty.focus();
				return;
			}
		}
		for (count=0;count<document.forms.length;count++){
			if (document.forms[count].name=="frmRelated"){
				var sItemList="";
					for(index=0;index<(document.frmRelated.elements.length);index++) {
						if(document.frmRelated.elements[index].checked){
							sItemList+=document.frmRelated.elements[index].value + "," ;
						iItemqty++;
					}
				}
				sItemList = sItemList.substring(0,sItemList.length-1)
				document.frmAdd.chkItems.value = sItemList;
			}
		}
		document.frmAdd.submit();
	}
	else{
		document.frmAdd.action = sNonSecurePath + "includes/AddWishitem.asp?Tp=" + sTpCatalog
		document.frmAdd.submit();
	}
}
// End of the function preprocess()

function UpgradeBuy(itemcode,iUnits)
{
	iTotalqty = parseInt(sCartItems)
	if ((iMaxCount != "") && (bMaxItem.toLowerCase()=="true")){
		if(iTotalqty >= parseInt(iMaxCount)){
			alert(getMessage("iTotalQtyExceed") + " " + iMaxCount);
			return;
		}
	}
	document.frmUpgrade.ic.value = itemcode;
	document.frmUpgrade.qty.value = iUnits;
	document.frmUpgrade.submit();
}
// End of the function UpgradeBuy(itemcode)

function DeleteFromCart(itemcode, itemtype, tpcode, eqCode)
{
	if(confirm(getMessage("iRemoveItemFromCart")))
	{
	document.frmDelete.itemcode.value = itemcode;
	document.frmDelete.itemtype.value = itemtype;
	document.frmDelete.tpcode.value = tpcode;
	document.frmDelete.EqCode.value = eqCode;
	document.frmDelete.submit();
	}
}
// End of the function DeleteFromCart(itemcode, itemtype, tpcode)

function EmptyCart()
{
	if(confirm(getMessage("iEmptyCart")))
	{
	document.frmDelete.todo.value = "empty";
	document.frmDelete.submit();
	}
}
// End of the function EmptyCart()

function UpdateCart(Index)
{
	var count,qty,retval,iItemqty;
	iItemqty=0
	
	for(count = 1;count<=Index;count++)
	{		
		qty = eval("document.frmCartContents.qty" + count + ".value")
		var validate=/(^\d+$)/
		if (validate.test(qty) == false)
		{
			alert(getMessage("iValidQuantity"));
			eval("document.frmCartContents.qty" + count + ".focus()");
			return;
		}
		qty=qty*1
		iItemqty=iItemqty+qty
	}
	if ((iMaxCount != "") && (bMaxItem.toLowerCase()=="true")){
		if(iItemqty > parseInt(iMaxCount)){
			alert(getMessage("iTotalQtyExceed") + " " + iMaxCount);
			return;
		}
	}
	document.frmCartContents.submit();
}
// End of the function UpdateCart(Index)

function submitSearch(iPageNo){	
	document.frmPSearch.PageNo.value= iPageNo;
	document.frmPSearch.submit();
}
// End of the function submitSearch(iPageNo)


function IsFloat(value)
{
	var pattern1 = /^(\d+)(\.){0,1}(\d*)$/
	var pattern2 = /^(\d*)(\.){0,1}(\d+)$/
	return pattern1.test(value) || pattern2.test(value);
}

// End of the function IsFloat()

function RetrieveCart(getcart,todo)
{
	if(todo=="1")
		if(confirm(getMessage("iDelSavedCart")))
			document.frmRetrieveCart.action = sNonSecurePath + "retrievecart.asp?action=DEL";
		else
			return;
	else
		document.frmRetrieveCart.action = sNonSecurePath + "retrievecart.asp?action=RET";
	document.frmRetrieveCart.PrevCartID.value =  getcart;
	document.frmRetrieveCart.submit();
}
// End of the function RetrieveCart(getcart,todo)

function SaveCart(){
	if (document.frmSaveCart.CARTNAME.value == ""){
    	alert(getMessage("iEnterCartName"));
		document.frmSaveCart.CARTNAME.focus();
		return;
	}
	if (document.frmSaveCart.CARTNAME.value.charAt(0)==" "){
		alert(getMessage("iCartSpaces"));
		document.frmSaveCart.CARTNAME.focus();
		return;
	}
	if(document.frmSaveCart.CARTNAME.value.indexOf('"')!=-1)
	 {
		alert(getMessage("iInvalidCartname"));
		document.frmSaveCart.CARTNAME.focus();
		return;
	 }
	document.frmSaveCart.action = sNonSecurePath + "savecart.asp?action=SN&saved=1"
	document.frmSaveCart.submit();
}
// End of the function SaveCart()

function SaveEmail(){
	
	// ### Validating email.
	ret_val = IsEmail(document.frmSaveEmail.EMAIL.value);
	if (ret_val == 1){
		document.frmSaveEmail.EMAIL.focus();
		document.frmSaveEmail.EMAIL.select();
		return;
	}
	
	document.frmSaveEmail.action = sNonSecurePath + "EmailidReq.asp"
	document.frmSaveEmail.submit();
}
// End of the function SaveEmail()

function RestoreCart()
{
	if (document.frmSaveCart.CARTLIST.options[document.frmSaveCart.CARTLIST.selectedIndex].value == 0 )	{
		alert(getMessage("iChooseCartName"));
		document.frmSaveCart.CARTLIST.focus();
	}	
	else	{	
		document.frmSaveCart.action = sNonSecurePath + "savecart.asp?action=SE&saved=1"
		document.frmSaveCart.submit();
	}
}
// End of the function RestoreCart()


function isAlphanumeric(str,cSpace){
    var sSpacepat = /^(((\w+)[\s,\-*\.]*)+)$/
     var sPat = /^(((\w+)[*,\-\.]*)+)$/
     if (cSpace=="1"){
		if(!(sSpacepat.test(str)))
		   {
			 alert(getMessage("iValidText"));
			 return false;
	       }	
		}
	  else{
		
		if(!(sPat.test(str)))  
			{
				alert(getMessage("iValidText"));
				return false;
	        }
	        }
     return true;
}
// End of the function isAlphanumeric(str,cSpace)

function IsEmail(strEmail)
{
var intReturn =1;
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>'@,;:\\\\\\\"\\.\\[\\]'"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=strEmail.match(emailPat)
	if (matchArray==null ) {
		alert(getMessage("iValidEmail"));
		return intReturn;
	}
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null) {
    alert(getMessage("iValidEmail"));
    return intReturn;
}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert(getMessage("iValidEmail"));
			return intReturn;
			}
		}
		intReturn=0;
		return intReturn;
	}
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert(getMessage("iValidEmail"));
    return intReturn;
}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
	alert(getMessage("iValidEmail"));
	return intReturn;
	}
var len=domArr.length	
if (len<2) {
   var errStr = getMessage("iValidEmail");
   alert(errStr)
   return intReturn;
}
intReturn=0;
return intReturn;
}
//	End of the function IsEmail(strEmail)

//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: This function is called before submiting page, to validate
//	@											the min & max price.
//  @   Pages Affected  : PowerSearch
//  @		Function Name		: PowerSearchVal()									
//	@		Input Parameters: null							
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

function PowerSearchVal(sCustTypeInt, sCustTypeDate){
	var len,iIndex,bCriteria,fMinval,fMaxval,value ;
	
	var validate=/(^\d+$)/
	
	//The hidden variables are being reset to blank when user comes back to page using back button.
	document.frmPSearch.sManDesc.value  = "";
	document.frmPSearch.sBrandDesc.value = "";
	document.frmPSearch.sCatDesc.value  =  "";
	
	if(sCustTypeInt!=""){
		sCustTypeInt = sCustTypeInt.substring(0,sCustTypeInt.length-1) 
		arr = sCustTypeInt.split(",");
		for(iIndex=0;iIndex<=arr.length-1;iIndex++){
			objField=eval("document.frmPSearch." + arr[iIndex]) 
			if(objField.value!=""){
				if(!(validate.test(objField.value)))  {
						alert(getMessage("iValidInteger"));
						objField.focus();
						objField.select();
						return false;
				}
			}	
		}	
	}
	if(sCustTypeDate!=""){
		sCustTypeDate = sCustTypeDate.substring(0,sCustTypeDate.length-1) 
		arr = sCustTypeDate.split(",");
		for(iIndex=0;iIndex<=arr.length-1;iIndex++){
			smonth=eval("document.frmPSearch." + arr[iIndex]+ "month.options[document.frmPSearch." + arr[iIndex]+ "month.selectedIndex].value")
			sday=eval("document.frmPSearch." + arr[iIndex]+ "day.options[document.frmPSearch." + arr[iIndex]+ "day.selectedIndex].value")
			syear=eval("document.frmPSearch." + arr[iIndex]+ "year.options[document.frmPSearch." + arr[iIndex]+ "year.selectedIndex].value")
			var sdate=new Date(syear, smonth ,sday)
			var sDefaultDate=new Date(1995,1,1)
			if((sdate-sDefaultDate)!=0)
				eval("document.frmPSearch.Txt" + arr[iIndex] + ".value=smonth+'/'+sday+'/'+syear")
			else
				eval("document.frmPSearch.Txt" + arr[iIndex] + ".value=''")
		}
	}
	
	bCriteria=false;
	len = document.frmPSearch.elements.length;
	for(iIndex=0;iIndex<=parseInt(len)-1;iIndex++){	
		if(document.frmPSearch.elements[iIndex].type == "text"){
			if(document.frmPSearch.elements[iIndex].value != ""){
				if(document.frmPSearch.elements[iIndex].value.charAt(0)==" "){
					alert(getMessage("iSearchNoStartSpace"));
					document.frmPSearch.elements[iIndex].focus();
					return false;
				}
				bCriteria = true;
			}
		}
		
		if(document.frmPSearch.elements[iIndex].type == "hidden"){
			if(document.frmPSearch.elements[iIndex].value != "")
				bCriteria = true;
		}
		if(document.frmPSearch.elements[iIndex].type == "checkbox"){
			if(document.frmPSearch.elements[iIndex].checked)
				bCriteria = true;
		}
		sFldname=document.frmPSearch.elements[iIndex].name
		if((sFldname == "selCategory")||(sFldname == "selBrand")||(sFldname == "selManufacturer")){
			if(document.frmPSearch.elements[iIndex].options[document.frmPSearch.elements[iIndex].selectedIndex].value != "")
				bCriteria = true;
		}
		if(bCriteria) break;
	}
	
	if(! bCriteria)	{
		alert(getMessage("iEnterSearchText"));
		document.frmPSearch.txtKey.focus();
		return false;
	}
	
	fMinval=document.frmPSearch.txtMin.value
	fMaxval=document.frmPSearch.txtMax.value
	
	if(document.frmPSearch.txtMin.value.length > 0)
	{ 
		 retval=IsFloat(fMinval);
		
		if(! retval)
		{
				alert(getMessage("iValidPrice"));
				document.frmPSearch.txtMin.focus();
				document.frmPSearch.txtMin.select();
				return false;
		}
		
			fMinval=parseFloat(fMinval);
		
		
	}
	if(document.frmPSearch.txtMax.value.length > 0)
	{   
	  
		 retval=IsFloat(fMaxval);
		if(! retval)
		{
			alert(getMessage("iValidPrice"));
			document.frmPSearch.txtMax.focus();
			document.frmPSearch.txtMax.select();
			return false;
		}
		
			fMaxval=parseFloat(fMaxval);
	
		
	}
	if(fMaxval < fMinval)
	{
		alert(getMessage("iMaxPriceGTMinPrice"));
		document.frmPSearch.txtMax.focus();
		document.frmPSearch.txtMax.select();
		return false;
		
	}
	document.frmPSearch.sManDesc.value  = document.frmPSearch.selManufacturer.options[document.frmPSearch.selManufacturer.selectedIndex].text;
	document.frmPSearch.sBrandDesc.value = document.frmPSearch.selBrand.options[document.frmPSearch.selBrand.selectedIndex].text;
	document.frmPSearch.sCatDesc.value  =  document.frmPSearch.selCategory.options[document.frmPSearch.selCategory.selectedIndex].text;
	document.frmPSearch.submit();
	
}

// End of the function powersearchval()

