function PageLoad() {
//	hideBorder();
//	PageResize();
}


function checkInt(textBox) {
	try {
		// check for an integer
		var val = parseInt(textBox.value);
		
		// if it's not a number use a blank string
		if (isNaN(val)) {
			val = "";
		}
		
		// set the value of the text box
		if (textBox.value != val) {
			textBox.value = val;
		}
	} catch (e) {
		// do nothing
	}
}
// called by a radiobutton that needs to ensure it is the only checked button on a grid
function SR( rdo )
{
   // find parent table
   var pTable = rdo.parentNode;
   while( pTable.tagName != "TABLE" )
   {
      pTable = pTable.parentNode;
   }

   // work through all cells in the table
   for(i = 0; i < pTable.cells.length; i++)
   {
      var t = pTable.cells(i);
      if ( t.tagName == "TD" )
      {
         for(j=0; j < t.childNodes.length; j++)
         {
            var s = t.childNodes(j);
            if( s.tagName == "SPAN" )
            {
               // assume the radiobutton we're looking for is in cell 1
               var r = s.all(0);
               if( r.type == "radio" )
               {
                  r.checked = false;
                  break;
               }
            }
         }
      }
   }
   rdo.checked = true;
   return true;
}
function PopupNewWindow(strUrl, x, y)
{
	var s = "height=" + y + ",width="+x+",status=yes,toolbar=no,menubar=no,location=no";
	var win = window.open(strUrl,null,s);
	if( !win )
	{
	   alert("It looks like you may be running a pop-up blocker.  Please enable pop-ups from CafeCartago.com to fully enjoy this site. Thank you!");
	}
	else
	{
	   win.focus();
	}
}

function PopupHelp(strUrl, x, y)
{
	var s = "height=" + y + ",width=" + x + ",resizable=0,status=0,toolbar=0,menubar=0,location=0,directories=0,scrollbars=0";
	var win = window.open(strUrl,null,s);
	if( !win )
	{
	   alert("It looks like you may be running a pop-up blocker.  Please enable pop-ups from CafeCartago.com to fully enjoy this site. Thank you!");
	}
	else
	{
	   win.focus();
	}
}

function LaunchNewWindow(strUrl)
{
   var s = "_blank";
	var win = window.open(strUrl,s);
	if( !win )
	{
	   alert("It looks like you may be running a pop-up blocker.  Please enable pop-ups from CafeCartago.com to fully enjoy this site. Thank you!");
	}
	else
	{
	   win.focus();
	}
}
function PopImage(sUrl, sTitle, sAlt, width, height )
{
   try
   {
      var strUrl = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
      strUrl = strUrl + "<html xmlns='http://www.w3.org/1999/xhtml' >";
      strUrl = strUrl + "<head><title>" + sTitle + "</title>";
      strUrl = strUrl + "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />";
      strUrl = strUrl + "</head>";
      strUrl = strUrl + "<body><div>";
      strUrl = strUrl + "<div><img alt='"+ sAlt +"' src='"+ sUrl +"' /></div>";
      strUrl = strUrl + "<div align='center'><input type='button' value='Close Window' onclick='window.close(); return false;'' ID='Button1' NAME='Button1'></div>";
      strUrl = strUrl + "</form></body>";
      strUrl = strUrl + "</div></body></html>";

      height = height + 45;
	   var s = "height=" + height + "px,width="+width+"px,status=yes,toolbar=no,menubar=no,location=no";

	   var win = window.open('','_blank', s);
	   if( !win )
	   {
	      alert("It looks like you may be running a pop-up blocker.  Please enable pop-ups from CafeCartago.com to fully enjoy this site. Thank you!");
	   }
	   else
	   {
	      win.document.write(strUrl);
	      win.focus();
	   }
   }
   catch(ex)
   {
      alert(ex);
   }
}

/*
function Session_Start()
{
   debugger;
   Session("backcolor") = "Beige";
}
*/

