//
// Common JavaScript library, 31 August 2001
//
// Application mebmer: CaTS, TRaCS, form4, kb, Web team, coop
//

var imgDir="/images/NewSite/nav/";browserName = navigator.appName;browserVer = parseInt ( navigator.appVersion );version = "n2";
if ( browserName == "Netscape" && browserVer >= 3 ) version = "n3";if ( browserName == "Microsoft Internet Explorer" && browserVer >=4 ) version = "e4";
if ( version == "n3" || version == "e4" ) {
b1dn = new Image();b1dn.src = imgDir+"NavHomeoff.gif";b1up = new Image();b1up.src = imgDir+"NavHomeon.gif";
b2dn = new Image();b2dn.src = imgDir+"leftnavsuccess.jpg";b2up = new Image();b2up.src = imgDir+"leftnavsuccess_over.jpg";
b3dn = new Image();b3dn.src = imgDir+"leftnavwebinar.jpg";b3up = new Image();b3up.src = imgDir+"leftnavwebinar_over.jpg";
b4dn = new Image();b4dn.src = imgDir+"LeftNavFindReseller.jpg";b4up = new Image();b4up.src = imgDir+"LeftNavFindReseller_over.jpg";
b5dn = new Image();b5dn.src = imgDir+"LeftNavRequestMore.jpg";b5up = new Image();b5up.src = imgDir+"LeftNavRequestMore_over.jpg";
b6dn = new Image();b6dn.src = imgDir+"LeftNavcontact.jpg";b6up = new Image();b6up.src = imgDir+"LeftNavcontact_over.jpg";
}

  function hiLite(imgDocID,imgObjName) {
  if ( version == "n3" || version == "e4" ) {
  document.images[imgDocID].src = eval(imgObjName + ".src")
  }
}

// Image rollover management
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function OpenWindow(urladdr, intwidth, intheight, scrollbars)
{

   var hWnd = window.open (urladdr,"NewWindow","width="+ intwidth +",height="+ intheight +",toolbar=no,resizable=no,scrollbars=" + scrollbars + ",screenX=50,ScreenY=50,left=100,top=100");
}

function flipImage(url, names)
// from: Tony Lai
{
if(navigator.appVersion.lastIndexOf("MSIE 3.0") == -1) document[names].src = url;
}

// image toggle for IE and Netscape 
// e.g. imgsw('happy','happyon.gif')
// e.g. imgsw('happy','happyoff.gif')
// from: Tony Lai
function imgsw(name,a) {
   if (document.images){
      document.images[name].src=a;
                       }
                      }

// for main menu and redirector
// from: Tony Lai
function bgRoll(strTextColor,strBgColor)
{
	window.event.srcElement.style.backgroundColor = strBgColor;
	window.event.srcElement.style.color = strTextColor;
	window.event.cancelBubble = true;
}

// for main menu and redirector
// from: Tony Lai
function fntRoll(strTextColor)
{
	window.event.srcElement.style.color = strTextColor;
	window.event.cancelBubble = true;
}

// open a new windows
// from: Tony Lai
function GetNewWindow(urladdr)
{
   var hWnd = window.open(urladdr,"MSCalendar1","width=230,height=190,toolbar=no,resizable=no,scrollbars=no,screenX=50,ScreenY=50,left=100,top=100");
  // if (hWnd.focus != null) hWnd.focus();  
}


//enter limit in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59
// from: Tony Lai
function begintimer(){
if (!document.images)
return
if (parselimit==1){
window.status='Times up! Your page will being disconnected from the server anytime. Please copy and paste your content and try it later...'
Timermsg()}
else{ 
parselimit-=1
curmin=Math.floor(parselimit/60)
cursec=parselimit%60
if (curmin!=0)
curtime=curmin+" minutes and "+cursec+" seconds left"
else
curtime=cursec+" seconds left"
window.status=curtime
setTimeout("begintimer()",1000)
}
}

// Timeout Error Messge
// from: Tony Lai
function Timermsg() {
alert("You have 5 minutes left before connection timeout, Please save your page ");
} 

// Super function, check user input Date(mm,dd,yy)
// working  with another function:
// chkMonth(), leapYear(year) and getDays()
// from: Tony Lai
function chkday(mm,dd,yy){
mm=Number(mm)
dd=Number(dd)
yy=Number(yy)
a = getDays(mm,yy)
a = a + 1  
if ((dd > 0) && (dd < a)){
return false }
else{return true}
}


function chkMonth(month){
// from: Tony Lai
if ((month > 0) && (month < 13)){
return false }
else{return true}
}

