function OpenPicLink(cUrl)
{
	AjaxGetData('',cUrl,'');
}

function ResetPassword(cType,cAccount,cMemberId)
{
	var cNewPassword=prompt("Please enter your new password","new password");
	if (cNewPassword!=null && cNewPassword!="")
  	{
  		var cConfirmPassword=prompt("Please re-enter your new password","confirm password");
		if (cConfirmPassword!=null && cConfirmPassword!="")
  		{
			if (cNewPassword == cConfirmPassword)
			{
				AjaxGetData('ResetPassword','','Type='+cType+'&Account='+cAccount+'&MemberId='+cMemberId+'&NewPassword='+cNewPassword);
			}
			else
			{
				alert('Password do not match. Please try again.');
			}
		}
		else
		{
			alert('Invalid entry');
		}
  	}
	else
	{
		alert('Invalid entry');
	}
}

function checkShippingInfo(lgo)
{
	if(document.getElementById('country').options[document.getElementById('country').selectedIndex].value != '')
	{
		document.getElementById('error_cnty_msg').style.display='none';
		if(document.getElementById('region_id').options[document.getElementById('region_id').selectedIndex].value != '')
		{
			document.getElementById('error_st_msg').style.display='none';
			if(document.getElementById('postcode').value != '')
			{
				document.getElementById('error_zip_msg').style.display='none';
				return true;
			}
			else
			{
				document.getElementById('error_zip_msg').style.display='';
			}
		}
		else
		{
			document.getElementById('error_st_msg').style.display='';
		}
	}
	else
	{
		document.getElementById('error_cnty_msg').style.display='';
	}
	return false;
}

