//<SCRIPT> // <- dummy script tag used to enable InterDev syntax coloring
//------------------------------------------------------------------------
//  sb_lib.js
//
//  Copyright© 2002, 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;
}

//------------------------------------------------------------------------
// Customer Registration Methods
//------------------------------------------------------------------------

function ValidateForm()
{  
 
    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 (document.frmCustReg.fname.value == "")
        {
            alert("Please enter a First Name")
            return false ;
        }
    if (document.frmCustReg.lname.value == "")
        {
            alert("Please enter a Last Name")
            return false ;
        }
    if (document.frmCustReg.company.value == "")
        {
            alert("Please enter a Company name")
            return false ;
        }
    if (document.frmCustReg.addr1.value == "")
        {
            alert("Please enter an Address")
            return false ;
        }
    if (document.frmCustReg.city.value == "")
        {
            alert("Please enter a City")
            return false ;
        }

    if (document.frmCustReg.title.value == "")
        {
            alert("Please enter your Job Title")
            return false ;
        }
              
    if (document.frmCustReg.Country[document.frmCustReg.Country.selectedIndex].text == "")
        {
            alert("Please select a Country")
            return false ;
        }
    if (document.frmCustReg.Country[document.frmCustReg.Country.selectedIndex].text == "United States" || document.frmCustReg.Country[document.frmCustReg.Country.selectedIndex].text == "USA" || document.frmCustReg.Country[document.frmCustReg.Country.selectedIndex].text == "Canada")
        {
            if (document.frmCustReg.State.value == "" || document.frmCustReg.postalCode.value == "")
            {
                alert("Please enter a state/province and postal code")
                return false ;
            }
        }
    if (document.frmCustReg.phone.value == "")
        {
            alert("Please enter a telephone number")
            return false ;
        }
    else
        {
            var chkPhone=document.frmCustReg.phone.value
            if (!IsValidPhoneNumber(chkPhone))
               {
               alert("The phone number may contain only numbers, dashes, and ().");
               return false ;
               }
        } 
    if (typeof(document.frmCustReg.ElectronicCatalog) != "undefined")
    {
		if (document.frmCustReg.ElectronicCatalog[document.frmCustReg.ElectronicCatalog.selectedIndex].text == ">")
			{
				alert("Please select a Product Of Interest")
				return false ;
			}  
		}

	 if (typeof(document.frmCustReg.industry) != "undefined")
	 {		
   if (document.frmCustReg.industry[document.frmCustReg.industry.selectedIndex].text == ">")
        {
            alert("Please select your Industry")
            return false ;
        }
    } 
    		
    if (typeof(document.frmCustReg.ItemsRequested) != "undefined")
    {
		if (document.frmCustReg.ItemsRequested[document.frmCustReg.ItemsRequested.selectedIndex].text == ">")
			{
				alert("Please select an Action Request")
				return false ;
			}                         
		}

    if (typeof(document.frmCustReg.IntendUse) != "undefined")
    {
		if (document.frmCustReg.IntendUse[document.frmCustReg.IntendUse.selectedIndex].text == ">")
        {
            alert("Please select your Intended Use")
            return false ;
        } 
    }		
	return true;
}

function IsValidUsername(Username)
{
  var illegalFirstChars = "_.0123456789";
  var firstChar = Username.substring(0,1);
  var search = illegalFirstChars.indexOf(firstChar);
  var userlen = Username.length
  if (userlen < 5) // username too short
    return false;
  if (search != -1) // illegal character found in first positon
    return false;
  if (HasBadCharacters(Username)) // illegal characters found within the string
    return false;
  return true;
}

function HasBadCharacters(CheckMe) {
	var extraChars = "_.-@0123456789";
	for (var i = 0, size = CheckMe.length; i != size; i++) {
		var aChar = CheckMe.substring(i,i+1);
		aChar = aChar.toUpperCase();
		search = extraChars.indexOf(aChar);
		if (search == -1 && (aChar < "A" || aChar > "Z")) return true;
	}
	return false;
}

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;
       }
   }
   return true;
}

function IsValidPasswordCombo(Password,Password2)
{
  if (Password.length < 5)
    return false;
  if (Password != Password2)
    return false;
  if (ContainSpaces(Password))
    return false;
  return true;
}

function IsEmailValid(str_address) 
{ 
  str_address = str_address.toString();
  var isEmail = str_address.match(/^([A-Za-z0-9_-]+[.]?)+@[A-Za-z0-9_.-]+\.(((com|net|org|edu|gov|mil|biz|info|pro|coop|aero|museum|name|[a-z]{2})$)|(((com|net|org|edu|gov|mil|biz|info|pro|coop|aero|museum|name)\.[a-z]{2})$))/); 
  return (!isEmail ? false : true);
} 

function ContainSpaces(CheckMe) {
	return (CheckMe.indexOf(' ') != -1) ? true : false;
}

// Login Functions
function ResetLogin()
{
  document.Theform.UserName.value = "";
  document.Theform.Password.value = "";
}

//------------------------------------------------------------------------
// 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=""
  ModCart.DelItem(str_cartIndex);
  ModCart.Send(flag);
}

function Update(flag)
{
  var qty;
  var itemID;
  if (typeof(document.frmShopCart.txtShipMethod !="undefined"))
  {
	if (document.frmShopCart.txtShipMethod.value == "" && flag == "comf")
	{	
		alert("Please select a shipping method");
		return;
	}  
  }
  // 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 IsEmailValid(str_address) 
{ 
  str_address = str_address.toString();
  var isEmail = str_address.match(/^([A-Za-z0-9_-]+[.]?)+@[A-Za-z0-9_.-]+\.(((com|net|org|edu|gov|mil|biz|info|pro|coop|aero|museum|name|[a-z]{2})$)|(((com|net|org|edu|gov|mil|biz|info|pro|coop|aero|museum|name)\.[a-z]{2})$))/); 

  return (!isEmail ? false : true);
} 

function ResetForm()
{
  theForm.Init();
}


// Login Functions
function ResetLogin()
{
  document.Theform.UserName.value = "";
  document.Theform.Password.value = "";
}

