//<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.email2.value=="" || !IsEmailValid(document.frmCustReg.email2.value))
        {
            alert("Please enter a valid email address") ;
            document.frmCustReg.email2.select();
            document.frmCustReg.email2.focus();
            return false ;
        }
        
    if (document.frmCustReg.FirstName.value == "")
        {
            alert("Please enter a First Name");
            document.frmCustReg.FirstName.focus();
            return false ;
        }
    if (document.frmCustReg.LastName.value == "")
        {
            alert("Please enter a Last Name");
            document.frmCustReg.LastName.focus();
            return false ;
        }
   
    if (document.frmCustReg.Company.value == "")
        {
            alert("Please enter a Company Name");
            document.frmCustReg.Company.focus();
            return false ;
        }
    
  
    return true;
}

function ValidateWebDemoForm()
{
   if (document.frmCustReg.email1.value=="" || !IsEmailValid(document.frmCustReg.email1.value))
        {
            alert("Please enter a valid email address") ;
            document.frmCustReg.email1.select();
            document.frmCustReg.email1.focus();
            return false ;
        }
                        
    if (document.frmCustReg.Name.value == "")
        {
            alert("Please enter a First and Last Name");
            document.frmCustReg.Name.focus();
            return false ;
        }
      
    if (document.frmCustReg.Company.value == "")
        {
            alert("Please enter a Company Name");
            document.frmCustReg.Company.focus();
            return false ;
        }
    if (document.frmCustReg.Telephone.value == "")
        {
            alert("Please enter a Phone Number");
            document.frmCustReg.Telephone.focus();
            return false ;
        }    
    
    var count = 0;
    for (var i = 0 ; i < document.frmCustReg.contactus.length ; ++i) 
    {
        if (document.frmCustReg.contactus[i].checked) { ++count; }

    }

    if (count == 0) 

    {        
        alert("Please select an Action Item.");
        return false;
    }          

    if (document.frmCustReg.contactus[2].checked)
    {
       if (document.frmCustReg.webDate.value=="")
            {
                alert("Please enter a valid date to schedule your web meeting");
                document.frmCustReg.webDate.select();
                document.frmCustReg.webDate.focus();
                return false ;
            }
        if (!IsDateValid(document.frmCustReg.webDate.value ))
            {
                document.frmCustReg.webDate.select();
                document.frmCustReg.webDate.focus();
                return false ;        
            }      
            
       if (document.frmCustReg.webTime.value=="")
            {
                alert("Please enter a valid time to schedule your web meeting");
                document.frmCustReg.webTime.select();
                document.frmCustReg.webTime.focus();
                return false ;
            }    
        
    return true;
    }
    return true;
}


function ValidateScheduleDemoForm()
{
   if (document.frmCustReg.email1.value=="" || !IsEmailValid(document.frmCustReg.email1.value))
        {
            alert("Please enter a valid email address") ;
            document.frmCustReg.email1.select();
            document.frmCustReg.email1.focus();
            return false ;
        }
                        
    if (document.frmCustReg.Name.value == "")
        {
            alert("Please enter a First and Last Name");
            document.frmCustReg.Name.focus();
            return false ;
        }
      
    if (document.frmCustReg.Company.value == "")
        {
            alert("Please enter a Company Name");
            document.frmCustReg.Company.focus();
            return false ;
        }
    if (document.frmCustReg.Telephone.value == "")
        {
            alert("Please enter a Phone Number");
            document.frmCustReg.Telephone.focus();
            return false ;
        }    
        
   if (document.frmCustReg.webDate.value=="")
            {
                alert("Please enter a valid date to schedule your web meeting");
                document.frmCustReg.webDate.select();
                document.frmCustReg.webDate.focus();
                return false ;
            }
        if (!IsDateValid(document.frmCustReg.webDate.value ))
            {
                document.frmCustReg.webDate.select();
                document.frmCustReg.webDate.focus();
                return false ;        
            }      
            
       if (document.frmCustReg.webTime.value=="")
            {
                alert("Please enter a valid time to schedule your web meeting");
                document.frmCustReg.webTime.select();
                document.frmCustReg.webTime.focus();
                return false ;
            }    
    return true;
}

function ValidateFormEmail()
{
   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 ;
        }

    
  
    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;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function IsDateValid(dtStr){
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Please enter a valid date to schedule your web meeting. The format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month to schedule your web meeting")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day to schedule your web meeting")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date to schedule your web meeting")
		return false
	}
return true
}

// 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;
  
  // 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 = "";
}


