//<SCRIPT> // <- dummy script tag used to enable InterDev syntax coloring
//------------------------------------------------------------------------
//  sb_lib.js
//
//  Copyright© 2008, i-MARK Inc. All Rights Reserved
//  
//  WARNING: The information in this file is protected by copyright law
//  and international treaty provisions. Unauthorized reproduction or
//  distribution of this file, or any portion of it, may result in severe
//  criminal and civil penalties, and will be prosecuted to the maximum
//  extent possible under the law.  Further, you may not reverse engineer,
//  decompile, or disassemble the file.
//------------------------------------------------------------------------

// Global
var KEY_ENTER = 13;

//------------------------------------------------------------------------
// Utility methods
//------------------------------------------------------------------------
function CacheImages(arr_images)
{
  var images = new Object();
  var len = arr_images.length;
  for(var i = 0; i < len; i++)
  {
    images["img" + i] = new Image();
    images["img" + i].src = arr_images[i];
  }
}

function imkXMLEscape(str)
{
  str = str.replace(/&/g, "&amp;");
  str = str.replace(/\"/g, "&quot;");
  str = str.replace(/'/g, "&apos;");
  str = str.replace(/</g, "&lt;");
  str = str.replace(/>/g, "&gt;");
  return str;
}

//------------------------------------------------------------------------
// Cart Methods
//------------------------------------------------------------------------
function AddItem()
{
  if(document.frmShopCart.txtItemID.value == "")
  {
    alert("Please enter a Part Number.");
    document.frmShopCart.txtItemID.focus();
    return;
  }
  var qty = imkToIntPosNonZero(document.frmShopCart.txtQty.value, "\nItem " + document.frmShopCart.txtItemID.value + " Quantity");

  if(isNaN(qty))
  {
    document.frmShopCart.txtItemID.focus();
    return;
  }
  ModCart.AddItem(document.frmShopCart.txtItemID.value, qty, document.frmShopCart.txtDesc.value);
  ModCart.Send();
}

function Delete(str_cartIndex)
{
  var flag=""
	//Special case for ATE, ATH, ATM family		
	var theForm = document.frmShopCart;
	var famID = theForm.famID.value;
	if (famID == "40002" || famID == "40003" || famID == "40004")
	{		
		var partNo = theForm["itemID" + str_cartIndex].value;
		if (partNo.indexOf("ATH") == 0 || partNo.indexOf("ATE") == 0 || partNo.indexOf("ATM") == 0)
		{
			var strMsg = "Please be advised that Warner Linear guideway rails and blocks\nare NOT interchangeable with products from other companies.\nExample: A Warner rail will NOT work with a block manufactured by a competitor.";
			if (!confirm(strMsg)) return;
		}
	}
 
  ModCart.DelItem(str_cartIndex);
  ModCart.Send(flag);
}

function Update(flag)
{
  var qty;
  var itemID;
  
  // we don't know if any item quantites have changed, so
  // iterate over the contents and update the quantity for each

  for(var i = 0; i < RowCount; i ++)
  {
    qty = document.frmShopCart["qty" + (i + 1)].value;
    itemID = document.frmShopCart["itemID" + (i + 1)].value;
    
    qty = imkToIntPosNonZero(qty, "\nItem " + itemID + " Quantity");
    if(isNaN(qty))
    {
      document.frmShopCart["qty" + (i + 1)].focus();
      ModCart.Reset();
      return;
    }
    ModCart.UpdateQty(i + 1, qty);
  }
  // add any items in the "add item" line
  if(document.frmShopCart.txtQty)
  {
    qty    = document.frmShopCart.txtQty.value;
    itemID = document.frmShopCart.txtItemID.value;
    if(qty != "" && itemID != "")
    {
      qty = imkToIntPosNonZero(document.frmShopCart.txtQty.value, "\nItem " + document.frmShopCart.txtItemID.value + " Quantity");
      if(isNaN(qty))
      {
        document.frmShopCart.txtItemID.focus();
        ModCart.Reset();
        return;
      }
      ModCart.AddItem(itemID, qty, document.frmShopCart.txtDesc.value);
    }
  }
  ModCart.Send(flag);
}

//------------------------------------------------------------------------
// CModCart
//
// Utility object to build and send the ModCart XML message.
//------------------------------------------------------------------------
function CModCart()
{
  this.AddItem        = CModCart_AddItem;
  this.DelItem        = CModCart_DelItem;
  this.UpdateQty      = CModCart_UpdateQty;
  this.Send           = CModCart_Send;
  this.Reset          = CModCart_Reset;
  
  this._getCartItemID = CModCart_getCartItemID
  this._str_modCartRequest = "";
}

function CModCart_AddItem(str_itemID, qty, str_desc)
{
  this._str_modCartRequest += "<AddItm id=\"" + imkXMLEscape(str_itemID) + "\"" + 
                              " assy=\"0\" qty=\"" + qty + "\"" + 
                              " dsc=\"" + (typeof(str_desc) != "undefined" ? imkXMLEscape(str_desc) : "") + "\"/>"
}

function CModCart_DelItem(str_cartIndex)
{
  var str_cartItemID = this._getCartItemID(str_cartIndex);
  if(typeof(str_cartItemID) != "undefined" && str_cartItemID != "")
    this._str_modCartRequest += "<DelItm cid=\"" + str_cartItemID + "\"/>"
}

function CModCart_UpdateQty(str_cartIndex, qty)
{
  var str_cartItemID = this._getCartItemID(str_cartIndex);
  if(typeof(str_cartItemID) != "undefined" && str_cartItemID != "")
    this._str_modCartRequest += "<UpdQty cid=\"" + str_cartItemID + "\"" +
                                " qty=\"" + qty + "\"/>"
}

function CModCart_Send(flag)
{
  var str_xml = "<ModCart>" + this._str_modCartRequest + "</ModCart>";
  document.frmShopCart.action="sb_modcart.asp"
  document.frmShopCart.flag.value=flag
  document.frmShopCart.modcart.value=str_xml
  document.frmShopCart.submit()
}

function CModCart_getCartItemID(str_cartIndex)
{
  if(document.frmShopCart["cartItemID" + str_cartIndex])
    return document.frmShopCart["cartItemID" + str_cartIndex].value
}

function CModCart_Reset()
{
  this._str_modCartRequest = "";
}

// global function used on menu


function SaveCart()
{


}

//***********************************

function GoBack(str_where)
{
  if (str_where == "catalog")
  {
    var frmMenu = sc_FindFrame("sc_header");
    var str_home = "../sc_app/sc_tocframe.asp";
    var str_dest;
  
    str_dest = str_home;
    if(frmMenu)
    {
      if(frmMenu.MainLoc != "")
        str_dest = frmMenu.MainLoc;
        
      frmMenu.MainLoc = str_home
    }
  
    sc_OpenLocation(str_dest, "sc_main");
  }
  else
  {
    history.go(-1);
  }
} 

// registration functions

function SubmitForm()
{

  // Generic validation
  if (!theForm.ValidateForm())
    return;
  
  // specific validation
  if (CountriesWithPostalCodes[theForm['country'].options[theForm['country'].selectedIndex].text])
  {
    if (IsEmpty(theForm['PostalCode'].value))
    {
      alert("A Postal Code is required for Canadian and US addresses")
      theForm['PostalCode'].focus();
      return;
    }
  }
  
  if (CountriesWithProvinces[theForm['country'].options[theForm['country'].selectedIndex].text])
  {
    if (theForm['state'].selecedIndex == -1)
    {
      alert("A State or Province is required for Canadian and US addresses.");
      theForm['state'].focus();
      return;
    }
  }

  if (!IsEmailValid(theForm['Email'].value))
  {
    alert("Check your e-mail address, it is not a valid e-mail format.");
    theForm['Email'].focus();
    return;
  }

  if (IsNewUser)
  {
    if (!IsValidUsername(theForm['UserName'].value))
    {
      alert("Please enter a valid user name.");
      theForm['UserName'].focus();
      return;
    }
    
    if (!IsValidPasswordCombo(theForm['Password1'].value, theForm['Password2'].value))
    {
      alert("Please enter a valid password combination");
      theForm['Password1'].focus();
      return;
    }
  }

  theForm.Submit();
}

function IsEmailValid(str_address) 
{ 
  str_address = str_address.toString();
  str_address = str_address.toLowerCase();
	str_address = str_address.replace("'","");  
  var isEmail = str_address.match(/^([A-Za-z0-9_-]+[.]?)+@[A-Za-z0-9_.-]+\.(((com|COM|net|NET|org|ORG|edu|EDU|gov|GOV|mil|MIL|biz|BIZ|info|INFO|pro|PRO|coop|COOP|aero|AERO|museum|MUSEUM|name|NAME|[a-z]{2})$)|(((com|COM|net|NET|org|ORG|edu|EDU|gov|GOV|mil|MIL|biz|BIZ|info|INFO|pro|PRO|coop|COOP|aero|AERO|museum|MUSEUM|name|NAME)\.[a-z]{2})$))/); 

  return (!isEmail ? false : true);
} 

function ResetForm()
{
  theForm.Init();
}


// Login Functions
function ResetLogin()
{
  document.Theform.UserName.value = "";
  document.Theform.Password.value = "";
}

function IsValidPhoneNumber(num)
{
   var extraChars = "()-0123456789 ";
   for (var i = 0, size = num.length; i != size; i++) 
   {
       var aChar = num.substring(i,i+1);
       search = extraChars.indexOf(aChar);
       if (search == -1)
       {
          return false ;
          break;
       }
       if (size < 10)
       {
          return false ;
          break;
       }
   }
   return true;
}

function ValidateForm()
{
    if (document.frmCustReg.FirstName.value == "")
        {
            alert("Please enter a First Name");
            document.frmCustReg.FirstName.focus();
            return false ;
        }
    if (!validExtendedChars(document.frmCustReg.FirstName.value))   
        {
            alert("First Name contains invalid characters (|,<,>,\",!,@,#,$,.,&,*). Please Remove them.");
            document.frmCustReg.FirstName.focus();
            return false ;
        }             
    if (document.frmCustReg.LastName.value == "")
        {
            alert("Please enter a Last Name");
            document.frmCustReg.LastName.focus();
            return false ;
        }
    if (!validExtendedChars(document.frmCustReg.LastName.value))   
        {
            alert("Last Name contains invalid characters  (|,<,>,\",!,@,#,$,.,&,*). Please Remove them.");
            document.frmCustReg.LastName.focus();
            return false ;
        }          
    if (document.frmCustReg.JobTitle.value == "")
        {
            alert("Please enter a Job Title");
            document.frmCustReg.JobTitle.focus();
            return false ;
        }
    if (!validChars(document.frmCustReg.JobTitle.value))   
        {
            alert("Job Title contains invalid characters (|,<,>,\"). Please Remove them.");
            document.frmCustReg.JobTitle.focus();
            return false ;
        }            
    if (document.frmCustReg.Company.value == "")
        {
            alert("Please enter a Company name");
            document.frmCustReg.Company.focus();
            return false ;
        }
    if (!validChars(document.frmCustReg.Company.value))   
        {
            alert("Company Name contains invalid characters (|,<,>,\"). Please Remove them.");
            document.frmCustReg.Company.focus();
            return false ;
        }            
    if (document.frmCustReg.Address1.value == "")
        {
            alert("Please enter an Address");
            document.frmCustReg.Address1.focus();
            return false ;
        }
    if (!validChars(document.frmCustReg.Address1.value))   
        {
            alert("Address contains invalid characters (|,<,>,\"). Please Remove them.");
            document.frmCustReg.Address1.focus();
            return false ;
        }         
    if (document.frmCustReg.Country[document.frmCustReg.Country.selectedIndex].text == "")
        {
            alert("Please select a Country");
            document.frmCustReg.Country.focus();
            return false ;
        }  
 
     if (document.frmCustReg.State.style.display=="block")
		{
			if (document.frmCustReg.State[document.frmCustReg.State.selectedIndex].text == "") 
			{
				alert("Please select a State");
				document.frmCustReg.State.focus();
				return false ;    
			}
		}

	if (document.frmCustReg.Country[document.frmCustReg.Country.selectedIndex].text == "USA"||document.frmCustReg.Country[document.frmCustReg.Country.selectedIndex].text == "CANADA") 
	    {
	    if (document.frmCustReg.State[document.frmCustReg.State.selectedIndex].value == "N/A")
	        {
	        alert("Please select a State/Province");
		    document.frmCustReg.State.focus();
			return false ;    
	        }
	     }
	
    if (document.frmCustReg.City.value == "")
        {
            alert("Please enter a City");
            document.frmCustReg.City.focus();
            return false ;
        }    
    if (!validChars(document.frmCustReg.City.value))   
        {
            alert("City contains invalid characters (|,<,>,\"). Please Remove them.");
            document.frmCustReg.City.focus();
            return false ;
        }          
    if ((document.frmCustReg.Country[document.frmCustReg.Country.selectedIndex].text == "Australia") || (document.frmCustReg.Country[document.frmCustReg.Country.selectedIndex].text == "Hungary"))
    {
      if (document.frmCustReg.PostalCode.value.length < 4)
          {
              alert("Please enter a valid Postal Code");
              document.frmCustReg.PostalCode.focus();
              return false ;
          }    
    }
    else
    {       
    if ((document.frmCustReg.PostalCode.value.length < 5)  && (document.frmCustReg.Country[document.frmCustReg.Country.selectedIndex].text == "USA"))
        {
            alert("Please enter a valid Postal Code");
            document.frmCustReg.PostalCode.focus();
            return false ;
        }
   }
    if (!validChars(document.frmCustReg.PostalCode.value))   
        {
            alert("Postal Code contains invalid characters (|,<,>,\"). Please Remove them.");
            document.frmCustReg.PostalCode.focus();
            return false ;
        }
    if (document.frmCustReg.Telephone.value == "")
        {
            alert("Please enter a telephone number")
            document.frmCustReg.Telephone.focus();
            return false ;
        }
    else
        {
          var chkPhone=document.frmCustReg.Telephone.value
        	var extraChars = "+()-0123456789 ";
	        for (var i = 0, size = chkPhone.length; i != size; i++) 
	        {
		        var aChar = chkPhone.substring(i,i+1);
		        search = extraChars.indexOf(aChar);
		        if (search == -1)
		        {
		            alert("The phone number may contain only numbers, dashes, '(', ')' and '+' symbols.")
		            document.frmCustReg.Telephone.focus();
		            return false ;
                }
            }
        }    
  if (typeof(document.frmCustReg.Industry) != "undefined")
  {
	if (document.frmCustReg.Industry.selectedIndex == 0)
	{
		alert("Please select an Industry");
		document.frmCustReg.Industry.focus();
		return;
	}

	if (document.frmCustReg.Industry.options[document.frmCustReg.Industry.selectedIndex].text == "OTHERS" && document.frmCustReg.OtherIndustry.value == "Please Enter Other Industry")
	{
	alert("Please enter an Industry for 'OTHERS'");
	document.frmCustReg.Industry.focus();
	return;
	}
	        
  }
    if (document.frmCustReg.Email.value=="" || !IsEmailValid(document.frmCustReg.Email.value))
        {
            alert("Please enter a valid email address") ;
            document.frmCustReg.Email.select();
            document.frmCustReg.Email.focus();
            return false ;
        }
    if (!validChars(document.frmCustReg.Email.value))   
        {
            alert("Email contains invalid characters (|,<,>,\"). Please Remove them.");
            document.frmCustReg.Email.focus();
            return false ;
        }   
        
    if (typeof(document.frmCustReg.UserName) != "undefined")
       {    
        if (!IsValidUsername(document.frmCustReg.UserName.value))
           {
                alert("Please enter a valid username")
                document.frmCustReg.UserName.select();
                document.frmCustReg.UserName.focus();
                return false ;
           }
        if(document.frmCustReg.UserName.value.indexOf(' ') > 0) 
           {
                alert("Spaces are not allowed in the UserName");
                document.frmCustReg.UserName.select();
                document.frmCustReg.UserName.focus();
                return false;
           }
    if (!validChars(document.frmCustReg.UserName.value))   
        {
            alert("User Name contains invalid characters (|,<,>,\"). Please Remove them.");
            document.frmCustReg.UserName.focus();
            return false ;
        }            
       }
   if (document.frmCustReg.NewReg.value=="False")
        {
            if (document.frmCustReg.EUN.value=="True")
                {
                 if (document.frmCustReg.OrgUser.value != document.frmCustReg.Email.value)
                    {
                       if (!IsValidPasswordCombo(document.frmCustReg.Password1.value,document.frmCustReg.Password2.value))
                        {
                            alert("Please enter valid matching passwords.")
                            return false ;
                        }
                    }
                }
            
        }
    else
        {
             if (document.frmCustReg.EUN.value=="True")
                {
					if (!IsValidPasswordCombo(document.frmCustReg.Password1.value,document.frmCustReg.Password2.value))
					   {
					    alert("Please enter valid matching passwords.")
					    return false ;
					   }
			    }		           
        }

    return true;
}

function validChars(str)
{
  var isGood = true;
    if (str.indexOf('"') >= 0)
      isGood = false;
    if (str.indexOf('|') >= 0)
      isGood = false;    
    if (str.indexOf('<') >= 0) 
      isGood = false;    
    if (str.indexOf('>') >= 0)
        isGood = false;
      
  return isGood;
}


function validExtendedChars(str)
{
  var isGood = true;
    if (str.indexOf('"') >= 0)
      isGood = false;
    if (str.indexOf('|') >= 0)
      isGood = false;    
    if (str.indexOf('<') >= 0) 
      isGood = false;    
    if (str.indexOf('>') >= 0)
        isGood = false;
    if (str.indexOf('!') >= 0)
        isGood = false;      
    if (str.indexOf('@') >= 0)
        isGood = false;      
    if (str.indexOf('#') >= 0)
        isGood = false;      
    if (str.indexOf('$') >= 0)
        isGood = false;      
    if (str.indexOf('.') >= 0)
        isGood = false;      
    if (str.indexOf(',') >= 0)
        isGood = false;      
    if (str.indexOf('&') >= 0)
        isGood = false;      
    if (str.indexOf('*') >= 0)
        isGood = false;      
        
  return isGood;
}