﻿//Open the default e-mail editor with the referral page in the message
function emailPage(e_subject, e_body) {
  var mail_str = "mailto:?subject=" + 
    escape(e_subject) + "&body=" + escape(e_body) + " " + escape(location.href);

  location.href = mail_str;
}

// Checks the validity of an email address
function checkEmail(email) {
  var at="@";
  var dot=".";
  var lat=email.indexOf(at);
  var lstr=email.length;
  var ldot=email.indexOf(dot);
  var l1dot=email.lastIndexOf(dot);

  if (email.indexOf(at)==-1&&ldot==l1dot){
    return false;
  }
  if ((email.indexOf(at)==-1&&ldot==l1dot)|| email.indexOf(at)==0 || email.indexOf(at)==lstr){
   return false;
  }
  if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr){
    return false;
  }
  if (email.indexOf(at,(lat+1))!=-1){
    return false;
  }
  if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot){
    return false;
  }
  if (email.indexOf(dot,(lat+2))==-1){
    return false;
  }
  if (email.indexOf(" ")!=-1 || email.indexOf("\\")!=-1 || email.indexOf(":")!=-1  || email.indexOf(";")!=-1 || email.indexOf("\"")!=-1){
    return false;
  }
  if (email.indexOf(",")!=-1 || email.indexOf("<")!=-1 || email.indexOf(">")!=-1  || email.indexOf("(")!=-1 || email.indexOf(")")!=-1 || email.indexOf("*")!=-1){
    return false;
  }
  return true;			
}

// Creates a popup window
function popupWindow(mypage, myname, w, h, scroll) {
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  var winprops = 'height=' + h + ',width=' + w + ',top=' + wint + ',left=' + winl + ',scrollbars=' + scroll + ',resizable,status';
  var win = window.open(mypage, myname, winprops);
  win.focus();
}

// Checks the size of a field value against a user defined value
function checkLength(form, fieldName, size) {
  var value = getSelectedValue(form, fieldName);

  if (value.length > size) {
    return false;
  }

  return true;
}

// Checks an array of required fields
function checkRequiredFields(form, fieldNames, labels, message) {
  var isValid = true;

  for (var i = 0; i < fieldNames.length; i++) {
    var fieldName = fieldNames[i];
    var fieldLabel = labels[i];
  
    if (!checkRequiredField(form, fieldName)) {
      isValid = false;
      message += "\n" + fieldLabel;
    }
  }

  if (!isValid) {
    alert(message);
  }

  return isValid;
}

// Checks a single required field
function checkRequiredField(form, fieldName) {
  var hasValue = false;

  if (getSelectedValue(form, fieldName) != "") {
    hasValue = true;
  }

  return hasValue;
}

// Prepopulates form elements based on type
function prepopulateForm(form, fields, values) {
  for (var i = 0; i < fields.length; i++) {
    var fieldName = fields[i];
    var fieldValue = values[i];

    setSelectedValue(form, fieldName, fieldValue);      
  }
}

// Trims the leading and trailing spaces from a text input field
function trimField(form, fieldName) { 
  var field = form.elements[fieldName];
		
  field.value = trimValue(field.value);
}

// Trims the leading and trailing spaces from a string
function trimValue(value) {
  var reg1 = /^(\s*)(.*[^\s])(\s*)$/;
  var reg2 = /^(\s*)$/;

  if (reg1.test(value)) {  
    return value.replace(reg1, "$2");
  }
  else if (reg2.test(value)) {
    return "";
  }
  else {
    return value;
  }
}

// Get the element type of the field or array of fields
function getElementType(field) {
  var elmType = "";

  if (field != undefined) {
    elmType = field.type;

    if (elmType == undefined && field[0]) {
      elmType = field[0].type;
    }
  }
  
  return elmType;
}

// Determines if a string value is a true array
function isArrayVal(value) {
  var objType = getType(value);

  if (objType == "Array") {
    return true;
  }
  else {
    return false;
  }
}

// Returns the object type
function getType(object) {
  if (object == null) {
    return "undefined";
  }

  var id = object.constructor.toString();

  var index1 = id.indexOf (" ");
  var index2 = id.indexOf ("(");

  return id.substring (index1 + 1, index2);
}