function setState(cCountry)
{
	document.getElementById('region_id').disabled=false;
	document.getElementById('region_id').options.length=0;
	if(cCountry == 'US')
	{
		document.getElementById('region_id').options[0]=new Option('Please select the state','',true,false)
		document.getElementById('region_id').options[1]=new Option('Alabama','AL',false,false)
		document.getElementById('region_id').options[2]=new Option('Alaska','AK',false,false)
		document.getElementById('region_id').options[3]=new Option('Arizona','AZ',false,false)
		document.getElementById('region_id').options[4]=new Option('Arkansas','AR',false,false)
		document.getElementById('region_id').options[5]=new Option('California','CA',false,false)
		document.getElementById('region_id').options[6]=new Option('Colorado','CO',false,false)
		document.getElementById('region_id').options[7]=new Option('Connecticut','CT',false,false)
		document.getElementById('region_id').options[8]=new Option('Delaware','DE',false,false)
		document.getElementById('region_id').options[9]=new Option('District of Columbia','DC',false,false)
		document.getElementById('region_id').options[10]=new Option('Florida','FL',false,false)
		document.getElementById('region_id').options[11]=new Option('Georgia','GA',false,false)
		document.getElementById('region_id').options[12]=new Option('Hawaii','HI',false,false)
		document.getElementById('region_id').options[13]=new Option('Idaho','ID',false,false)
		document.getElementById('region_id').options[14]=new Option('Illinois','IL',false,false)
		document.getElementById('region_id').options[15]=new Option('Indiana','IN',false,false)
		document.getElementById('region_id').options[16]=new Option('Iowa','IA',false,false)
		document.getElementById('region_id').options[17]=new Option('Kansas','KS',false,false)
		document.getElementById('region_id').options[18]=new Option('Kentucky','KY',false,false)
		document.getElementById('region_id').options[19]=new Option('Louisiana','LA',false,false)
		document.getElementById('region_id').options[20]=new Option('Maine','ME',false,false)
		document.getElementById('region_id').options[21]=new Option('Maryland','MD',false,false)
		document.getElementById('region_id').options[22]=new Option('Massachusetts','MA',false,false)
		document.getElementById('region_id').options[23]=new Option('Michigan','MI',false,false)
		document.getElementById('region_id').options[24]=new Option('Minnesota','MN',false,false)
		document.getElementById('region_id').options[25]=new Option('Mississippi','MS',false,false)
		document.getElementById('region_id').options[26]=new Option('Missouri','MO',false,false)
		document.getElementById('region_id').options[27]=new Option('Montana','MI',false,false)
		document.getElementById('region_id').options[28]=new Option('Nebraska','NE',false,false)
		document.getElementById('region_id').options[29]=new Option('Nevada','NV',false,false)
		document.getElementById('region_id').options[30]=new Option('New Hampshire','NH',false,false)
		document.getElementById('region_id').options[31]=new Option('New Jersey','NJ',false,false)
		document.getElementById('region_id').options[32]=new Option('New Mexico','NM',false,false)
		document.getElementById('region_id').options[33]=new Option('New York','NY',false,false)
		document.getElementById('region_id').options[34]=new Option('North Carolina','NC',false,false)
		document.getElementById('region_id').options[35]=new Option('North Dakota','ND',false,false)
		document.getElementById('region_id').options[36]=new Option('Ohio','OH',false,false)
		document.getElementById('region_id').options[37]=new Option('Oklahoma','OK',false,false)
		document.getElementById('region_id').options[38]=new Option('Oregon','OR',false,false)
		document.getElementById('region_id').options[39]=new Option('Pennsylvania','PA',false,false)
		document.getElementById('region_id').options[40]=new Option('Puerto Rico','PR',false,false)
		document.getElementById('region_id').options[41]=new Option('Rhode Island','RI',false,false)
		document.getElementById('region_id').options[42]=new Option('South Carolina','SC',false,false)
		document.getElementById('region_id').options[43]=new Option('South Dakota','SD',false,false)
		document.getElementById('region_id').options[44]=new Option('Tennessee','TN',false,false)
		document.getElementById('region_id').options[45]=new Option('Texas','TX',false,false)
		document.getElementById('region_id').options[46]=new Option('Utah','UT',false,false)
		document.getElementById('region_id').options[47]=new Option('Vermont','VT',false,false)
		document.getElementById('region_id').options[48]=new Option('Virgin Islands','VI',false,false)
		document.getElementById('region_id').options[49]=new Option('Virginia','VA',false,false)
		document.getElementById('region_id').options[50]=new Option('Washington','WA',false,false)
		document.getElementById('region_id').options[51]=new Option('West Virginia','WV',false,false)
		document.getElementById('region_id').options[52]=new Option('Wisconsin','WI',false,false)
		document.getElementById('region_id').options[53]=new Option('Wyoming','WY',false,false)
	}
	else
	{
		document.getElementById('region_id').options[0]=new Option('Please select the province','',true,false)
		document.getElementById('region_id').options[1]=new Option('Ontario','Ontario',false,false)
		document.getElementById('region_id').options[2]=new Option('Quebec','Quebec',false,false)
		document.getElementById('region_id').options[3]=new Option('Nova Scotia','Scotia',false,false)
		document.getElementById('region_id').options[4]=new Option('New Brunswick','Brunswick',false,false)
		document.getElementById('region_id').options[5]=new Option('Manitoba','Manitoba',false,false)
		document.getElementById('region_id').options[6]=new Option('British Columbia','Columbia',false,false)
		document.getElementById('region_id').options[7]=new Option('Price Edward Island','Edward',false,false)
		document.getElementById('region_id').options[8]=new Option('Saskatchewan','Saskatchewan',false,false)
		document.getElementById('region_id').options[9]=new Option('Alberta','Alberta',false,false)
		document.getElementById('region_id').options[10]=new Option('Newfoundland','Newfoundland',false,false)
	}	
}

function deleteFile(cId)
{
	var elem = document.getElementById(cId);
	elem.style.display='none';
}

function addFile()
{
	for(var i=0;i<10;i++)
	{
		var elem = document.getElementById('file'+i);
		if(elem.style.display == 'none')
		{
			elem.style.display='';
			break;
		}
	}
}

function ShowItem(cId)
{
	var elem=document.getElementById(cId)
	elem.style.display='';
}

function HideItem(cId)
{
	var elem=document.getElementById(cId)
	elem.style.display='none';
}

