/*******************************
/**  CLIENT SCRIPTS FOR ESHOP **
*******************************/

function CountryState()
{
	
	
	
	var cindex = document.forms[0].CountryList.selectedIndex;
	
	if ( ( document.forms[0].CountryList.options[cindex].value != 'US' ) && ( document.forms[0].CountryList.options[cindex].value != 'CA' ) )
	{
		
		
		
		var items = document.forms[0].State.length;
		
        for (var x=0; x<items; x++)
		{
			if ( document.forms[0].State.options[x].value == "--" )
			{
				document.forms[0].State.selectedIndex = x;
				document.forms[0].State.disabled = true;
			}
		}
	}
	else
	{
		document.forms[0].State.disabled = false;
		document.forms[0].State.selectedIndex = 0;
		
	}

}


function sCountryState()
{
	
	
	
	var cindex = document.forms[0].sCountryList.selectedIndex;
	
	if ( ( document.forms[0].sCountryList.options[cindex].value != 'US' ) && ( document.forms[0].sCountryList.options[cindex].value != 'CA' ) )
	{
		
		
		
		var items = document.forms[0].sState.length;
		
        for (var x=0; x<items; x++)
		{
			if ( document.forms[0].sState.options[x].value == "--" )
			{
				document.forms[0].sState.selectedIndex = x;
				document.forms[0].sState.disabled = true;
			}
		}
	}
	else
	{
		document.forms[0].sState.disabled = false;
		document.forms[0].sState.selectedIndex = 0;
		
	}

}







		function MM_findObj(n, d) 
		{ //v3.0
			var p,i,x;  
			if(!d) d=document; 
			if((p=n.indexOf("?"))>0&&parent.frames.length) 
			{
				d=parent.frames[n.substring(p+1)].document; 
				n=n.substring(0,p);
			}
			if(!(x=d[n])&&d.all) 
				x=d.all[n]; 
				
			for (i=0;!x&&i<d.forms.length;i++) 
				x=d.forms[i][n];
			
			for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
				x=MM_findObj(n,d.layers[i].document); 
				
			return x;
		}
		
		function MM_swapImage() 
		{ //v3.0
			var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; 
			for(i=0;i<(a.length-2);i+=3)
				if ((x=MM_findObj(a[i]))!=null)
				{
					document.MM_sr[j++]=x; 
					if(!x.oSrc) 
						x.oSrc=x.src; x.src=a[i+2];
				}
		}
		
		function MM_swapImgRestore() 
		{ //v3.0
			var i,x,a=document.MM_sr; 
			for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) 
				x.src=x.oSrc;
		}

		function MM_preloadImages() 
		{ //v3.0
			var d=document; 
			if(d.images)
			{ 
				if(!d.MM_p) 
					d.MM_p=new Array();
				var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
				for(i=0; i<a.length; i++)
					if (a[i].indexOf("#")!=0)
					{ 
						d.MM_p[j]=new Image; 
						d.MM_p[j++].src=a[i];
					}
			}
		}
		
		function FocusEditor()
		{
			document.forms[0].HtmlEditor.focus();
		}
		
		
		function QuickBuy(boughtproduct)
		{
			alert(boughtproduct + ' has been added to your cart');
		}
		
		function ProdSearch()
		{
			if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
			{
				event.cancelBubble = true;
				event.returnValue = false;
				
				document.forms[0].SearchButton.click();
				
			}
			else
			{
				return true;
			}
		}
		
		function CheckCartItems(count)
		{
			if ( count == '0' )
			{
				alert('There are no items in your cart.');
				return false;
			}
			else
			{
				return true;
			}
		
		}
		
		function AddDeptClick()
		{
			if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
			{
				event.cancelBubble = true;
				event.returnValue = false;
				
				document.forms[0].AddNewDepartmentButton.click();
				
			}
			else
			{
				return true;
			}
		
		}
		
		function EnterKeySearchQuick()
		{
			if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
			{
				event.cancelBubble = true;
				event.returnValue = false;
				
				document.forms[0].QuickButton.click();
				
			}
			else
			{
				return true;
			}
		
		}
		
		
		function EnterKeySearchAdvanced()
		{
			if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
			{
				event.cancelBubble = true;
				event.returnValue = false;
				
				document.forms[0].FindButton.click();
				
			}
			else
			{
				return true;
			}
		
		}
		
		// LOG CLICK
		function PerformLoginBanner()
		{
			
			if ( (document.forms[0].Banner_email.value == '') || (document.forms[0].Banner_password.value == '') )
			{
				alert('Login requires email/pass');
			}
			else 
			{
				var linkurl = 'http://www.genesisstampco.com/SSL/Signin.aspx?Email=' + document.forms[0].Banner_email.value + '&Password=' + document.forms[0].Banner_password.value;
			
				location.href(linkurl);
			}
		}
		
		// SEARCH CLICK 
		function PerformSearchBanner()
		{
			var linkurl = 'http://www.genesisstampco.com/StoreModules/SearchResults.aspx?txtSearch=' + document.forms[0].Banner_SearchField.value + '&QuickSearch=1';
		
			location.href(linkurl);
		}
		
		// ENTER KEY SEARCH 
		function EnterKeySearchBanner(myurl)
		{
			if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
			{
				event.cancelBubble = true;
				event.returnValue = false;
				
				var linkurl = myurl + document.forms[0].Banner_SearchField.value + '&QuickSearch=1';
				
				location.href(linkurl);
				
				return false;
				
			} 
			else 
			{	
				return true;
			}
			
		}
		
	
		
		// ENTER KEY LOGIN - DYNAMIC BANNER
		function EnterKeyLoginBanner(myurl)
		{
			if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
			{
				event.cancelBubble = true;
				event.returnValue = false;
				
				if ( (document.forms[0].Banner_email.value == '') || (document.forms[0].Banner_password.value == '') )
				{
					alert('Login requires email and password');
				}
				else
				{
					var linkurl = myurl + document.forms[0].Banner_email.value + '&Password=' + document.forms[0].Banner_password.value;
					
					location.href(linkurl);
				}
				return false;
			} 
			else
			{	
				return true;
			}
			
		}
		
		// ALERT USER IF CHECKOUT IS CLICKED WITH EMPTY CART
		function checkOutError(path, cost)
		{
			if (cost != 'Shopping Cart is Empty')
			{
				window.location= path;
			}
			else 
			{
				alert('Cart is empty!');
			}
		}
		
		// COPY CONTACT DETAILS
        function copyContactDetails() 
        {
            document.CheckOut.sFirstName.value = document.CheckOut.FirstName.value;
            document.CheckOut.sLastName.value = document.CheckOut.LastName.value;
            document.CheckOut.sStreet.value = document.CheckOut.Street.value;
            document.CheckOut.sCity.value = document.CheckOut.City.value;
            document.CheckOut.sCountryList.selectedIndex = document.CheckOut.CountryList.selectedIndex;
            document.CheckOut.sState.value = document.CheckOut.State.value;
            document.CheckOut.sPostalCode.value = document.CheckOut.PostalCode.value;
            document.CheckOut.sTelephone.value = document.CheckOut.Telephone.value;
        }
        
        // COPY CONTACT DETAILS
        function copyContactDetails2() 
        {
            document.Register.sFirstName.value = document.Register.FirstName.value;
            document.Register.sLastName.value = document.Register.LastName.value;
            document.Register.sStreet.value = document.Register.Street.value;
            document.Register.sCity.value = document.Register.City.value;
            document.Register.sCountryList.selectedIndex = document.Register.CountryList.selectedIndex;
            document.Register.sState.value = document.Register.State.value;
            document.Register.sPostalCode.value = document.Register.PostalCode.value;
            document.Register.sTelephone.value = document.Register.Telephone.value;
        }
        
          // COPY CONTACT DETAILS
        function copyContactDetails3() 
        {
            document.UpdateProfile.sFirstName.value = document.UpdateProfile.FirstName.value;
            document.UpdateProfile.sLastName.value = document.UpdateProfile.LastName.value;
            document.UpdateProfile.sStreet.value = document.UpdateProfile.Street.value;
            document.UpdateProfile.sCity.value = document.UpdateProfile.City.value;
            document.UpdateProfile.sCountryList.selectedIndex = document.UpdateProfile.CountryList.selectedIndex;
            document.UpdateProfile.sState.value = document.UpdateProfile.State.value;
            document.UpdateProfile.sPostalCode.value = document.UpdateProfile.PostalCode.value;
            document.UpdateProfile.sTelephone.value = document.UpdateProfile.Telephone.value;
        }
      
		// FORMAT PHONE FOR 1st PHONENUM
		function FormatPhone1() 
		{
			document.CheckOut.Telephone.value= formatPhone(document.CheckOut.Telephone.value);
		}
		
		// FORMAT PHONE FOR 2nd PHONENUM
		function FormatPhone2() 
		{
			document.CheckOut.sTelephone.value= formatPhone(document.CheckOut.sTelephone.value);
		}
		
		// FORMAT PHONE FOR 3rd PHONENUM
		function FormatPhone3() 
		{
			document.Register.Telephone.value= formatPhone(document.Register.Telephone.value);
		}
		
		// FORMAT PHONE FOR 4th PHONENUM
		function FormatPhone4() 
		{
			document.Register.sTelephone.value= formatPhone(document.Register.sTelephone.value);
		}
		
		// FORMAT PHONE FOR 5th PHONENUM
		function FormatPhone5() 
		{
			document.UpdateProfile.Telephone.value= formatPhone(document.UpdateProfile.Telephone.value);
		}
		
		// FORMAT PHONE FOR 6th PHONENUM
		function FormatPhone6() 
		{
			document.UpdateProfile.sTelephone.value= formatPhone(document.UpdateProfile.sTelephone.value);
		}
		
		function formatPhone(num)
		{ 
			var _return=false;
			/*
				* 7181238748 to 1(718)123-8748
			*/ 
			
			num= replaceSubstring(num, "-", "");
			num= replaceSubstring(num, " ", "");
			
			if(num.length != 10)
			{ 
				/* 
				* if user did not enter 10 digit phone number then simply print whatever user entered 
				*/ 
				_return=_OUTPUT?num:false;
			} 
			else
			{ 
				/* formating phone number here */ 
				_return="(";
				var ini = num.substring(0,3);
				_return+=ini+") ";
				var st = num.substring(3,6);
				_return+=st+"-";
				var end = num.substring(6,10);
				_return+=end;
			}
			return _return; 
		} 
	
	// REPLACE A SUBSTRING WITHIN A STRING
	function replaceSubstring(inputString, fromString, toString) 
	{
		// Goes through the inputString and replaces every occurrence of fromString with toString
		var temp = inputString;
		if (fromString == "") 
		{
			return inputString;
		}
		if (toString.indexOf(fromString) == -1) 
		{ // If the string being replaced is not a part of the replacement string (normal situation)
			while (temp.indexOf(fromString) != -1) 
			{
				var toTheLeft = temp.substring(0, temp.indexOf(fromString));
				var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
				temp = toTheLeft + toString + toTheRight;
			}
		} 
		else 
		{ // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
			var midStrings = new Array("~", "`", "_", "^", "#");
			var midStringLen = 1;
			var midString = "";
			// Find a string that doesn't exist in the inputString to be used
			// as an "inbetween" string
			while (midString == "") 
			{
				for (var i=0; i < midStrings.length; i++) 
				{
					var tempMidString = "";
					for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
					if (fromString.indexOf(tempMidString) == -1) 
					{
						midString = tempMidString;
						i = midStrings.length + 1;
					}
				}
			} // Keep on going until we build an "inbetween" string that doesn't exist
			// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
			while (temp.indexOf(fromString) != -1) 
			{
				var toTheLeft = temp.substring(0, temp.indexOf(fromString));
				var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
				temp = toTheLeft + midString + toTheRight;
			}
			// Next, replace the "inbetween" string with the "toString"
			while (temp.indexOf(midString) != -1) 
			{
				var toTheLeft = temp.substring(0, temp.indexOf(midString));
				var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
				temp = toTheLeft + toString + toTheRight;
			}
		} // Ends the check to see if the string being replaced is part of the replacement string or not
		return temp; // Send the updated string back to the user
	
	} // Ends the "replaceSubstring" function


	