var thisFormPull = '';
var errorDiv;
var errorText;
var errorList = new Array();
var errorCount = 0;

var errorBlank = "This Field is Required";
var errorEmail = "Email must be in xyz@abc.def format";
var errorDate = "Invalid Date Format.";


function showError(Form , Field) {
 clearError2();
 document[Form][Field].style.backgroundColor='#ff7f00';
}

function blurError(Form, Field) {
 if(errorLookup(Form, Field))
  showError(Form, Field);
 else
  clearError2();
}

 function showError2(thisId, Message) {
  clearError2();
  errorName = thisId;
  div = document.createElement('div');
  divs = div.style;
  divs.position='absolute';
  divs.zIndex=1;
//  divs.top= getTop(errorName)-18;
  divs.top= getTop(errorName) + errorName.offsetHeight;
  divs.left = getLeft(errorName);
  if(errorName.offsetWidth > 150)
   divs.width=errorName.offsetWidth;
  else
   divs.width=150;
  //divs.height=18;
  divs.borderWidth=1;
  divs.borderColor='black';
  divs.borderStyle='solid';
  divs.backgroundColor='#ff7f00';
  div.className = 'errorClass';
  div.innerHTML = Message;
  errorDiv = div;
  document.body.appendChild(div);
 }

function clearError2() {
 if(errorDiv) {
  document.body.removeChild(errorDiv);
  errorDiv = null;
 }
}
function clearError(Form, Field) {
 if((Message = errorLookup(Form, Field))  && (document[Form][Field].style.backgroundColor=='#ff7f00' || document[Form][Field].style.backgroundColor=='rgb(255, 127, 0)'))
  showError2(document[Form][Field], Message);
 document[Form][Field].style.backgroundColor='white';
}

function errorTest(Form, Field) {
 var Fail = null;
 var firstError = null;
 if(Field) {
  if(errorList[Form][Field]) {
   if(rrorLookup(Form, Field)) {
    showError(Form, error);
    Fail = 1;
   }
  }
 }
 else if(errorList[Form]) {
  for(error in errorList[Form]) {
   if(errorLookup(Form, error)) {
    Fail = 1;
    if(firstError == null)
     firstError = document[Form][error];
    showError(Form, error);
    errorCount++;
   }
  }
 }
 if(!Fail)
  return true;
 else {
  if(firstError != null)
   firstError.focus();
  return false;
 }
}
 function errorLookup(Form, error)
 {
  Split = errorList[Form][error].split(':');
  for(var i =0; i < Split.length; i++)
  {
   thisId = document[Form][error];
   switch(Split[i])
   {
    case 'blank':
     if(thisId.value=='')
      return errorBlank;
    break;
    case 'email':
     if(!thisId.value.match(/^$|.+@.+\..+/))
      return errorEmail;
    break;
    case "date":
      if(!thisId.value.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}/) && thisId.value!='')
       return errorDate;
    break;
    case "date2":
      if(!thisId.value.match(/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/) && thisId.value!='')
       return errorDate;
    break;
   }
  }
 }
 function  addErrorCheck(Form, Field, Errors)
 {
  if(!errorList[Form])
   errorList[Form] = new Array();
  errorList[Form][Field] = Errors;
 }
function frontSubmit(Form, noError) {
 if(noError)
  document[Form].submit();
 else if(errorTest(Form))
  document[Form].submit();
}


function formPullOther(thisId) {
 document['onkeypress']=formPullEscape;
 if(thisFormPull)
  promptClear();
 if(thisId.options.length == thisId.selectedIndex + 1) {
  thisFormPull = thisId;
  P = document.getElementById('formEnterFloat');
  Ps = P.style;
  Ps.top  = getTop(thisId);
  Ps.left = getLeft(thisId);
  thisId.style.visibility='hidden';
  Ps.visibility = 'visible';
  document.promptForm.prompt.value='';
  document.promptForm.prompt.focus();
 }
}

function promptClear() {
 if(!thisFormPull)
  return;
 P = document.getElementById('formEnterFloat');
 Ps = P.style;
 Ps.visibility = 'hidden';
 thisFormPull.style.visibility='visible';
 thisFormPull.focus();
 thisFormPull = '';
}

function formPromptSubmit() {
 City = document.promptForm.prompt.value;
 thisId = thisFormPull;
 P = document.getElementById('formEnterFloat');
 if(City && P.style.visibility!='hidden') {
  Len = thisId.options.length;
  thisId.options.length++;

  thisId.options[Len] = new Option(thisId.options[Len-1].text, thisId.options[Len-1].value);

  thisId.options[Len-1].value = thisId.options[Len-1].text = City; 

  thisId.selectedIndex = Len - 1;
  if(thisId['onchange'])
   thisId['onchange']();
 }
 promptClear();
 return false;
}


function formPullEscape(evt) {
 if(browser[0] == 'ie')
  if(event.keyCode==27)
   promptClear();
 if(browser[0] == 'ns')
  if(evt.keyCode==27)
   promptClear();
}
                                                                                                 

function phoneFormat(thisId, Event) {
 var valid = 0;
 if(browser[0] == 'ie')
  var Key = Event.keyCode;
 else
  var Key = Event.charCode;
                                                                                                 
 var bob = "abc";
 Valid = new Array();
 Valid[37] = 1;
 Valid[38] = 1;
 Valid[39] = 1;
 Valid[40] = 1;
 Valid[8] = 1;
 Valid[9] = 1;
 Valid[46] = 1;
if(thisId.value.length == 14 && !Valid[Event.keyCode])
  thisId.value+=' ';
 if(thisId.value.length >= 15)
  return true;
                                                                                                 
 if((Key >=48 && Key <= 57) || Valid[Event.keyCode]) {
  valid = 1;
  if(thisId.value.length == 0)
   thisId.value = '(';
                                                                                                 
  if(thisId.value.substr(0,1)!='(')
   thisId.value = '(' + thisId.value;
                                                                                                 
  if(thisId.value.length == 3 && !Valid[Event.keyCode]) {
   thisId.value += String.fromCharCode(Key);
   thisId.value+= ') ';
   return false;
  }
  if(thisId.value.length == 4 && !thisId.value.substr(5,1)!=')' && !Valid[Event.keyCode]) {
   thisId.value+= ')';
  }
  if(thisId.value.length == 5 && !thisId.value.substr(5,1)!=' ' && !Valid[Event.keyCode]) {
   thisId.value+= ' ';
  }
 if(thisId.value.length == 8 && !Valid[Event.keyCode]) {
   thisId.value += String.fromCharCode(Key);
   thisId.value+= '-';
   return false;
  }
  if(thisId.value.length==9 && !thisId.value.match(/-$/) && !Valid[Event.keyCode])
   thisId.value+= '-';
 }
 if(valid)
  return true;
 else
  return false;
}

function goTo(Cont, Clear) {
 document.more.formControl.value=Cont;
 if(Clear)
  document.more.formClear.value=1;
 else
  document.more.formClear.value='';
}

