// QQ START
  function checkSubmit() {
    aMandatory["first_name"] = new Array("First Name", "Text", "");
    aMandatory["last_name"] = new Array("Last Name", "Text", "");
    aMandatory["email"] = new Array("E-Mail", "Text", "");
    aMandatory["phone"] = new Array("Phone", "Text", "");
    if(document.quick_quote_form.name != undefined) {
      if(chkMandatory(document.quick_quote_form)) {
       if(validEmail(document.quick_quote_form.email)){
         disableButtons(document.quick_quote_form);
         return true;
       }
       return false;
      }
    }
    return false;
  }
// END QQ START

// FORMVALIDATE
var aMandatory = new Array();
function chkMandatory(objForm)
{
  for (var iLoop in aMandatory)
  {
    var strVal;
    strVal = "";
    if (aMandatory[iLoop][1] == "Text")
      strVal = eval("objForm." + iLoop + ".value");
    else if (aMandatory[iLoop][1] == "Radio")
    {
      var oEle = eval("objForm." + iLoop);
      var iLen = oEle.length;
      for (var iEle = 0; iEle < iLen; iEle++)
        if (oEle[iEle].checked)
        {
          strVal = oEle[iEle].value;
          break;
        }
    }
    else if (aMandatory[iLoop][1] == "Select")
    {
      var iIdx = eval("objForm." + iLoop + ".selectedIndex");
      if (iIdx > -1)
        strVal = eval("objForm." + iLoop + "[" + iIdx + "].value");
      else
        strVal = "";
    }
    strVal = alltrim(strVal);

    if (strVal == "")
    {
      alert('Please enter a value for ' + aMandatory[iLoop][0] + '.');
      if (aMandatory[iLoop][2] != "")
        eval("objForm." + aMandatory[iLoop][2] + ".focus()");
      else
      {
        if (aMandatory[iLoop][1] == "Radio")
          eval("objForm." + iLoop + "[0].focus()");
        else
          eval("objForm." + iLoop + ".focus()");
      }
      return false;
    }
  }
  return true;
}

function disableButtons(objForm) {
  var i
  for(i=0; i<objForm.elements.length; i++) {
    if(objForm.elements[i].type == "submit" || objForm.elements[i].type == "button") {
      objForm.elements[i].disabled = true;
    }
  }
  hideshowlayers(objForm);
}

function hideshowlayers(objForm) {
  // This fixes a bug in IE where layers disappear even though they are visible
  if (objForm && objForm.all) {
		for(i=0; i<objForm.all.length; i++) {
			if(objForm.all[i].tagName == "DIV") {
				if(objForm.all[i].style.visibility == "visible") {
					objForm.all[i].style.visibility = "hidden";
					objForm.all[i].style.visibility = "visible";
				}
			}
		}
	}
}

function setFormFocus (objForm)
{
  //Call this function from the body onLoad and it will set focus to first field in objForm
  if(objForm) {
    aForm = objForm;
    if(aForm.elements[0]!=null) {
      var i;
      var max = aForm.length;
      for( i = 0; i < max; i++ ) {
        if( aForm.elements[i].type != "hidden" &&
            aForm.elements[i].type != "button" &&
            !aForm.elements[i].disabled &&
            !aForm.elements[i].readOnly ) {
            aForm.elements[i].focus();
          break;
        }
      }
    }
  }
}
// END FORMVALIDATE

// STRING
var whitespace = " \t\n\r";

function isEmpty(strVal)
{
	return ((strVal == null) || (strVal.length == 0));
}

function isWhitespace(strVal)
{
	var nPos = 0;

	if (isEmpty(strVal))
		return false;

	for (nPos = 0; nPos < strVal.length; nPos++)
		if (whitespace.indexOf(strVal.charAt(nPos)) == -1)
			return false;

	return true;
}

function rtrim(strVal)
{
	var nPos = 0;

	if (isEmpty(strVal))
		return strVal;

	for (nPos = strVal.length-1; nPos >= 0; nPos--) {
		if (whitespace.indexOf(strVal.charAt(nPos)) == -1)
			break;
	}

	return (nPos == strVal.length-1 ? strVal.substring(0) : strVal.substring(0, nPos+1));
}