// called by ActionMenu to validate selection before submitting to ShoppingCart
function ValSel( btn, rID, qID )
{
   try
   {
      var pe = btn.parentNode;

      if( pe.parentNode != null )
         pe = btn.parentNode.parentNode;


      var s = GotSize( pe, rID );
      var q = 1; //GotQuan( pe, qID );

      if( s && q )
      {
         __doPostBack(btn.id,pe.rowIndex-1);
         return true;
      }
      else
      {
         var str = "";
         if( !s )
         {
            str = "Please select a package size.";
         }
         if( !q )
         {
            if( str.length > 0 )
               str = str + "\n";
            str = str + "Please enter a quantity for this Item.";
         }
         alert( str );
      }
   }
   catch(ex)
   {
      alert(ex);
   }
   return false;
}
function GotSize( pe, sID )
{
   try
   {
      for( i = 0; i < 20; i++ )
      {
         var rdo = document.getElementById(sID+'_' + i);
         if( rdo == null )
         {
            return false;
         }
         if( rdo.type == "radio" )
         {
            if( rdo.checked )
            { 
               return true;
            }
         }
      }
      return false;
   }
   catch(ex)
   {
      alert(ex);
   }
   return false;
}
function GotQuan( pe, sID )
{
   var r = document.getElementById(sID);
   if( r.type=="text" )
   {
      var b = r.value
      if( b.length > 0 )
      { 
         return true;
      }
   }
   return false;
}
// displays browser information
function DumpBrowser()
{
  alert( 
      "appCodeName = " + navigator.appCodeName +
      "\nappName = " + navigator.appName +
      "\nappVersion = " + navigator.appVersion + 
      "\nplatform = " + navigator.platform + 
      "\nproduct = " + navigator.product + 
      "\nproductSub = " + navigator.productSub + 
      "\nvendor = " + navigator.vendor 
      );// = "Mozilla"	
}
// verify credit card date is current, called by ProfileCreditCardInfo.aspx
// display a wait cursor
function doHourglass()
{
  document.body.style.cursor = 'wait';
}
// scroll to top of window
function Top()
{ 
   window.scroll(0,0); 
} 
// scroll to a particular control
function ScrollTo(ctrlID)
{
   var r = document.getElementById(ctrlID);
   if( r )
   {
      var leftPos = 0;
      var topPos = 0;
                 
      while(r != null)
      {
       leftPos += r.offsetLeft;
       topPos += r.offsetTop;
       r = r.offsetParent;
      }   
   
      window.scroll(0, topPos);
   }
}
// set focus on a control
function SetFocus(id)
{
   var r = document.getElementById(id);
   if( r )
   {
      r.focus()
      }
}
// performs grouplike radiobutton control, only valid if all share the same "value" attribute
function chkExclusive(id) 
{
   var value = null; 
   var element = document.getElementById(id);
   var radioGroupName = null; 
   if (element) 
   {
      radioGroupName = element.value;
   }
   if (radioGroupName == null) 
   {
      return null; 
   }
   var radios = document.getElementsByTagName("input"); for (var i=0; i<radios.length; i++) 
   {
      var input = radios[ i ];
      if (input.value == radioGroupName) 
      {
         input.checked = 0;
      }
   }
   element.checked = 1;
}

// WebValidation.js.
//<script>
var Page_DomValidationVer = "2";
var Page_IsValid = true;  
var Page_BlockSubmit = false;

// returns the parent control for a given validator
function ValidatorGetControl(val) {
    var id = val.getAttribute("controltovalidate");
    if( val.controltovalidate != null)
      id = val.controltovalidate;
    var control = document.getElementById(id);
    return control;
}

// the AlertValidator's state is determined by the other validators: if any are false then this is false
function AlertValidatorEvaluator(val) {
    var i;
    var sID = val.getAttribute("id");

   // fetch control's validator array
    var cont = ValidatorGetControl(val);
    var vals = cont.Validators;
    
    val.isvalid = true;
    if( vals != null )
    {
       // loop through all validators for this control, but skip this one (the AlertValidator)
       // if multiple alertvalidators are specified we will probably get a deadly embrace
       for (i = 0; i < vals.length; i++) 
       {
           var v = vals[i];
           var xID = v.getAttribute("id");
           if( !(sID == xID ) )
           {
               if(!( typeof(v.disabled)=="undefined" || v.disabled == true ) ||
                  !( typeof(v.isDisabled)=="undefined" || v.isDisabled == true ) ||
                  !( typeof(v.enabled)=="undefined" || v.enabled == false ))
               {
                   if (!vals[i].isvalid) 
                   {
                      val.isvalid = false;
                      break;;
                   }
               }
           }
       }
   }

   AlertValidatorUpdateDisplay(val);

   return val.isvalid;
}

