var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ) {
		//expires = expires * 1000 * 60 * 60 * 24;
		expires = expires * 1000 * 60;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// --------------------------------------------

function trim(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

// --------------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

function setFocusDelayed() {
  global_valfield.focus();
}

function setfocus(valfield) {
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}

// --------------------------------------------
//						validateArray
// Validate if value is in passed in array
// Returns false if it's in the array
// --------------------------------------------

function validateArray(valfield, theArray, theAlert) {
	var theValue=trim(valfield.value);
	if (theValue == "") {
		alert("\n" + theAlert + " is required");
		setfocus(valfield);
		return false;
	}
	for (var i=0;i<theArray.length;i++) {
		var theTest=trim(theArray[i]);
		if (theValue == theTest) {
			alert("\n" + theAlert + " already exists");
			valfield.value='';
			setfocus(valfield);
			return false;
		}
	}
	return true;
}

function setFieldValue(valfield, theValue) {
	valfield.value=theValue;
	Set_Cookie(valfield.name, theValue, 15);
	return true;
}

function getFieldValue(valfield) {
	return valfield.value;
}

// --------------------------------------------
//            validateField
// Validate if something has been entered
// Returns true if so
// --------------------------------------------

function validateField(valfield, theAlert) {
	if(valfield.value == "") {
		alert("\n" + theAlert + " is required");
		setfocus(valfield);
		return false;
	}
	return true;
}

// --------------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so
// --------------------------------------------

function validateEmail(valfield) {
	var valid=false;
  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    alert("\nNot a valid e-mail address");
    setfocus(valfield);
    return false;
  }
  return true;
}

// --------------------------------------------
//            validateSelect
// Validate if something has been entered
// Returns true if so
// --------------------------------------------

function validateSelect(valfield, theAlert) {
	var selindex=valfield.selectedIndex;
	if(selindex == 0) {
		alert("\n"+ theAlert + " is required");
		setfocus(valfield);
		return false;
	}
	return true;
}

// Radio Button Validation
// copyright Stephen Chapman, 15th Nov 2004,14th Sep 2005
// you may copy this function but please keep the copyright notice with it
function validateButton(btn, desc) {
  var cnt = -1;
  for (var i=btn.length-1; i > -1; i--) {
      if (btn[i].checked) {cnt = i; i = -1;}
  }
  if (cnt > -1) {
  	return true;
  }
  if(desc == 'offices') {
  	alert("\nYou must select the office you work from");
  	return false;
  } else if(desc == 'newdb') {
  	alert("\nYou must select whether to add to or replace your existing database")
  }
}

// ----------------------------------------------
//							validateCheckBox
// Validate whether the check box has be checked
// ----------------------------------------------
function validateCheckBox(valfield, desc) {
	if(!valfield.checked) {
		if(desc == 'agreetocharge') {
    	alert ("\nYou must approve the charge to your Credit Card\nin order to continue");
    	return false;
		}
	}
	return true
}

// ----------------------------------------------
//							textCounter
//	Count the number of characters being input
// ----------------------------------------------
function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		// if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	} else {
		countfield.value = maxlimit - field.value.length;
	}
}

// ----------------------------------------------
//							toggleLayer
// Function to show/hide divs
// ----------------------------------------------
function toggleLayer(oldLayer, newLayer) {
	var style2 = document.getElementById(oldLayer).style;
	var style3 = document.getElementById(newLayer).style;
	style2.display = "none";
	style3.display = "block";
}

// ----------------------------------------------
//							togglePage
// Function to show/hide divs
// ----------------------------------------------
function togglePage(newLayer) {
	var style1 = document.getElementById('page1').style;
	var style2 = document.getElementById('page2').style;
	var style3 = document.getElementById('page3').style;
	var style4 = document.getElementById('page4').style;
	style1.display = "none";
	style2.display = "none";
	style3.display = "none";
	style4.display = "none";
	// Now display the correct page
	var style = document.getElementById(newLayer).style;
	style.display = "block"	;
}
// ----------------------------------------------
//							toggleRow
// Function to show/hide divs
// ----------------------------------------------
function toggleRow(theForm, theID) {
	if(theID == 'dataselect') {
		var cnt=-1;
		for (var i=theForm.dataup.length-1; i > -1 ; i--) {
			if(theForm.dataup[i].checked) {
				cnt = i;
			}
		}
		var style2 = document.getElementById(theID).style;
		if(theForm.dataup[cnt].value == 3) {
			style2.display = "block";
		} else {
			style2.display = "none";
		}
	}
}

function isValidCreditCard(form, type, ccnum) {
   // get rid of any spaces in number
   ccnum = ccnum.split(" ").join("");
   if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Mastercard") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Discover Card") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Amex") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "Diners Club") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)) {
   	alert('Credit Card Number Invalid for Credit Card Type.\nPlease Check your Card Number and re-enter it.');
   	form.cc_number.focus();
   	return false;
   }
   // Remove all dashes for the checksum checks to eliminate negative numbers
   ccnum = ccnum.split("-").join("");
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}

/*************************************************************************\
boolean isExpiryDate([int year, int month])
return true if the date is a valid expiry date,
else return false.
\*************************************************************************/
function isExpiryDate() {
	var argv = isExpiryDate.arguments;
	var argc = isExpiryDate.arguments.length;
	theForm = argv[0];
	year = argc > 0 ? argv[1] : this.year;
	month = argc > 1 ? argv[2] : this.month;
	
	if (!isNum(year+"")) {
		return false;
	}
	if (!isNum(month+"")) {
		return false;
	}
	today = new Date();
	expiry = new Date(year, month);
	if (today.getTime() > expiry.getTime()) {
		alert("This card has already expired.");
		theForm.cc_month.focus();
		return false;
	} else {
		return true;
	}
}

/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
function isNum(argvalue) {
	argvalue = argvalue.toString();
	
	if (argvalue.length == 0) {
		return false;
	}
	for (var n = 0; n < argvalue.length; n++) {
		if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9") {
			return false;
		}
	}
	return true;
}