function leapYear(year) {
if (year % 4 == 0) // basic rule
return true // is leap year
/* else */ // else not needed when statement is "return"
return false // is not leap year
}


function getDays(month, year) {
// create array to hold number of days in each month
// from: Tony Lai
var ar = new Array(13)
ar[1] = 31 // January
ar[2] = (leapYear(year)) ? 29 : 28 // February
ar[3] = 31 // March
ar[4] = 30 // April
ar[5] = 31 // May
ar[6] = 30 // June
ar[7] = 31 // July
ar[8] = 31 // August
ar[9] = 30 // September
ar[10] = 31 // October
ar[11] = 30 // November
ar[12] = 31 // December
return ar[month]
}

// function, check user input email address textbox object
// from: Tony Lai
function chkemail(email){
// have basic email checking

  var checkStr = stripChars(/\s/g,email.value); 
  if (checkStr == "")
  {
    alert("Please enter a value for the email address field.");
    email.focus();
    return (false);
  }

  var checkOK = "@";
  var allValid = false;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++){
      if (ch == checkOK.charAt(j)){
				allValid = true;}
    }
  }
  if (!allValid)
  {
    alert("Email address is missing \"@\" ");
    email.focus();
    return (false);
  }

  var checkOK = ".";
  var allValid = false;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++){
      if (ch == checkOK.charAt(j)){
				allValid = true;}
    }
  }
  if (!allValid)
  {
    alert("Email address is missing \".\" ");
    email.focus();
    return (false);
  }

  var checkOK = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@_.-";
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Email address is invalid.");
    email.focus();
    return (false);
  }
	else
	{
    return (true);
	}
}

//round to 2 decimal place
function roundit(which){
return Math.round(which*100)/100
}


function checkDate(sDate)	//Julia (mm/dd/yyyy format)
{   
 var iPos;
 var iDay;
 var iMonth;
 var iYear;
 var MyDate;

 blCorrect=false;
 pos=sDate.search('/');
 if(pos!=2)return false;
 iMonth=(sDate.substring(0,pos));
 sDate=sDate.substr(pos+1);
 pos=sDate.search('/');
 if(pos!=2)return false;
 iDay=(sDate.substring(0,pos));
 sDate=sDate.substr(pos+1);                         
 if(sDate.length!=4)return false;
 iYear=(sDate);
 if(iYear<1799)return false;

 MyDate=new Date(iYear,iMonth-1,iDay);
 if(iYear==MyDate.getFullYear()
 && (iMonth-1)==MyDate.getMonth()
 && (iDay==MyDate.getDate())) return true;
 return false;
}

function checkDigLet(sDig)	//Julia
{
   var regularexpression =/[^A-Za-z0-9]/i;
   var checkStr=new String("");  
   checkStr = sDig;
   if (checkStr.search(regularexpression)!=-1) return false;
   return true;
}

function checkDig(sDig)	//Julia
{
   var regularexpression =/[^0-9]/i;
   var checkStr=new String("");  
   checkStr = sDig;
   if (checkStr.search(regularexpression)!=-1) return false;
   return true;
}
function checkDigAndDot(sDig)	//Julia (check for dd..d.d format. To modify, change regular expression as you like!)
{
   var regularexpression =/^\d*\.\d{1}$/;
   var checkStr=new String(""); 
   var notvalid=true; 
   checkStr = sDig;
   notvalid = !isValid(regularexpression,checkStr);
   if (notvalid) 
   {
	return false;
   }
   return true;
}
function isValid(pattern, str) //Julia
{
	return pattern.test(str);
}

function stripChars(pattern, str)	//Julia	(see example below)
					//	var str1 = stripChars(/\s/g,document.addform.ModuleName.value);
					//	if  (str1=='')
					//	{
					//		alert("Please enter a value for  the \"Module Name\" field.");
					//		document.addform.ModuleName.focus();
					//		return (false);	
					//	}
{
	return (str.replace(pattern,""));
}

function checkDigNew(sDig)	//Julia
{
   var regularexpression =/[^0-9]/i;
   var checkStr=new String("");  
   checkStr = sDig;
   if (sDig=="" ||(checkStr.search(regularexpression)!=-1)) return false;
   return true;
}

function checkCur(sDig)	//Julia (check for dd..d.d format. To modify, change regular expression as you like!)
{
   var regularexpression =/^\d*$|^\d*\.\d{2}$/;
   //var checkStr=new String(""); 
   var notvalid=true; 
  // checkStr = sDig;
  // notvalid = !isValid(regularexpression,checkStr);
   //   if (notvalid) 
	if (!regularexpression.test(sDig))
   {
	return false;
   }
   return true;
}