// paints the control if any validators are invalid, otherwise restore the original colors
function AlertValidatorUpdateDisplay(val) {

    var sID = val.getAttribute("id");

   var control = ValidatorGetControl(val);

   if( !val.isvalid )
   {
      /// save current values
      var o = control.getAttribute("obc");
      if( o == null )
      {
         control.setAttribute( "obc", control.style.backgroundColor );
      }
      var o = control.getAttribute("otc");
      if( !(typeof(o) == "string") )
      {
         control.setAttribute( "otc", control.style.color );
      }

      // set new values.  values can be defined either in line or in properties
      control.style.backgroundColor = val.style.backgroundColor;
      control.style.color = val.style.color;
   }
   else
   {
      // status is valid, so restore original colors if they have been saved
        var o = control.getAttribute("obc");
        if (typeof(o) == "string") 
        {
          control.style.backgroundColor = o;
          control.removeAttribute("obc");
        }
        o = control.getAttribute("otc");
        if (typeof(o) == "string") 
        {
          control.style.color = o;
          control.removeAttribute("otc");
        }
   }
}

// the RatingValidator's state is determined by the other validators: if any are false then this is false
function RatingValidatorEvaluator(val) {
    var i;
    var sID = val.getAttribute("id");

   // fetch control's validator array
    var cont = ValidatorGetControl(val);
    var vals = cont.Validators;
//    debugger;
    val.isvalid = true;
    
    if( vals != null )
    {
       // loop through all validators for this control, but skip this one (the AlertValidator)
       // if multiple alertvalidators are specified we will probably get a deadly embrace
       for (i = 0; i < vals.length; i++) 
       {
           var xID = vals[i].getAttribute("id");
           if( !(sID == xID ) )
           {
              if (!vals[i].isvalid) {
                  val.isvalid = false;
                  break;;
              }
           }
      }
   }
//      AlertValidatorUpdateDisplay(val);

   return val.isvalid;

}

var over='';var focus='';
function tb_Over(obj,color){over=obj.id;if(over!=focus||focus==''){obj.style.borderColor=color;}}
function tb_Focus(obj,color){focus=obj.id;obj.style.borderColor=color;}
function tb_Out(obj,color){if(over!=focus||focus==''){obj.style.borderColor=color;}over = '';}
function tb_Blur(obj,color){focus='';obj.style.borderColor=color;}
function tb_NumOnly(event){try{var key;if(event){if(event.which)key=event.which;else if(event.keyCode)key=event.keyCode;else if(event.charCode)key=event.charCode;}
if(key){if(key>=0x20&&key<=0x7F){if(key<0x30||key>0x39)return false;}}}catch(ex){alert(ex);}return true;}
function tb_NoSpaces(event){try{var key;if(event){if(event.which)key=event.which;else if(event.keyCode)key=event.keyCode;else if(event.charCode)key=event.charCode;}
if(key){if(key==0x20){return false;}}}catch(ex){alert(ex);}return true;}
function tb_AllCaps(event){try{var key;if(event){if(event.which)key=event.which;else if(event.keyCode)key=event.keyCode;else if(event.charCode)key=event.charCode;}if(key){if(key>=0x61&&key<=0x7A){key=key-0x20;if(event.which)   event.which=key;else if(event.keyCode)   event.keyCode=key;else if(event.charCode)   event.charCode=key;}}}catch(ex){alert(ex);}return true;}