function AdjustProductPrice(cForm)
{
	//alert( "i='" + i + "', n='" + elem[i].name + "', nt='" + elem[i].type + "', nv='" + elem[i].nodeValue + "'" );
	var elem = document.getElementById('Parent_'+cForm).getElementsByTagName('*');
	if (elem.length > 0)
	{
		for(var i = 0; i < elem.length; i++)
		{
			if (elem[i].type == 'radio')
			{
				if (elem[i].checked==1)
				{
					var newItem=elem[i].value
				}
			}
		}
		document.getElementById('Product_Price_Value').value = document.getElementById('Product_Price_Value').value - (document.getElementById('Main_'+cForm).value - document.getElementById('Sub_'+newItem).value);
		document.getElementById('Product_Price_Show').innerHTML = "$"+formatCurrency(document.getElementById('Product_Price_Value').value,2);
		document.getElementById('Main_'+cForm).value = document.getElementById('Sub_'+newItem).value;
		for(var i = 0; i < elem.length; i++)
		{
			if (elem[i].type == 'radio')
			{
				chgName = elem[i].value
				chgPrice = (document.getElementById('Main_'+cForm).value-document.getElementById('Sub_'+chgName).value)*(-1)
				if (chgPrice > 0)
				{
					document.getElementById('Price_'+chgName).innerHTML="[add $"+formatCurrency(chgPrice,2)+"]"
				}
				else if (chgPrice < 0)
				{
					document.getElementById('Price_'+chgName).innerHTML="[subtract $"+formatCurrency(chgPrice*(-1),2)+"]"
				}
				else
				{
					document.getElementById('Price_'+chgName).innerHTML=""//formatCurrency(chgPrice,2)
				}
			}
		}
	}
}

function addCompareItem(nItem,cPage)
{
	cCompare=getCookie('CompareItems');
	if (cCompare!=null && cCompare!="")
  	{
		cCompare = cCompare + "," + nItem;
		setCookie('CompareItems',cCompare,30);
  	}
	else
	{
		setCookie('CompareItems',nItem,30);
	}
	AjaxGetData('',cPage,'');
}

function removeCompareItem(nItem,cPage)
{
	cCompare=getCookie('CompareItems');
	if (cCompare!=null && cCompare!="")
  	{
		cCompare = "," + cCompare + ",";
		cCompare = cCompare.split("," + nItem + ",").join(",");
		cCompare = cCompare.substring(1, cCompare.length-1);
		setCookie('CompareItems',cCompare,30);
	}
	AjaxGetData('',cPage,'');
}

function removeCompare(cPage)
{
	deleteCookie('CompareItems');
	AjaxGetData('',cPage,'');
}
																
function CheckOptionsSelected(cFormId,cMssgFieldId)
{
	var variables = '';
	var radioName = '';
	lSelected=true;
	var elem = document.getElementById(cFormId).elements;
	if (elem.length > 0)
	{
		for(var i = 0; i < elem.length; i++)
		{
			if (elem[i].type == 'radio')
			{
				if(radioName != elem[i].name)
				{
					if(lSelected==false)
					{
						document.getElementById(cMssgFieldId).style.display='';
						return false;
					}
					lSelected=false;
					radioName=elem[i].name;
				}
				if (elem[i].checked==1)
				{
					lSelected=true;
				}
			}
		}
	}
	if(lSelected==false)
	{
		document.getElementById(cMssgFieldId).style.display='';
		return false;
	}
	else
	{
		document.getElementById(cMssgFieldId).style.display='none';
		return true;
	}
}

function checkRequestedInventory(lOverOrder,nAvailable,cQtyFieldId,cMssgFieldId)
{
	if(document.getElementById(cQtyFieldId).value < 1)
	{
		document.getElementById(cQtyFieldId).value=1;
	}
	if(lOverOrder == 'False')
	{
		if(document.getElementById(cQtyFieldId).value > nAvailable)
		{
			document.getElementById(cMssgFieldId).style.display='';
			document.getElementById(cQtyFieldId).value=nAvailable;
		}
		else
		{
			document.getElementById(cMssgFieldId).style.display='none';
		}
	}
	else
	{
		document.getElementById(cMssgFieldId).style.display='none';
	}
}

function formatCurrency(num, length)
{
	num = String(num);
	if(num.indexOf('.') == -1)
	{
		num = num + ".00";
	}
	else
	{
		if (num > 8191 && num < 10485)
		{
			num = num - 5000;
			num = Math.round(num*Math.pow(10,length))/Math.pow(10,length);
			num = num+5000;
		} 
		else 
		{
			num = Math.round(num*Math.pow(10,length))/Math.pow(10,length);
		}
	}
	return num;
}