// Selects a form field
function selectField(form, fieldName) {
  var field = form.elements[fieldName];
  var elmType;

  if (field != undefined) {
    elmType = getElementType(field);

    if (elmType == "text" || elmType == "textarea" || elmType == "password" || elmType == "file") {
      if (field[0]) {
        field = field[0];
      }

      field.focus();
      field.select();
    }
    else if (elmType == "select-one" || elmType == "select-multiple") {
      field.focus();
    }
    else if (elmType == "radio" || elmType == "checkbox" || elmType == "submit") {
      if (field[0]) {
        field = field[0];
      }

      field.focus();
    }
  }
}


// Sets the selected value for form element this could be a single value or an array
function setSelectedValue(form, fieldName, value) {
  var field = form.elements[fieldName];
  var elmType;

  if (field != undefined) {
    elmType = getElementType(field);
   
    if (elmType == "hidden" || elmType == "text" || elmType == "textarea" || elmType == "password" || elmType == "file" || elmType == "submit") {
      setTextValue(field, value);
    }
    else if (elmType == "select-one" || elmType == "select-multiple") {
      setSelectValue(field, value);
    }
    else if (elmType == "checkbox") { 
      setCheckboxValue(field, value);	
    }
    else if (elmType == "radio") { 
      setRadioValue(field, value);	
    }
  }
}

// Sets hidden, text, textarea, password, file, submit elements
function setTextValue(field, value) {
  if (field[0]) {
    for (var i = 0; i < field.length; i++) {
      if (isArrayVal(value) && value[i]) {
        field[i].value = value[i];
      }
      else if (!isArrayVal(value)) {
        field[0].value = value;
        break;
      }
    }
  }
  else if (isArrayVal(value)) {
    field.value = value[0];
  }
  else {
    field.value = value;
  }
}

// Sets select and multi select box elements
function setSelectValue(field, value) {
  if (field[0]) {
    for (var i = 0; i < field.length; i++) {
      if (isArrayVal(value)) {
        for (var j = 0; j < value.length; j++) {
          if (field[i].value == value[j]) {
            field[i].selected = true;
          }
        }
      }
      else {
        if (field[i].value == value) {
          field[i].selected = true;
        }
      }
    }
  }
}

// Sets checkbox elements
function setCheckboxValue(field, value) { 
  if (field[0]) {
    for (var i = 0; i < field.length; i++) {
      if (isArrayVal(value)) {
        for (var j = 0; j < value.length; j++) {
          if (field[i].type == "checkbox" && field[i].value == value[j]) {
            field[i].checked = true;
          }
        }
      }
      else {
        if (field[i].type == "checkbox" && field[i].value == value) {
          field[i].checked = true;
        }
      }
    }
  }
  else {
    if (isArrayVal(value)) {
      for (var i = 0; i < value.length; i++) {
        if (field.type == "checkbox" && field.value == value[i]) {
          field.checked = true;
        }
      }
    }
    else {
      if (field.type == "checkbox" && field.value == value) {
        field.checked = true;
      }
    }
  }
}

// Sets radio button elements
function setRadioValue(field, value) {
  var isNotSelected = true;
 
  if (field[0] && !isArrayVal(value)) {
    for (var i = 0; i < field.length && isNotSelected; i++) {
      if (field[i].value == value) {
        field[i].checked = true;
        isNotSelected = false;
      }
    }
  }
  else if (!isArrayVal(value)) {
    if (field.value == value) {
      field.checked = true;
    }
  }
}

// Returns the selected value for form element this could be a single value or an array
function getSelectedValue(form, fieldName) {
  var field = form.elements[fieldName];
  var sValue;
  var elmType;

  if (field != undefined) {
    elmType = getElementType(field);
   
    if (elmType == "hidden" || elmType == "text" || elmType == "textarea" || elmType == "password" || elmType == "file" || elmType == "submit") {
      sValue = getTextValue(field);
    }
    else if (elmType == "select-one" || elmType == "select-multiple") {
      sValue = getSelectValue(field);
    }
    else if (elmType == "radio" || elmType == "checkbox") { 
      sValue = getRadioCheckValue(field);	
    }
  }

  if (sValue == undefined) {
    sValue = "";
  }
  else if (isArrayVal(sValue) && sValue.length == 1) {
    sValue = sValue[0];
  }
  
  return sValue;
}

