/*******************************
/**  CLIENT SCRIPTS FOR ESHOP **
*******************************/

		
		function SendPassClick()
		{
			if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
			{
				event.cancelBubble = true;
				event.returnValue = false;
				
				document.forms[0].ForgotButton.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 PerformLogin()
		{			
			if ( (document.forms[0].Banner_email.value == '') || (document.forms[0].Banner_password.value == '') )
			{
				alert('Login requires email/pass');
			}
			else 
			{
				//alert('Before linkurl');
				var linkurl = 'http://www.liroots.com/SSL/Signin.aspx?Email=' + document.forms[0].Banner_email.value + '&Password=' + document.forms[0].Banner_password.value;
			    //alert(linkurl);
				window.location.href = linkurl;
			}
		}
		
		function PerformSearch()
		{
			var linkurl = 'http://www.liroots.com/StoreModules/SearchResults.aspx?txtSearch=' + document.forms[0].Banner_SearchField.value;
		
			window.location.href = linkurl;
		
		}
		
		function EnterKeySearch(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;
				
				window.location.href = linkurl;
				
				return false;
				
			} 
			else 
			{	
				return true;
			}
			
		}
		
		function EnterKeyLogin(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;
					
					window.location.href = linkurl;
				}
				//return false;
			} 
			else 
			{	
				return true;
			}
			
		}
		
		
			
			
		function testEnterKey(buttonToClick)
		{
			if (event.keyCode == 13)
			{
				event.cancelBubble = true;
				event.returnValue = false;
				Form1[buttonToClick].click();
			}
			
		}
		
		
		function catchKeyPress(code, sender) 
		{
                  var ret=null;
                  
                  testForEnterAndEsc();
                  
                  if (code == '13') 
                  {
                      switch (sender.name) 
                      {
                        case 'SearchField':
                            ret=document.getElementById("searchButton");
                            break;
                        case 'email':
                            ret=document.getElementById("SigninBtn");
                            break;
                        case 'password':
                            ret=document.getElementById("SigninBtn");
                            break;
                      }
                      
                      if(ret) 
                      { 
                          ret.click(); 
                      }
                  } 
                  else if(code == '27') 
                  {
                      ret=document.getElementById("cancelButton");
                      if(ret) { ret.click(); }
                  }
         }
    
         function testForEnterAndEsc() 
         {    
	           if (event.keyCode == 13 || event.keyCode == 27) 
	           {        
		          event.cancelBubble = true;
		          event.returnValue = false;
               }
         } 
     
	
		// 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.sCompanyName.value = document.CheckOut.CompanyName.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;
            
			if ( document.CheckOut.sPostalCode.value != '' )
			{
				document.CheckOut.submit();
			}
			
        }
        
        // COPY CONTACT DETAILS
        function copyContactDetails2() 
        {
            document.Register.sFirstName.value = document.Register.FirstName.value;
            document.Register.sLastName.value = document.Register.LastName.value;
            document.Register.sCompanyName.value = document.Register.CompanyName.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.sCompanyName.value = document.UpdateProfile.CompanyName.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 4th PHONENUM
		function FormatPhone5() 
		{
			document.UpdateProfile.Telephone.value= formatPhone(document.UpdateProfile.Telephone.value);
		}
		
		// FORMAT PHONE FOR 4th 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


	