function CreditCardType(cCardNo)
{
	//   *CARD TYPES            *PREFIX           *WIDTH
	//   American Express       34, 37            15
	//	 Diners Club            300 to 305, 36    14
	//	 Carte Blanche          38                14
	//	 Discover               6011              16
	//	 EnRoute                2014, 2149        15
	//	 JCB                    3                 16
	//	 JCB                    2131, 1800        15
	//	 Master Card            51 to 55          16
	//	 Visa                   4                 13, 16
    
	//Just in case nothing is found
	cType = 'Unknown';

	//Remove all spaces and dashes from the passed string
	cCardNo.replace(" ", "");
	cCardNo.replace("-", "");

	//Check that the minimum length of the string isn't less than fourteen characters and -is- numeric
	if ((cCardNo.length < 14) || !(IsNumeric(cCardNo))) { return cType; }

	//Check the first two digits first
	if ((Left(cCardNo,2) == 34) || (Left(cCardNo,2) == 37))
	{
		cType = 'American Express';
	}
	else if (Left(cCardNo,2) == 36)
	{
		cType = 'Diners Club';
	}
	else if (Left(cCardNo,2) == 38)
	{
		cType = 'Carte Blanche';
	}
	else if ((Left(cCardNo,2) >= 51) && (Left(cCardNo,2) <= 55))
	{
		cType = 'Master Card';
	}
	else
	{
		//None of the above - so check the first four digits collectively
		if ((Left(cCardNo,4) == 2014) || (Left(cCardNo,4) == 2149))
		{
			cType = 'EnRoute';
		}
		else if ((Left(cCardNo,4) == 2131) || (Left(cCardNo,4) == 1800))
		{
			cType = 'JCB';
		}
		else if (Left(cCardNo,4) == 6011)
		{
			cType = 'Discover';
		}
		else
		{
			//None of the above - so check the first three digits collectively
			if ((Left(cCardNo,3) == 300) || (Left(cCardNo,3) == 305))
			{
				cType = 'American Diners Club';
			}
			else
			{
				//None of the above so simply check the first digit
				if (Left(cCardNo,1) == 3)
				{
					cType = 'JCB';
				}
				else if (Left(cCardNo,1) == 4)
				{
					cType = 'Visa';
				}
			}
		}
    }
	return cType;
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1)
			{
				c_end=document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
							
function setCookie(cName,cValue,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=cName+ "=" +escape(cValue)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	return true;
}

function deleteCookie(cName)
{
	cCookie=getCookie(cName);
	if (cCookie!=null && cCookie!="")
  	{
		setCookie(cName,'',0);
		return true;
	}
	return false;
}

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;
}

function InStr(strSearch, charSearchFor)
{
	for (i=0; i < Len(strSearch); i++)
	{
		if (charSearchFor == Mid(strSearch, i, 1))
		{
			return i;
		}
	}
	return -1;
}

function Left(str, n)
{
	if (n <= 0)     // Invalid bound, return blank string
		return "";
	else if (n > String(str).length)   // Invalid bound, return
		return str;                // entire string
	else // Valid bound, return appropriate substring
		return String(str).substring(0,n);
}

function Right(str, n)
{
	if (n <= 0)     // Invalid bound, return blank string
		return "";
	else if (n > String(str).length)   // Invalid bound, return
		return str;                     // entire string
	else { // Valid bound, return appropriate substring
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}

function Mid(str, start, len)
{
	// Make sure start and len are within proper bounds
	if (start < 0 || len < 0) return "";
	var iEnd, iLen = String(str).length;
	if (start + len > iLen)
		iEnd = iLen;
	else
		iEnd = start + len;
	return String(str).substring(start,iEnd);
}

function GetFormValues(cForm)
{
	var variables = '';
	var elem = document.getElementById(cForm).elements;
	if (elem.length > 0)
	{
		for(var i = 0; i < elem.length; i++)
		{
			if (elem[i].type=='text' || elem[i].type=="textarea" || elem[i].type=="password" || elem[i].type=="hidden")
			{
				if (elem[i].value != '')
				{
					variables += elem[i].name + "=" + elem[i].value + "&";
				}
			}
			if (elem[i].type == 'checkbox')
			{
				if (elem[i].checked==1)
				{
					variables += elem[i].name + "=" + elem[i].value + "&";
				}
				else
				{
					variables += elem[i].name + "=&";
				}
			}
			if (elem[i].type == 'radio')
			{
				if (elem[i].checked==1)
				{
					variables += elem[i].name + "=" + elem[i].value + "&";
				}
			}
			if (elem[i].type == 'select-one')
			{
				variables += elem[i].name + "=" + elem[i].options[elem[i].selectedIndex].value + "&";
			}
		}
	}
	variables += "go=true";
	return variables;
}
