// Browser Detection Javascript
// copyright 1 February 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function whichBrs() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'Internet Explorer';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';} else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function isUrl(s)
 {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

function Left(str, n)
{
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function SetCookie(cookieName,cookieValue,nDays)
{
 if(nDays != 0)
 {
  var today = new Date();
  var expire = new Date();
  if (nDays==null || nDays==0) nDays=1;
  expire.setTime(today.getTime() + 3600000*24*nDays);
  document.cookie = cookieName+"="+(cookieValue) + ";expires="+expire.toGMTString();
 }
 else
 {
  document.cookie = cookieName+"="+(cookieValue);
 }

}

function checkEnter(evt)
{
    var charCode = (window.event)?event.keyCode:evt.which;   
    if (charCode == 13)
    {
     document.onKeyPress = "";
     dosave();
    }
}

function checkEnterCreate(evt)
{
    var charCode = (window.event)?event.keyCode:evt.which;   
    if (charCode == 13)
    {
     document.onKeyPress = "";
     dosavecreate();
    }
}

function checkEnterKey(evt)
{
    var charCode = (window.event)?event.keyCode:evt.which;   
    if (charCode == 13)
    {
     document.onKeyPress = "";
     doSearchKey();
    }
}

function checkEnterNum(evt)
{
    var charCode = (window.event)?event.keyCode:evt.which;   
    if (charCode == 13)
    {
     document.onKeyPress = "";
     doSearchNum();
    }
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}
function notChecked( box )
{
    if( box.checked ){
        return false;
    }
    else{
        return true;
    }
} 

function isValidURL(url) {
	var urlRegxp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	if (urlRegxp.test(url) != true) {
		return false;
	} else {
		return true;
	}
}

function ckEnter(evt)
{ 	
    var charCode = (window.event)?event.keyCode:evt.which;   
    if (charCode == 13)
    {
     document.onKeyPress = "";
    }
}

function checkemail(EmailValue)
{
 var str=EmailValue;
 var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
 if (filter.test(str))
 {
  testresults=true;
 }
 else
 {
  testresults=false;
 }
 return (testresults);
}

function NumericCheck(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 NumericCheckPhone(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 NumericCheckCSI(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 NumericCheckInt(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   var i;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function isValidCreditCard(type, ccnum)
{
   if (type == 1)
   {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   }
   else if (type == 2)
   {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   }
   else if (type == 3)
   {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   }
   else if (type == 4)
   {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   }
   //else if (type == "Diners")
   //{
   //   // Diners: length 14, prefix 30, 36, or 38.
   //   var re = /^3[0,6,8]\d{12}$/;
   //}
   
   if (!re.test(ccnum)) return false;
   // Remove all dashes for the checksum checks to eliminate negative numbers
   ccnum = ccnum.split("-").join("");
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}

function dosavecreateCheck()
{
 errorstring = "";
 temp1 = trim(document.EForm.txtFName.value);
 temp2 = trim(document.EForm.txtLName.value);
 temp3 = trim(document.EForm.txtEmail.value);
 temp4 = trim(document.EForm.txtPassW.value);
 temp5 = trim(document.EForm.txtPassW2.value);
 if(temp1 == "" || temp2 == "" || temp3 == "" || temp4 == "" || temp5 == "")
 {
  errorstring = "All Fields are required except for address (2).\n\r";
 }
 else
 {
  if(checkemail(temp3) == false)
  {
   errorstring = "Error: Invalid email address.\n\r";
  }
  else
  {
   if(temp4 != temp5)
   {
    errorstring = "Error: Passwords do match.\n\r";
    document.EForm.txtPassW.value = "";
    document.EForm.txtPassW2.value = "";
   }
   else
   {
    if(temp4.length < 6)
    {
     errorstring = "Error: Password must be at least 6 characters in length.\n\r";
    }
    else
    {
     i = document.EForm.selStateS.selectedIndex;
     temp1 = trim(document.EForm.txtFNameS.value);
     temp2 = trim(document.EForm.txtLNameS.value);
     temp3 = trim(document.EForm.txtAddress1S.value);
     temp4 = trim(document.EForm.txtCityS.value);
     temp5 = trim(document.EForm.txtZipS.value);
     if(temp1 == "" || temp2 == "" || temp3 == "" || temp4 == "" || temp5 == "" || i == 0)
     {
       errorstring = "All Shipping Fields are required except for address (2).\n\r";
     }
     if(errorstring == "")
     {
      i = document.EForm.selStateB.selectedIndex;
      temp1 = trim(document.EForm.txtFNameB.value);
      temp2 = trim(document.EForm.txtLNameB.value);
      temp3 = trim(document.EForm.txtAddress1B.value);
      temp4 = trim(document.EForm.txtCityB.value);
      temp5 = trim(document.EForm.txtZipB.value);
      if(temp1 == "" || temp2 == "" || temp3 == "" || temp4 == "" || temp5 == "" || i == 0)
      {
        errorstring = "All Billing Fields are required except for address (2).\n\r";
      }
      if(errorstring == "")
      {
        temp0 = trim(document.EForm.txtLName.value);
        temp1 = trim(document.EForm.txtLNameS.value);
        temp2 = trim(document.EForm.txtLNameB.value);
        if(temp0.length <= 2 || temp1.length  <= 2 || temp2.length <= 2)
        {
          errorstring = "Error: Last Name must be at least 2 characters in length.\n\r";
        }    
        else
        {
          temp1 = trim(document.EForm.txtAddress1S.value);
          temp2 = trim(document.EForm.txtAddress1B.value);
          if(temp1.length <= 2 || temp2.length <= 2)
          {
            errorstring = "Error: Address(1) must be at least 2 characters in length.\n\r";
          }    
          else
          {
            temp1 = trim(document.EForm.txtCityB.value);
            temp2 = trim(document.EForm.txtCityS.value);
            if(temp1.length <= 2 || temp2.length <= 2)
            {
              errorstring = "Error: City must be at least 2 characters in length.\n\r";
            }    
            else
            {
              temp1 = trim(document.EForm.txtZipS.value);
              temp2 = trim(document.EForm.txtZipB.value);
              if(temp1.length  < 5 || temp2.length  < 5 || NumericCheck(temp1) == false  || NumericCheck(temp2) == false)
              {
                errorstring = "Error: Invalid Zipcode.\n\r";
              }    
            }
          }
        }
      }    
     }    
    }
   }
  }
 }
 return errorstring;
}


function doSameCreate()
{
 if(document.EForm.optSame.checked)
 {
   document.EForm.txtFNameB.value = document.EForm.txtFNameS.value;
   document.EForm.txtLNameB.value = document.EForm.txtLNameS.value;
   document.EForm.txtAddress1B.value = document.EForm.txtAddress1S.value;
   document.EForm.txtAddress2B.value = document.EForm.txtAddress2S.value;
   document.EForm.txtCityB.value = document.EForm.txtCityS.value;
   document.EForm.txtZipB.value = document.EForm.txtZipS.value;
   document.EForm.selStateB.selectedIndex = document.EForm.selStateS.selectedIndex;
  }
}

function doCheckCustomer(CustomerID,BrowserType,CartID,ShoppingCartCount,VerifyInfo,CartID)
{
 if (document.cookie && document.cookie != "")
 {
  if(BrowserType == "E")
  {
    if(navigator.userAgent.indexOf("Firefox") != -1)
    {
    	BrowserType = "F";
    }
    else if(navigator.userAgent.indexOf("MSIE") != -1)
    {
    	BrowserType = "I";
    }
    else
    {
     	BrowserType = "O";
    }
  }
  if (CustomerID == "00" || CartID == "00" || ShoppingCartCount == "00")
  {
    CustomerID = readCookie('CustomerID');
    CartID = readCookie('CartID');
    ShoppingCartCount = readCookie('ShoppingCartCount');
    if (CartID == null)
    {
    	CartID = "0";
    }
    if (CustomerID == null)
    {
    	CustomerID = "0";
    }
    if (ShoppingCartCount == null)
    {
    	ShoppingCartCount = "0";
    }
    if(CartID == 0 && ShoppingCartCount != 0)
    {
    	ShoppingCartCount = 0;
    }
    URLValue = location.href;
    if (URLValue.indexOf("?") == -1)
    {
     if (URLValue.indexOf(".asp") == -1)
     {
      location.href = URLValue + "index.asp?u1=" + CustomerID + "&p1=" + CartID + "&s1=" + ShoppingCartCount + "&bt=" + BrowserType;
     }
     else
     {
      location.href = URLValue + "?u1=" + CustomerID + "&p1=" + CartID + "&s1=" + ShoppingCartCount + "&bt=" + BrowserType;
     }
    }
    else
    {
     location.href = URLValue + "&u1=" + CustomerID + "&p1=" + CartID + "&s1=" + ShoppingCartCount + "&bt=" + BrowserType;
    }
  }
  else
  {
  	if(VerifyInfo == "T")
  	{
      if (CartID == "0")
      {
        eraseCookie("CartID");
        eraseCookie("ShoppingCartCount");
      }
    }
  }
 }
 else
 {
   	  alert("Your browser does not have cookies enabled. You must enable your cookies before proceeding.");
    	CustomerID = "0";
    	CartID = "0";
    	ShoppingCartCount = "0";
    	if(BrowserType == "E")
    	{
    	  if(navigator.userAgent.indexOf("Firefox") != -1)
    	  {
    	    BrowserType = "F";
    	  }
    	  else if(navigator.userAgent.indexOf("MSIE") != -1)
    	  {
    	    BrowserType = "I";
    	  }
    	  else
    	  {
    	    BrowserType = "O";
    	  }
    	}
      URLValue = location.href;
      if (URLValue.indexOf("?") == -1)
      {
       if (URLValue.indexOf(".asp") == -1)
       {
        location.href = URLValue + "index.asp?u1=" + CustomerID + "&p1=" + CartID + "&s1=" + ShoppingCartCount + "&bt=" + BrowserType;
       }
       else
       {
        location.href = URLValue + "?u1=" + CustomerID + "&p1=" + CartID + "&s1=" + ShoppingCartCount + "&bt=" + BrowserType;
       }
      }
      else
      {
       location.href = URLValue + "&u1=" + CustomerID + "&p1=" + CartID + "&s1=" + ShoppingCartCount + "&bt=" + BrowserType;
      }
 }
}

function doCheckout(CustomerID)
{
	 if(CustomerID == 0)
	 {
	  	location.href = "https://www.elmidae.com/login.asp";
	 }
	 else
	 {
	 	location.href = "https://www.elmidae.com/shoppingbag_enter.asp";
	 }
}