// Returns the value(s) of hidden, text, textarea, password, file, and submit fields
function getTextValue(field) {
  var sValue;

  if (field[0]) {
    sValue = new Array();

    for (var i = 0; i < field.length; i++) {
      if (trimValue(field[i].value).length != 0) {
        sValue[sValue.length] = trimValue(field[i].value);
      }
    }
  }
  else {
    sValue = "";

    if (trimValue(field.value).length != 0) {
      sValue = trimValue(field.value);
    }
  }
  
  return sValue;
}

// Returns the selected select box or multiple select box value(s)
function getSelectValue(field) {
  var sValue;
   
  if (field[0]) {
    sValue = new Array();

    for (var i = 0; i < field.length; i++) {
      if (field[i].selected && trimValue(field[i].value).length != 0) {     
        sValue[sValue.length] = trimValue(field[i].value);
      }
    }
  }
  else {
    sValue = "";

    if (field.selected && trimValue(field.value).length != 0) {     
      sValue = trimValue(field.value);
    }
  }

  return sValue;  
}

// Returns the selected radio button or check box value(s)
function getRadioCheckValue(field) {
  var sValue;

  if (field[0]) {
    sValue = new Array();

    for (var i = 0; i < field.length; i++) {
      if (field[i].checked && trimValue(field[i].value).length != 0) {
        sValue[sValue.length] = trimValue(field[i].value);
      }
    }
  }
  else {
    sValue = "";

    if (field.checked && trimValue(field.value).length != 0) {
      sValue = trimValue(field.value);
    }
  }

  return sValue;
}

// Formats the US currency
function formatUSCurrency(amount, hasCents) {
  var strValue = new String(amount);
  var strDec = "";

  var numReg = /^(\d+)(\.\d{2})?$/;

  if (numReg.test(strValue)) {
    var commaReg  = /(\d+)(\d{3})/;

    strDec = strValue.replace(numReg, '$2');
    strValue = strValue.replace(numReg, '$1');
    
    while (commaReg.test(strValue)) {
      strValue = strValue.replace(commaReg, '$1,$2');
    }

    strValue = "$" + strValue;

    if (hasCents) {
      strValue += strDec;
    }
  }

  return strValue;
}

// Checks the required fields (supports the old function name)
function checkAllRequired(form, fields, labels, message) {
  return checkRequiredFields(form, fields, labels, message);
}

// Checks a required field (supports the old function name)
function checkRequired(form, field, label, message) {
  if (!checkRequiredField(form, field)) {
    alert(label + " " + message);
    return false;
  }
  else {
    return true;
  }
}

// Returns the selected value for a radio button group (supports the old function name)
function getSelectedRadioValue(form, field) {
  return getSelectedValue(form, field);
}

// Returns the selected value for a select box single select (supports the old function name)
function getSelectedDropDownValue(form, field) {
  return getSelectedValue(form, field);
}

// Selects a text field (supports the old function name)
function selectTextField(form, field) {
  selectField(form, field);
}

// Removes all spaces from a string
function deleteSpaces(string) {
	var finalString = "";
	string = '' + string;
	splitString = string.split(" ");
	for(i = 0; i < splitString.length; i++)
		finalString += splitString[i];
	return finalString;
}

//check if a string contains PO Box or Post office box in all its variations and returns true if found
function containsPO(address){
	address = deleteSpaces(address);
	var POString = new Array(6);
	POString[0] = "POBOX";
	POString[1] = "P.OBOX";
	POString[2] = "PO.BOX";
	POString[3] = "P.O.BOX";
	POString[4] = "POSTOFFICEBOX";
	POString[5] = "POST.OFFICE.BOX";
	for(i=0;i<POString.length;i++)
		if(address.toUpperCase().indexOf(POString[i]) > -1)
			return true;
	return false;
}