function ltrim(strVal)
{
	var nPos = 0;

	if (isEmpty(strVal))
		return strVal;

	for (nPos = 0; nPos < strVal.length; nPos++) {
		if (whitespace.indexOf(strVal.charAt(nPos)) == -1)
			break;
	}

	return strVal.substring(nPos);
}

function alltrim(strVal)
{
	return (isEmpty(strVal) ? strVal:ltrim(rtrim(strVal)));
}

function replace(strSrc, strVal, strWith)
{
	var nPos = 0, strLeft="", strRight="";

	if (isEmpty(strSrc) || (strSrc.indexOf(strVal) < 0))
		return strSrc;

	nPos = strSrc.indexOf(strVal);
	strLeft = strSrc.substring(0, nPos);
	nPos += strVal.length;
	strRight = strSrc.substring(nPos);

	return (strLeft + strWith + strRight);
}

function replaceall(strSrc, strVal, strWith)
{
	var strBuffer=strSrc;

	while (strBuffer.indexOf(strVal) >= 0)
		strBuffer = replace(strBuffer, strVal, strWith);

	return (strBuffer);
}

function occurs(strVal, strSrc)
{
	var nCnt = 0, strBuffer=strSrc;

	while (strBuffer.indexOf(strVal) >= 0)
	{
		strBuffer = replace(strBuffer, strVal, "");
		nCnt++;
	}

	return (nCnt);
}

function replicate(strVal, nCnt)
{
	var i = 0, strBuffer = "";

	for (i = 0; i < nCnt; i++)
		strBuffer += strVal;

	return (strBuffer);
}

function stuff(strSrc, nStartPos, nCnt, strReplacement)
{
	var strLeft= "", strRight="";

	if (nCnt < 0 || nStartPos < 0 || (nStartPos > strSrc.length-1))
		return strSrc;

	strLeft = strSrc.substring(0, nStartPos);
	strRight = strSrc.substring(nStartPos+nCnt);

	return (strLeft + strReplacement + strRight);
}
// END STRING

// EMAIL
function validEmail(objFld) {
	if (!validEmailText(objFld.value)) {
    alert('Please enter a valid email address');
    objFld.select();
    objFld.focus();
    return false;
	}
	return true;
}

function validEmailText(strEmailText) {
  var strInvalid = "*?#&^~`'\\[]<>;/:\" ";
  var bAliasedEmail = false;
  var  strAliasedEmail = "";

  var i = 0;
    var  bValid;
  bValid = true;

  strEmailText = alltrim(strEmailText);
  if (isEmpty(strEmailText))
    return true;

  var strEmail = new String(strEmailText);

  // Assumption: if strEmail contains < and >, Email is between < and >
  if (strEmail.indexOf('<') < strEmail.indexOf('>') && strEmail.indexOf('<') >= 0)
  {
    bAliasedEmail = true;
    strAliasedEmail = strEmail;
    strEmail = strEmail.substring(strEmail.indexOf('<')+1,strEmail.indexOf('>'));
  }

  if (strEmail.length < 5)  // check the length
    bValid = false;
  else if (strEmail.lastIndexOf("@") <= 0 || (strEmail.lastIndexOf(".") - strEmail.lastIndexOf("@") <= 1))

// check positions of @ and .

    bValid = false;
  else if (occurs('@', strEmail) > 1) // check if @ occurs more than once
    bValid = false
  else if (strEmail.lastIndexOf(".") == (strEmail.length - 1))
    // check to make sure email doesnt end in period
    bValid = false;
  else  // check if any invalid characters present
  {
    for (i = 0; i < strEmail.length; i++)
      if (strInvalid.indexOf(strEmail.charAt(i)) >= 0)
      {
        bValid = false;
        break;
      }
  }

  if (!bValid) {
    return bValid;
  }

  if (bAliasedEmail)
   this.value = strAliasedEmail;
  else
   this.value = strEmail;

  return bValid;
}

// This one doesn't accept aliases, but it does check for a valid extension.
function validEmailTextAlt(strEmailText) {
	return (strEmailText.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(\/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$/) != -1);
}

