
   var cancelButton = false; // If cancel button pressed, don't do edit checks
   
   // Only allow numerals, backspace and delete to be entered in a field
   // If dec is true, allow a decimal point
    function NumberFilter(control, e, dec)
    {
          // Check for a Firefox control key
          if (isNaN(e.keyCode) != true && isNaN(e.charCode) != true && e.charCode == 0)
          {
            if (e.keyCode > 34 && e.keyCode < 40)
                return true;  // directional key
          }
          
          dec = typeof(dec) != 'undefined' ? dec : false;
                 
          var unicode;
          try
          {   
              unicode = e.keyCode;  //IE?
              if (unicode == 0)
                 unicode = e.charCode; // Not IE
          }
          catch(err)
          {
            return false;
          }
          if (unicode == 8 || unicode == 9) // backspace, tab
             return true;
          if (dec == true && unicode == 46)
            return true;
          if(unicode < 48 || unicode > 57)
            return false;
                      
          return true;
    }
    
    ////////////////////////////
    // Close the current window
    ////////////////////////////
    function closeWindow()
    {
        window.close();
    }
    
    // Allow all threshold characters:
    //     digits, equal, comma, dash, decimal point, greater than
    function thresholdFilter(control, e)
    {
        // If a numeral or decimal point, we are ok.
        if (NumberFilter(control, e, true))
            return true;

          var unicode;
          try
          {   
              unicode = e.keyCode;  //IE?
              if (unicode == 0)
                 unicode = e.charCode; // Not IE
          }
          catch(err)
          {
            return false;
          }
          if (unicode == 61 || unicode == 45 || unicode == 44 || unicode == 62 ) // =-,>
             return true;
            
          return false;            
    }
    
    // Edit check and of the shipping pages
    function shipCheck(form)
    {
        if (cancelButton == true) // Don't validate a cancel
            return true;
            
        if (validateZip(form.tbZip.value) == false)
            return false;
        if (validateDimensions(form.tbTLen.value, form.tbTWidth.value, form.tbTHeight.value, 'Typical') == false)
            return false;
        if (validateDimensions(form.tbLLen.value, form.tbLWidth.value, form.tbLHeight.value, 'Large') == false)
            return false; 
            
        var i;
        for (i = 0; i < form.elements.length; i++)
        {
            if ( form.elements[i].name.indexOf('tbPreOff') > -1)
            {
                if (validateMoney(form.elements[i].value, form.elements[i].name) == false)
                    return false;
            }
            if ( form.elements[i].name.indexOf('tbPostOff') > -1) 
            {
                if (validateMoney(form.elements[i].value, form.elements[i].name) == false)
                    return false;            
            } 
            if ( form.elements[i].name.indexOf('tbMinCharge') > -1) 
            {
                if (validateThreshold(form.elements[i].value, form.elements[i].name) == false)
                    return false;            
            }                                           

        }           
        return true;
        
    }
    
    // Make sure zip is 5 digits
    function validateZip(zip)
    {
        if (zip.length != 5)
        {
            alert('Origin Zip Code must be specified and be 5 digits.');
            return false;
        }
        
        return true;
    }
    
    // If any package dimension is set, all must be set
    function validateDimensions(l,w,h,name)
    {
        if (l.length == 0 || w.length == 0 || h.length == 0)
        {
            if  (l.length != 0 || w.length != 0 || h.length != 0)
            {
                alert('If any package dimensions are set, all must be set.\n Check dimensions for ' + name);
                return false;
            }
            return true;
        }
        return true;
    }
    
    // Validate a money amount
    function validateMoney(amount, name)
    {
        var len = amount.length;
        if (len == 0)
            return true;
        
        var dot = amount.indexOf('.');
             
        if (dot > -1)
        {
            if (len - dot != 2 && len - dot != 3)
            {
                alert('Decimal point is incorrect in ' + controlName(name) +
                      ' number '  + controlRow(name) + '\n' + amount + '\nMaximum 2 digits after decimal point.');
                return false;
            }
            
            if (amount.lastIndexOf('.') != dot)
            {
                alert('More than one decimal point in ' + controlName(name) +
                      ' number '  + controlRow(name) +  '\n' + amount);
                return false;
            }
        }
        
        return true;
    }
    
    // Given a generated control name from a .Net repeater, find
    // the actual name
    function controlName(name)
    {
        var dollar = name.lastIndexOf('$');
        name = name.substring(dollar + 1);
        if (name.substring(0,2) == 'tb')
            return name.substring(2);
        return name;
    }
 
    // Given a generated control name from a .Net repeater, find
    // the row number
    function controlRow(name)
    {
        var dollar = name.indexOf('$ctl');
        name = name.substring(dollar + 4);
        dollar = name.indexOf('$');
        var number = name.substring(0, dollar);
        if (number.indexOf('0') == 0) // get rid of leading zero
            return number.substring(1);
        else
            return number;
    }
    
    // Thresholds are in the form...
    //      1-5=2.99,5.01-7=3.99,>7=10.99
    // Do complete validation of the threshold
    function validateThreshold(threshold, name)
    {
        if (threshold.length == 0)
            return true;
        
        try
        {
            var equations = threshold.split(",");
            
            var lastRight = 0; // the last right side of a range            
                   
            for (i = 0; i < equations.length; i++)
            {                
                if (equations[i].indexOf('=') == -1)
                {
                    alert('Invalid threshold equation in ' + controlName(name) +
                          ' number '  + controlRow(name) + ':\n' + 
                          'Missing = sign\n' + equations[i]);                
                    return false;
                }
                
                if (equations[i].indexOf('=') != equations[i].lastIndexOf('='))
                {
                    alert('Invalid threshold equation in ' + controlName(name) +
                          ' number '  + controlRow(name) + ':\n' + equations[i]);
                    return false;                
                }
                                
                var sides = equations[i].split("=");
                var left = sides[0];
                var right = sides[1];
                               
                if (i == equations.length - 1 && left.indexOf('>') != 0)
                {
                    alert('Invalid threshold equation in ' + controlName(name) +
                          ' number '  + controlRow(name) + ':\n' + equations[i] +
                          '\nLast term must begin with >');
                    return false;                
                }
                
                if (left.length == 0 || right.length == 0 ||
                    right.indexOf('.') != right.lastIndexOf('.') ||
                    left.indexOf('-') != left.lastIndexOf('-') ||
                    right.indexOf('-') != -1 ||
                    right.indexOf('>') != -1 ||
                    left.indexOf('>') != left.lastIndexOf('>') ||
                    left.indexOf('>') > 0 ||
                    left.indexOf('>') == 0 && (i != equations.length - 1 || left.indexOf('-') != -1))                                    
                {
                    alert('Invalid threshold equation in ' + controlName(name) +
                          ' number '  + controlRow(name) + ':\n' + equations[i]);
                    return false;
                }
                
                if (left.indexOf('>') == -1)
                {
                    var range = left.split("-");
                    if (range.length != 2 || Number(range[0]) >= Number(range[1]))
                    {
                        alert('Invalid threshold equation in ' + controlName(name) +
                          ' number '  + controlRow(name) + ':\n' + equations[i] + ' : ' + left);
                        return false;                    
                    }
                    
                    var diff = range[0] - lastRight;
                    diff = Math.round(diff * 100) / 100; // for some reason, some rounding needed
                    if (lastRight != 0 && diff != .01)
                    {
                        alert('Invalid threshold equation in ' + controlName(name) +
                          ' number '  + controlRow(name) + ':\nGap in range' +
                          equations[i] + ' : ' + lastRight  + ' : ' + range[0]);
                        return false;                                        
                    }
                                       
                    lastRight = range[1];
                    
                }
                else  // last term with >
                {
                    var amt = left.substring(1);  // get rid of >
                    var diff = amt - lastRight;
                    diff = Math.round(diff * 100) / 100; // for some reason, some rounding needed
                    if (lastRight != 0 && diff != 0)
                    {
                        alert('Invalid threshold equation in ' + controlName(name) +
                          ' number '  + controlRow(name) + ':\nGap in range' +
                          equations[i] + ' : ' + lastRight  + ' : ' + amt);
                        return false;                                        
                    }
                    
                }
                                               
            }
        }
        catch(err)
        {
            alert(err);
            return false;
        }
        
        return true;
    }
    
/////////////////////////////////////////////////////////////////////////
// If this function is set to the keydown event for a textbox or such,
// it will check for the enter key an click the argument button
/////////////////////////////////////////////////////////////////////////
function setDefaultButton(e, btn)
{
        var evt = e ? e : window.event;
       // process only the Enter key
        if (evt.keyCode == 13)
        {
            // cancel the default submit
            evt.cancel = true;
            // submit the form by programmatically clicking the specified button
            btn.click();
            return false;
        }
}    
    
///////////////////////////////////////////////////
// Make sure email at least looks like an email
///////////////////////////////////////////////////
function validateEmail(email)
{   
    var at = email.indexOf('@');
    var dot = email.lastIndexOf('.');
    
    if (at < 0 || at != email.lastIndexOf('@') || dot < 0 || dot < at)
        return false;
       
    return true;
}    

////////////////////////////////////////
// create a cookie
////////////////////////////////////////
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

///////////////////////////////////////
// read a cookie
///////////////////////////////////////
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

////////////////////////////////////////
// delete a cookie
////////////////////////////////////////
function eraseCookie(name) {
	createCookie(name,"",-1);
}