// used to toggle the colour of a row
function toggleRow(field,index) {
  var obj = document.getElementById("row_"+index).style
  if (field.checked) {
    obj.backgroundColor = "#ffe5ad";
  }
  else {
    obj.backgroundColor = "";
  }
}




var split_char = "\x1e"; function mungeFieldValues(form,texts,options) { var munge = "";
	// get text fields
	if (texts != "") {
		var textList = texts.split(",");
		for (i=0; i<textList.length; i++) { munge = munge + textList[i] + "=" + form.elements[textList[i]].value + split_char; }
	}

	// get option list
	if (options != "") {
		var optionList = options.split(",");
		for (i=0; i<optionList.length; i++) {
			var value;
			if (form.elements[optionList[i]].options != null) {
				value = form.elements[optionList[i]].options[form.elements[optionList[i]].selectedIndex].value;
				if (value == '') { value = form.elements[optionList[i]].options[form.elements[optionList[i]].selectedIndex].text; }
			}
			else {
				value =  form.elements[optionList[i]].value;
			}
			munge = munge + optionList[i] + "=" + value + split_char;
		}
	}
	return munge;
}


function isValidNumber(tmp) {
  tmp = replace(tmp," ","");
  return !(isNaN(tmp));
}

function isValidPhonenumber(tmp) {
  tmp = replace(tmp," ","");
  return !(tmp.length != 10 || isNaN(tmp));
}

function replace(inString,oldText,newText) {
  return (inString.split(oldText).join(newText));
}

function getJustNumber(tmp) {
  tmp = replace(tmp," ","");
	return tmp;
}

// transmitter obect -------------------------------------------------------

function Transmitter(form,rm,texts,options) {
	this.form = form;
	this.rm = rm;
	this.texts = texts;
	this.options = options;
	
	this.sendAndClose = sendAndClose;

	return this;
}


function sendAndClose() {
	var form = this.form;
	var texts = this.texts;
	var options = this.options;

	var munge = "";
	// get text fields
	if (texts != "") {
		var textList = texts.split(",");
		for (i=0; i<textList.length; i++) { munge = munge + textList[i] + "=" + form.elements[textList[i]].value + split_char; }
	}

	// get option list
	if (options != "") {
		var optionList = options.split(",");
		for (i=0; i<optionList.length; i++) {
			var value;
			if (form.elements[optionList[i]].options != null) {
				value = form.elements[optionList[i]].options[form.elements[optionList[i]].selectedIndex].value;
				if (value == '') { value = form.elements[optionList[i]].options[form.elements[optionList[i]].selectedIndex].text; }
			}
			else {
				value =  form.elements[optionList[i]].value;
			}
			munge = munge + optionList[i] + "=" + value + split_char;
		}
	}

	var o = opener.document.forms[0];

	// inform the main window that this is a new contact
	o.rm.value = this.rm;

	// place this data into the main window
	o.subwindow_data.value = munge;

	// submit the main window
	o.submit();

	// wait a while (100 milliseconds), and then close this window (Netscape fix)
	setTimeout('window.close()',100);
}




// validator object --------------------------------------------------------
// use: var myValidator = new Validator(yourForm)

// constructor for our validator object
function Validator(form) {
	this.form = form;
	this.errors = 0;
	this.thisField = "";
	this.message = "";

	this.addError = addError;
	this.showSoftErrors = softErrors;
	this.showErrors = hardErrors;
	this.getForm = getForm;
	this.getField = getField;

	return this;
}

function getForm() {
	return this.form;
}

function getField(field) {
	var form = this.getForm();
	return form.elements[field];	
}


// return true/false after prompting user to fix (any) errors ...
function softErrors() {
	if (this.errors) {
		if (!confirm("Please fix the following errors:\n\n"+this.message+"\n\nPress OK to save anyway, CANCEL to fix errors...")) {

		// the user wants to fix the errors - help them out a bit ...
		if (this.thisField) {
			this.getField(this.thisField).focus();
		}

		// the form is NOT ready for submission
		return true;
		}
	}

	// the form can be submitted ...
	return false;
}


// return false and alert if errors
function hardErrors() {
	if (this.errors) {
		alert("Please fix the following errors:\n\n"+this.message);

		// the user wants to fix the errors - help them out a bit ...
		if (this.thisField) {
			this.getField(this.thisField).focus();
		}

		// the form is NOT ready for submission
		return true;
	}

	// the form can be submitted ...
	return false;
}