//enables/disables all visible validators
function EnableValidators(enable)
{
    if ((typeof(Page_Validators) != "undefined") && (Page_Validators != null)) {
        var i;
        for (i = 0; i < Page_Validators.length; i++) {
            var val = Page_Validators[i];
//            if( (typeof(val.isAlert) == "undefined") || val.isAlert!="true")
            {
            val.enabled = enable;
            val.enableclientscript = enable;
            }
        }
        if( enable == false )
         Page_ClientValidate("");
    }
}
// performs grouplike radiobutton control, only valid if all share the same "value" attribute
function syncButtons(id) 
{
   var retval = true;
   var value = null; 
   var element = document.getElementById(id);
   var groupName = null; 
   if (element) 
   {
      groupName = element.value;
   }
   if (groupName == null) 
   {
      return false; 
   }
   var radios = document.getElementsByTagName("input");for (var i=0; i<radios.length; i++) 
   {
      var input = radios[ i ];
      if (input.type == 'radio' && input.value == groupName) 
      {
         if( input == element )
         {
            if( input.checked == 1 )
            {
               retval = false;
               break;
            }
         }
         input.checked = 0;
      }
   }
   element.checked = 1;
   return retval;
}
// requires a common 'value' attribute
function chkSame(id) 
{
   var value = null; 
   var element = document.getElementById(id);
   var groupName = null; 
   var checked = 0;

   if (element) 
   {
      // check the property on the SPAN container
      groupName = element.parentElement.group;
      checked = element.checked;
   }
   if (groupName == null) 
   {
      return null; 
   }
   var radios = document.getElementsByTagName("input");
   for (var i=0; i<radios.length; i++) 
   {
      var input = radios[ i ];
      if (input.type == 'checkbox' && input.parentElement.group == groupName && input.id != element.id) 
      {
         input.checked = checked;
      }
   }
}
// validation for date set greater than or equal to current date
function vnd1(val, args)
{
   try
   {
   args.IsValid = false;

//   var val = document.getElementById('ClientID').value;
   var s1 = val.controltovalidate;
   var s2 = val.LinkedControlToValidate
   if( s2 == null )
      s2 = val.getAttribute("linkedcontroltovalidate");

   var i = s1.lastIndexOf("_");
   var s = s1.substr( 0, i+1 );
   var m, y;
   if( s1.indexOf("Month") >= 0)
   {
      m = document.getElementById(s1);
      y = document.getElementById(s + s2);
   }
   else
   {
      y = document.getElementById(s1);
      m = document.getElementById(s + s2);
   }
   var d = new Date();
   var dm = m.options[m.selectedIndex].value;
   var dy = y.options[y.selectedIndex].value;
   var cm = d.getMonth()+1;
   var cy = d.getYear();
   
   // non-IE browsers return no. years since 1900
   if( cy < 300 )
      cy = cy + 1900;
      
   if( cy < dy ) // current year less than tested year is always valid
   {
      args.IsValid = true;
   }
   else if( cy == dy )// same year check the month
   {
      args.IsValid = dm >= cm;
   }    
   }
   catch(ex)
   {
     // alert(ex);
   }
}
// textbox changed
function vnd2(tb, rdoID)
{
   try
   {
      if(syncButtons(rdoID)) 
         EnableValidators(true);
   }
   catch(ex)
   {
     // alert(ex);
   }
};
function vnd3(tb, rdoID)
{
   if( event.propertyName == "value" )
      vnd2(tb, rdoID);
}
// textbox changed
function vnd4(tb, rdoID, chkID)
{
   if( event.propertyName == "value" )
      vnd5(tb, rdoID, chkID);
}
function vnd5(tb, rdoID, chkID)
{
   try
   {
      if(syncButtons(rdoID)) 
         EnableValidators(true);
      var chk = document.getElementById(chkID);
      if( chk != null )
         chk.checked = false;
   }
   catch(ex)
   {
     // alert(ex);
   }
};
// called by the UseSame checkbox on Shipping checkout
function vnd6(rdoID, chkID)
{
   try
   {
      var chk = document.getElementById(chkID);
      if( chk != null && chk.checked )
      {
         var rdo = document.getElementById(rdoID);
         if( rdo != null )
            rdo.checked = false;
         EnableValidators(false);
      }
   }
   catch(ex)
   {
     // alert(ex);
   }
};

// called by Checkout to set querystring parameter "pf=1" which instructs the next page load to switch
// to the print format master page
function setPF(btn)
{
   try
   {
      if( window.opener )
         window.opener.location.href += "?pf=1";
      else
         window.location.href += "?pf=1";
   }
   catch(ex)
   {
     alert(ex);
   }
};