// open a new window and focus on it
function OpenAccpacWindow(destination)
{
	if (window.name=='ACCPAC')
	{
		window.location.href=destination;
	}
	else
	{
		var strFeatures = "resizable=yes, toolbar=1,menubar=1,location=1,directories=1,scrollbars=1";
		var AW=window.open (destination,'ACCPAC',strFeatures);
		AW.focus();
	}
	return false;
}


//*** Validate email Function ***
function IsValidEmail(src) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
  }

//
// This form is for the standard validating of required form fields
// It checks to see if the REQUIRED field is present "sFIELDNAME"
// If the field exists, the validation check is completed
// If the field does not exist the validation for that field is bypassed
//
// Standard required field names are:
// sName  sTitle  sClientID sBPID sCoName sAddress sCity sStateProv
// sZipPostal sCountry sTelephone sFax sEmail
//
// If the field is not a required field, then you can use the field name
// without the preceeding "s"
//
// Any required fields, unique to a specific form, must be validated using
// a JavaScript within the form code.


function sValidate(theForm)

{

if (theForm.sName)
{
if (theForm.sName.value == "")
{
alert("Please enter your name in the \"Name\" field.")
theForm.sName.focus()
return (false)
}
}

if (theForm.sTitle)
{
if (theForm.sTitle.value.length < 1)
{
alert("Please enter your company title in the \"Title\" field.")
theForm.sTitle.focus()
return (false)
}
}

if (theForm.sClientID)
{
if (theForm.sClientID.value.length < 1)
{
alert("Please enter your client ID number in the \"Client ID\" field.")
theForm.sClientID.focus()
return (false)
}
}

if (theForm.sBPID)
{
if (theForm.sBPID.value.length < 1)
{
alert("Please enter your Business Partner ID in the \"BP ID\" field.")
theForm.sBPID.focus()
return (false)
}
}

if (theForm.sCoName)
{
if (theForm.sCoName.value == "")
{
alert("Please enter your company's name in the \"Company\" field.")
theForm.sCoName.focus()
return (false)
}
}

if (theForm.sAddress)
{
if (theForm.sAddress.value == "")
{
alert("Please enter the address of your company in the \"Address\" field.")
theForm.sAddress.focus()
return (false)
}
}

if (theForm.sCity)
{
if (theForm.sCity.value == "")
{
alert("Please enter your city in the \"City\" field.")
theForm.sCity.focus()
return (false)
}
}

if (theForm.sStateProv)
{
if (theForm.sStateProv.value.length < 2 || theForm.sStateProv.value == "-- ")
{
  alert("Please enter your state or province.")
  theForm.sStateProv.focus()
  return (false)
}
}

if (theForm.sZipPostal)
{
if (theForm.sZipPostal.value == "")
{
alert("Please enter your zip or postal code.")
theForm.sZipPostal.focus()
return (false)
}
}

if (theForm.sCountry)
{
if (theForm.sCountry.value == "")
{
alert("Please enter your country.")
theForm.sCountry.focus()
return (false)
}
}

if (theForm.sTelephone)
{
if (theForm.sTelephone.value == "")
{
alert("Please enter your telephone number.")
theForm.sTelephone.focus()
return (false)
}
}

if (theForm.sFax)
{
if (theForm.sFax.value == "")
{
alert("Please enter your fax number.")
theForm.sFax.focus()
return (false)
}
}

// the chkemail function is located earlier in the common.js file

if (theForm.sEmail)
{
if (!chkemail(theForm.sEmail))
{
theForm.sEmail.focus();
return (false);
}
}

// If we made it to here, everything that is required is valid, so return
// to the validation function for further checking & the return command
   return (true)

}


// START of credit card type and credit card number checking.
// This function takes the name and number of a credit card, and determines 
// whether the number is valid for the type of card.
function checkCreditCard (CCType, CCNumber)
 {
   var cardType = CCType //getRadioButtonValue (radio)
    var normalizedCCN = stripCharsInBag(CCNumber, " ")
    if (!isCardMatch(cardType, normalizedCCN)) {
       return false;
}
    else 
       return true
} // END FUNCTION checkCreditCard()

/*  ================================================================
    Credit card verification functions
    Originally included as Starter Application 1.0.0 in LivePayment.
    20 Feb 1997 modified by egk:
    ================================================================ */


/*  ================================================================
    FUNCTION:  isCreditCard(st)
 
    INPUT:     st - a string representing a credit card number

    RETURNS:  true, if the credit card number passes the Luhn Mod-10
		    test.
	      false, otherwise
    ================================================================ */

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
} // END FUNCTION isCreditCard()