// add any errors to the list
function addError(thisMessage, field) {
	this.errors++;
	this.message = this.message + this.errors + ". " + thisMessage + "\n";
	if (this.thisField == "") { this.thisField = field; }                                     // hold the first problem field
}

function isValidExpiry(date) {

  var error = false;
  if (date.length == 5) {
    if ( (date.substr(2,1) == "-")  || (date.substr(2,1) == "/") ) {
      month = date.substr(0,2);
      year = date.substr(3,2);
      if ( !( (!isNaN(month) && month>0 && month<13) && (!isNaN(year) && year>=6) )) { error = true; }
    }
    else { error = true; }
  }
  else { error = true; }

  return !error;

}

function isValidDate(date) {

  var error = false;
  if (date.length == 10) {
    if ( ((date.substr(2,1) == "-") && (date.substr(5,1) == "-" )) || ((date.substr(2,1) == "/") && (date.substr(5,1) == "/" ))) {
      day = date.substr(0,2);
      month = date.substr(3,2);
      year = date.substr(6,4);
      if ( !((!isNaN(day) && day>0 && day<32) && (!isNaN(month) && month>0 && month<13) && (!isNaN(year) && year>1900 && year<2200) )) { error = true;
}

    }
    else { error = true; }
  }
  else { error = true; }

  return !error;



}

function isValidTime(tmp) {
	var valid = false;
	if (tmp.length == 8 && tmp.charAt(2)==":" && tmp.charAt(5)==":") {
		valid = true;
	}
	
	if (tmp.length == 5 && tmp.charAt(2)==":" ) {
		valid = true;
	}

	return valid;
}




var MINUTE = 60 * 1000;
var HOUR = MINUTE * 60;
var DAY = HOUR * 24;
var WEEK = DAY * 7;
var today = new Date();

function preDates(tmp) {
	var signdate = new Date(tmp.substring(6,10),(tmp.substring(3,5)-1),tmp.substring(0,2));
	var diff = (signdate - today) / DAY;
	return (diff<-1);
}


function postDates(tmp) {
	var signdate = new Date(tmp.substring(6,10),(tmp.substring(3,5)-1),tmp.substring(0,2));
	var diff = (signdate - today) / DAY;
	return (diff>=1);
}





//      Credit Card Validator
//      By Mark Morley (mark@islandnet.com)
//
//      This routine analyzes a string to determine if it represents a
//      valid credit card number and if so, returns which type of card
//      it is.  It returns the string "BAD" if the string does not
//      represent a valid credit card, otherwise it returns one of these
//      self explanatory values:
//
//      visa, mastercard, amex, discover, enroute, dinersclub, or jcb
//
//      NOTE: This routine merely indicates if a given string represents
//            a valid card number.  It cannot tell you if that number has
//            actually been assigned to someone!