/* validates credit card number and type */
function cc_validate(val, args)
{
   args.IsValid = false;
   try
   {
      var number = args.Value;


     // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
     var number=number.replace(/\D/g, '');
     
     //we currently do not accept JCB 
     var ccType = cc_check(number);
     if (ccType>=1 && ccType<=6)
     {
        // got a good cc format, now verify the checksum
        args.IsValid = luhn_check(number);
        if( args.IsValid && (ccType < 3) )
        {
           alert("We currently accept Visa, Mastercard, AMEX and Discover, please use one of those cards.");
           args.IsValid = false; 
           return;
        }
     }
  }
  catch(ex)
  {
    alert(ex);
  }
  if( !args.IsValid )
      alert("The credit card number is not valid, please try again.");
  
}

/* validates the number format and returns
   0 - Invalid format/card
   1 = JCB
   2 = Diner's Club
   3 = AMEX
   4 = Visa
   5 = Mastercard
   6 = Discover
*/
function cc_check(number)
{
   var ccType = 0;

  // Set the string length and parity
  var number_length=number.length;

/*
Visa: ^4[0-9]{12}(?:[0-9]{3})?$ All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13. 
MasterCard: ^5[1-5][0-9]{14}$ All MasterCard numbers start with the numbers 51 through 55. All have 16 digits. 
American Express: ^3[47][0-9]{13}$ American Express card numbers start with 34 or 37 and have 15 digits. 
Diners Club: ^3(?:0[0-5]|[68][0-9])[0-9]{11}$ Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits. There are Diners Club cards that begin with 5 and have 16 digits. These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard. 
Discover: ^6(?:011|5[0-9]{2})[0-9]{12}$ Discover card numbers begin with 6011 or 65. All have 16 digits. 
JCB: ^(?:2131|1800|35\d{3})\d{11}$ JCB cards beginning with 2131 or 1800 have 15 digits. JCB cards beginning with 35 have 16 digits.*/

   var n1 = number.charAt(0);
   switch (n1)
   {
      case '1' :
         // JCB
         n1 = number.substr(0,4);
         if (n1=="1800" && number_length==15)
            ccType = 1;
         break;
      case '2' :
         // JCB
         n1 = number.substr(0,4);
         if (n1=="2131" && number_length==15)
            ccType = 1;
         break;
      case '3' :
         n1 = number.substr(0,2);
         if( n1=="30" ) {
            // possible Diner's club
            n1 = number.substr(2,1);
            if ((n1>=0 && n1<=5) && number_length==14)
               ccType = 2;
            break;
         }
         else if(n1=="34") {
            // AMEX
            if (number_length==15)
               ccType = 3;
            break;
         }
         else if(n1=="35") {
            // JCB
            if (number_length==16)
               ccType = 1;
            break;
         }
         else if(n1=="36") {
            // Diner's Club
            if (number_length==14)
               ccType = 2;
            break;
         }
         else if(n1=="37") {
            // AMEX
            if (number_length==15)
               ccType = 3;
            break;
         }
         else if(n1=="38")
         {
            // Diner's Club
            if (number_length==14)
               ccType = 2;
            break;
         }
         break;
      case '4' :
         // Visa
         if (number_length==13 || number_length==16)
            ccType = 4;
         break;
      case '5' :
         // Mastercard
         n1 = number.substr(0,2);
         if (( n1>=51 && n1<=55 ) && number_length==16)
            ccType = 5;
         break;
      case '6' :
         // Discover
         if( number_length==16 ) {
            n1 = number.substr(0,2)
            if( n1=="65" )
               ccType = 6;
            else {
               n1 = number.substr(0,4)
               if( n1=="6011" )
                  ccType = 6;
            }
         }
         break;
      default :
         break;
   }
   return ccType;
}

/* Luhn algorithm number checker - (c) 2005-2008 shaman - www.planzero.org *
 * This code has been released into the public domain, however please      *
 * give credit to the original author where possible.                      */

function luhn_check(number) {
//<![CDATA[

  // Set the string length and parity
  var number_length=number.length;
  var parity=number_length % 2;

  // Loop through each digit and do the maths
  var total=0;
  for (i=0; i < number_length; i++) {
    var digit=number.charAt(i);
    // Multiply alternate digits by two
    if (i % 2 == parity) {
      digit=digit * 2;
      // If the sum is two digits, add them together (in effect)
      if (digit > 9) {
        digit=digit - 9;
      }
    }
    // Total up the digits
    total = total + parseInt(digit);
  }

  // If the total mod 10 equals 0, the number is valid
  if (total % 10 == 0) {
    return true;
  } else {
    return false;
  }

//]]>
}