/*  ================================================================
    FUNCTION:  isVisa()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid VISA number.
		    
	      false, otherwise

    Sample number: 4111 1111 1111 1111 (16 digits)
    ================================================================ */

function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}  // END FUNCTION isVisa()


/*  ================================================================
    FUNCTION:  isMasterCard()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid MasterCard
		    number.
		    
	      false, otherwise

    Sample number: 5500 0000 0000 0004 (16 digits)
    ================================================================ */

function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isMasterCard()


/*  ================================================================
    FUNCTION:  isAmericanExpress()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid American
		    Express number.
		    
	      false, otherwise

    Sample number: 340000000000009 (15 digits)
    ================================================================ */

function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isAmericanExpress()



/*  ================================================================
    FUNCTION:  isDinersClub()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Diner's
		    Club number.
		    
	      false, otherwise

    Sample number: 30000000000004 (14 digits)
    ================================================================ */

function isDinersClub(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 14) && (firstdig == 3) &&
      ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
    return isCreditCard(cc);
  return false;
} // END FUNCTION isDinersClub()



/*  ================================================================
    FUNCTION:  isCarteBlanche()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Carte
		    Blanche number.
		    
	      false, otherwise
    ================================================================ */

function isCarteBlanche(cc)
{
  return isDinersClub(cc);
}


/*  ================================================================
    FUNCTION:  isDiscover()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Discover
		    card number.
		    
	      false, otherwise

    Sample number: 6011000000000004 (16 digits)
    ================================================================ */

function isDiscover(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) && (first4digs == "6011"))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isDiscover()


/*  ================================================================
    FUNCTION:  isEnRoute()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid enRoute
		    card number.
		    
	      false, otherwise

    Sample number: 201400000000009 (15 digits)
    ================================================================ */

function isEnRoute(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 15) &&
      ((first4digs == "2014") ||
       (first4digs == "2149")))
    return isCreditCard(cc);
  return false;
} // END FUNCTION isEnRoute()


/*  ================================================================
    FUNCTION:  isJCB()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid JCB
		    card number.
		    
	      false, otherwise
    ================================================================ */

function isJCB(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) &&
      ((first4digs == "3088") ||
       (first4digs == "3096") ||
       (first4digs == "3112") ||
       (first4digs == "3158") ||
       (first4digs == "3337") ||
       (first4digs == "3528")))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isJCB()


/*  ================================================================
    FUNCTION:  isAnyCard()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is any valid credit
		    card number for any of the accepted card types.
		    
	      false, otherwise
    ================================================================ */

function isAnyCard(cc)
{
  if (!isCreditCard(cc))
    return false;
  if (!isMasterCard(cc) && !isVisa(cc) && !isAmericanExpress(cc) && !isDinersClub(cc) &&
      !isDiscover(cc) && !isEnRoute(cc) && !isJCB(cc)) {
    return false;
  }
  return true;

} // END FUNCTION isAnyCard()


/*  ================================================================
    FUNCTION:  isCardMatch()
 
    INPUT:    cardType - a string representing the credit card type
	      cardNumber - a string representing a credit card number

    RETURNS:  true, if the credit card number is valid for the particular
	      credit card type given in "cardType".
		    
	      false, otherwise
    ================================================================ */

function isCardMatch (cardType, cardNumber)
{

	cardType = cardType.toUpperCase();
	var doesMatch = false;

	if ((cardType == "VISA") && (isVisa(cardNumber)))
		doesMatch = true;
	if ((cardType == "MASTERCARD") && (isMasterCard(cardNumber)))
		doesMatch = true;
	if ( ( (cardType == "AMERICANEXPRESS") || (cardType == "AMEX") )
                && (isAmericanExpress(cardNumber))) doesMatch = true;
	if ((cardType == "DISCOVER") && (isDiscover(cardNumber)))
		doesMatch = true;
	if ((cardType == "JCB") && (isJCB(cardNumber)))
		doesMatch = true;
	if ((cardType == "DINERS") && (isDinersClub(cardNumber)))
		doesMatch = true;
	if ((cardType == "CARTEBLANCHE") && (isCarteBlanche(cardNumber)))
		doesMatch = true;
	if ((cardType == "ENROUTE") && (isEnRoute(cardNumber)))
		doesMatch = true;
	return doesMatch;

}  // END FUNCTION CardMatch()


// Removes all characters which appear in string bag from string s.

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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}
// END of credit card type and credit card number checking.
