//**********************************************************************
//
//	The McDuffie Group, Ltd. / Platte River Systems, Inc.
//
//	The following programs are the proprietary property of
//	The McDuffie Group, Ltd. and Platte River Systems, Inc.
//	All rights are reserved, including the right to reproduce
//	this source, its object, or portions thereof in any form.
//
//	Copyright (c) 2006-2008 by The McDuffie Group, Ltd.
//	Copyright (c) 2006-2008 by Platte River Systems, Inc.
//	Licensed Program Material.  All Rights Reserved.
//
//**********************************************************************

//**********************************************************************
var Id = '$Id: submit.js,v 1.16 2008/08/29 20:27:01 jt Exp $';
//**********************************************************************

//	MODULE NAME
//
//	submit.js		JavaScript library for the Submit Form
//
//	MODULE DESCRIPION
//
//	  Support code to implement most special features used by the
//	Lead Registration / Submission processor.
//
//
//	FUNCTION / MEMBER LIST
//
//	checkLength		Check that theField.value is within length limits.
//	depositStateList	Reset the StateList SELECT LIST when a new one is sent.
//	getStates		Request a new StateList SELECT LIST for a specified country.
//	doGetStates		onChange handler called from CountryList object.
//	validateSubmit		onSubmit handler to validate all data entered prior to its
//				 being submitted to the server.
//
//	REQUIRE / INCLUDE LIST
//
//	  zxml.js		JavaScript library to equalize differences in XML and
//				 XMLHttp features 


var oXmlHttp1 = null;		// The XMLHttp object for StateList
var sSCode = '';

// List of descriptions used during error checking
// -- Provided by a dynamicly loaded data set

// checkLength(TEXTFIELD theField, INT minlen, INT maxlen, STRING error)
// Check whether field value is within length range
function checkLength(theField, minlen, maxlen, error) {
  var fldVal = theField.value;
  if ( fldVal.length < minlen || fldVal.length > maxlen ) {
    addError(error);
    return false;
  } else {
    return true;
  }
}

function depositStateList(rspTxt) {

  var oSelect = document.getElementById("StateListSel");

  // Expected format being returned is a SELECT HTML structure

  oSelect.innerHTML = rspTxt;

}

function getStates(iCountryCode, sStateCode) {

  if ( ! oXmlHttp1 ) {
    oXmlHttp1 = zXmlHttp.createRequest();
  } else if ( oXmlHttp1.readyState != 0 ) {
    oXmlHttp1.abort();
  }

  oXmlHttp1.open("get",
		"Submit.php?func=statelist" +
		  "&countrycode=" + iCountryCode +
		  "&statecode=" + sStateCode,
		true);
  oXmlHttp1.onreadystatechange = function () {
    if ( oXmlHttp1.readyState == 4 ) {	// Request done
      if ( oXmlHttp1.status == 200 ) {	// Document Good
	depositStateList(oXmlHttp1.responseText);
      }
    }
  };

  oXmlHttp1.send(null);

}

function doGetStates() {
  var theForm = document.forms['Form1'];
  var sCCode = theForm.CountryList.options[theForm.CountryList.selectedIndex].value;
  getStates(sCCode, sSCode);
}

function validateSubmit() {

  var theForm = document.forms['Form1'];
  var oErrMsg = document.getElementById('errmsg');
  var noFocus = true;
  var fldname = '';
  var tstvalue = '';

  with (theForm) {
    var countrycode = CountryList[CountryList.selectedIndex].value;
    var statecode = StateList[StateList.selectedIndex].value;

				// Validate no missing values.
    for ( fldname in reqdflds ) {
      if ( reqdflds[fldname] ) {
	if ( fldname == 'CountryCode' || fldname == 'StateCode' ) {
	  if ( fldname == 'StateCode' ) {
	    tstvalue = statecode;
	  } else {
	    tstvalue = countrycode;
	  }
	  if ( tstvalue  == '' ) {
	    addError(msgs['s'+fldname]);
	    if ( noFocus ) {
	      elements[fldname].focus();
	      noFocus = false;
	    }
	  }
	} else if ( fldname == 'PostCode' && countrycode == 840 ) { // US ZipCode
	  if (! checkZIPCode(PostCode, msgs['sPostCode']) && noFocus ) {
	    PostCode.focus();
	    noFocus = false;
	  }
	} else if ( fldname == 'Prefix' ) {
	  tstvalue = elements[fldname]+'s';
	} else {
	  if ( ! checkString(elements[fldname], msgs['s'+fldname]) && noFocus ) {
	    elements[fldname].focus();
	    noFocus = false;
	  }
	}
      }
    }

    if ( checkLength(Password, 6, 30, msgs['iPassword']) ) {
      var stripedPassword = stripWhitespace(Password.value);
      if ( stripedPassword != Password.value ) {
	addError(msgs['hPassword']);
	if ( noFocus ) {
	  Password.focus();
	  noFocus = false;
	}
      } else {
	if ( func == 'submit' && Password.value != Password2.value ) {
	  addError(msgs['ePassword']);
	  if ( noFocus ) {
	    Password.focus();
	    noFocus = false;
	  }
	}
      }
    }

    if ( countrycode == 840 || countrycode == 124 ) {
      if (! checkUSPhone(Phone, msgs['sPhone'], true) && noFocus ) {
        Phone.focus();
	noFocus = false;
      }
    } else {
      if (! checkInternationalPhone(Phone, msgs['sPhone'], true) && noFocus ) {
        Phone.focus();
	noFocus = false;
      }
    }
  } // WITH

  if (iHasExtra) {
    validateExtra();
  }

  if ( hasErrors() ) {
    var errors = returnError('<li>');
    errors = errors.replace(/<li>\s*$/, '');
    if ( oErrMsg && errloc == 'inline' ) {
      oErrMsg.innerHTML = 'The following error(s) have been detected:<ul>' +
                          '<li>' + errors + '</ul>' +
                          '<p>Please correct these error(s) before proceeding.</p>';
    } else {
      errorWindow(errors, 400, false, htmls['startHtml'], htmls['endHtml']);
    }
    return false;
  } else {
    return true;
  }

}