function cctype(string)
{
   // First we loop through the string, building a reversed copy of the
   // digits as we go.  We strip out any non-digits too, so it doesn't
   // matter if the user used spaces or dashes (or any other garbage).

   rstring = "";
   for( l = string.length - 1; l >= 0; l-- )
   {
      c = string.substr( l, 1 );
      if( c >= "0" && c <= "9" )
         rstring += c;
   }

   // Grab the last 4 digits...

   l4 = rstring.substr( rstring.length - 4, 4 );

   // Special case for "enRoute".  If there are exactly 15 digits and it
   // begins with "2014" or "2149" then it's a valid enRoute number.  They
   // do not use the LUHN formula.

   if( rstring.length == 15 && (l4 == "4102" || l4 == "9412") )
      return "enroute";

   // Using the LUHN formula we calculate a checksum value.  All the other
   // cards we recognise must pass this test.

   sum = 0;
   for( l = 1; l <= rstring.length; l++ )
   {
      c = rstring.substr( l - 1, 1 );
      if( l % 2 == 0 )
      {
         n = 2 * parseInt( c );
         if( n > 9 )
         {
            sum += 1;
            n -= 10;
         }
         sum += n;
      }
      else
         sum += parseInt( c );
   }

   // If the sum is not an even multiple of 10 then it fails.

   if( sum % 10 )
      return "BAD";

   // If the number is 16 digits long and starts with "6011" then it's
   // a Discover card.

   if( rstring.length == 16 && l4 == "1106" )
      return "discover";

   // If the number is 15 digits long and begins with either "2131" or
   // "1800" then it's a JCB card.

   if( rstring.length == 15 && (l4 == "1312" || l4 == "0081") )
      return "jcb";

   // Grab the right most digit...

   l1 = rstring.substr( rstring.length - 1, 1 );

   // If the number is 16 digits long and begins with "3" then it's also a
   // JCB card.

   if( rstring.length == 16 && l1 == "3" )
      return "jcb";

   // If the number is 13 or 16 digits long and begins with "4" then it's
   // a VISA card.

   if( (rstring.length == 13 || rstring.length == 16) && l1 == "4" )
      return "visa";

   // Grab the last two digits...

   l2 = rstring.substr( rstring.length - 2, 2 );

   // If the number is 16 digits long and the first two digits are "51", "52",
   // "53", "54", or "55" then it's a MasterCard.

   if( rstring.length == 16 && (l2 == "15" || l2 == "25" || l2 == "35" || l2 == "45" || l2 == "55" ) )
      return "mastercard";

   // If the number is 16 digits long and the first two digits are "56"
   // BasterCard.

   if( rstring.length == 16 && (l2 == "65" ) )
      return "bankcard";

   // If the number is 15 digits long and it begins with either "34" or "37"
   // then it's an American Express card.

   if( rstring.length == 15 && (l2 == "43" || l2 == "73" ) )
      return "amex";

   // Grab the last 3 digits...

   l3 = rstring.substr( rstring.length - 3, 3 );

   // If the number is 14 digits long and begins with "300", "301", "302",
   // "303", "304", "305", "36", or "38", then it's a Diners Club or
   // Cart Blanche card.

   if( rstring.length == 14 && (l2 == "63" || l2 == "83" || l3 == "003" || l3 == "103" || l3 == "203" || l3 == "303" || l3 == "403" || l3 == "503" ) )
      return "dinersclub";

   // If it's none of those then it's "BAD".

   return "BAD";
}


// function to open a window centred on the screen
function launchCentreAll(url, name, height, width) {
	var str = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;
	if (window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;
		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
		str += ",resizable=yes,scrollbars=yes";
	}
	return window.open(url, name, str);
}

function launchCentreResize(url, name, height, width) {
	var str = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;
	if (window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;
		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
		str += ",resizable=yes";
	}
	return window.open(url, name, str);
}

function launchCentreScroll(url, name, height, width) {
	var str = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;
	if (window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;
		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
		str += ",scrollbars=yes";
	}
	return window.open(url, name, str);
}

function launchCentre(url, name, height, width) {
	var str = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;
	if (window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;
		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
		str += ",scrollbars=no";
	}
	return window.open(url, name, str);
}


function smallSearch(field) {
	eval("var val = document."+field+".value");
	url ="/client/dev/smallSearch?rm=viewSearch&return="+field+"&account="+val;

	launchCentreAll(url, 'smallSearch', 440, 520);
}


function isValidEmail(field) {
	var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.biz)|(\..{2,2}))$)\b/gi);
	return goodEmail;
}

function doRates(field) {

	var sel_destination = field.options[field.selectedIndex].text;
	var table = "";	// contents for div

	if (sel_destination == 'Please select') {
	}
	else {
		var components = rates[sel_destination].split("|");
		table = "<table width=100% >";

		if (sel_destination != "Please select") {
			table = table + "<tr><td colspan=1 align=right><font color=#551133><u>Rates to <b>"+sel_destination+"</b></u></font></td><td colspan=2>&nbsp;</td><tr>";
		}
		for (i=0; i<components.length; i++) {
				var this_rate = components[i].split(",");
				if (this_rate != "") {
					var destination = this_rate[0];
					var rate = this_rate[1];
					table = table + "<tr><td width=70% align=right class='rates_column_heading'>"+destination+"</td><td class='rates_column_value' align='right' >"+rate+"</td><td width=20>&nbsp;</td></tr>";
				}
		}
		table = table + "</table>";
	}

	// draw into div
	document.getElementById("rate").innerHTML = table;
}

