// FORM VALIDATION SCRIPTS
// JavaScript Document by Xpanxion, Inc.  11-29-2007

// entsub(): accepts ENTER to submit form on IE. Runs function submitform(). Add onkeypress="entsub();" to each input line.
// changeCase(frmObj): changes field to Proper Case where frmObj is form object (note: not value, but just object)
// Validate_Email_Address(obj): validates that field is proper email address. obj is field value. returns false if not valid.
// clearfield(eventobj): clears background highlight from field. Add onblur="clearfield(this);" to each input line.
// highlightobject(eventobj): highlights background of field object. Use in validation scripts to highlight wrong fields.
// highlight(e): automatic highlight of background of field objects. Add onKeyUp="highlight(event)" onclick="highlight(event)" to form tags. Make sure to set variable highlightcolor="#cccccc" in script tag in main document.
// Phone number formatting: add to input tag - onkeydown="javascript:backspacerDOWN(this,event);" onkeyup="javascript:backspacerUP(this,event);" 
// ValidPhoneNumber(obj): returns false if obj is not a valid 10-digit phone number
// *** SAMPLE submitform() function AT BOTTOM


var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 13;
var phonevalue1;
var phonevalue2;
var cursorposition;
var ns6=document.getElementById&&!document.all
var previous=''
var eventobj
var intended=/INPUT|TEXTAREA|SELECT|OPTION/

function numbersonly(myfield, e, dec) {
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
    return true;

  // numbers
  else if ((("0123456789").indexOf(keychar) > -1))
    return true;

  // decimal point jump
  else if (dec && (keychar == ".")) {
    myfield.form.elements[dec].focus();
    return false;
  } else
    return false;
}

function anybadchars(badstring) {
	var str = badstring.replace(" ","");
	var goodfilter=/[a-z0-9_ ]/i  
	var badfilter=/update|insert|join|union|select|drop|delete/i

	if (goodfilter.test(str)==true && badfilter.test(str)==false)
		{ testresults2=false; 
		 }
	else{

		testresults2=true;
		}
		
	if (str=='') {
	   return false;
	   } else {	
	return (testresults2);
	}
}

function ParseForNumber1(object){
phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object){
phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object,e) { 
if(e){ 
e = e 
} else {
e = window.event 
} 
if(e.which){ 
var keycode = e.which 
} else {
var keycode = e.keyCode 
}

ParseForNumber1(object)

if(keycode >= 48){
ValidatePhone(object)
}
}

function backspacerDOWN(object,e) { 
if(e){ 
e = e 
} else {
e = window.event 
} 
if(e.which){ 
var keycode = e.which 
} else {
var keycode = e.keyCode 
}
ParseForNumber2(object)
} 

function GetCursorPosition(){

var t1 = phonevalue1;
var t2 = phonevalue2;
var bool = false
for (i=0; i<t1.length; i++)
{
if (t1.substring(i,1) != t2.substring(i,1)) {
if(!bool) {
cursorposition=i
bool=true
}
}
}
}

function ValidatePhone(object){

var p = phonevalue1

p = p.replace(/[^\d]*/gi,"")

if (p.length < 3) {
object.value=p
} else if(p.length==3){
pp=p;
d4=p.indexOf('(')
d5=p.indexOf(')')
if(d4==-1){
pp="("+pp;
}
if(d5==-1){
pp=pp+")";
}
object.value = pp;
} else if(p.length>3 && p.length < 7){
p ="(" + p; 
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

object.value = pp; 

} else if(p.length >= 7){
p ="(" + p; 
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

l40 = pp.length;
p40 = pp.substring(0,8);
p40 = p40 + "-"

p41 = pp.substring(8,l40);
ppp = p40 + p41;

object.value = ppp.substring(0, maxphonelength);
}

GetCursorPosition()

if(cursorposition >= 0){
if (cursorposition == 0) {
cursorposition = 2
} else if (cursorposition <= 2) {
cursorposition = cursorposition + 1
} else if (cursorposition <= 5) {
cursorposition = cursorposition + 2
} else if (cursorposition == 6) {
cursorposition = cursorposition + 2
} else if (cursorposition == 7) {
cursorposition = cursorposition + 4
e1=object.value.indexOf(')')
e2=object.value.indexOf('-')
if (e1>-1 && e2>-1){
if (e2-e1 == 4) {
cursorposition = cursorposition - 1
}
}
} else if (cursorposition < 11) {
cursorposition = cursorposition + 3
} else if (cursorposition == 11) {
cursorposition = cursorposition + 1
} else if (cursorposition >= 12) {
cursorposition = cursorposition
}

var txtRange = object.createTextRange();
txtRange.moveStart( "character", cursorposition);
txtRange.moveEnd( "character", cursorposition - object.value.length);
txtRange.select();
}

}

function ParseChar(sStr, sChar)
{
if (sChar.length == null) 
{
zChar = new Array(sChar);
}
else zChar = sChar;

for (i=0; i<zChar.length; i++)
{
sNewStr = "";

var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);

while (iEnd != -1)
{
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

sStr = sNewStr;
}

return sNewStr;
}


function ValidPhoneNumber(PhoneNumber) {
	var PNum = new String(PhoneNumber);
	
	//	555-555-5555
	//	(555)555-5555
	//	(555) 555-5555
	//	555-5555

        // NOTE: COMBINE THE FOLLOWING FOUR LINES ONTO ONE LINE.
	var regex = /^[0-9]{3,3}\-[0-9]{3,3}\-[0-9]{4,4}$|^\([0-9]{3,3}\) [0-9]{3,3}\-[0-9]{4,4}$|^\([0-9]{3,3}\)[0-9]{3,3}\-[0-9]{4,4}$|^[0-9]{3,3}\-[0-9]{4,4}$/;
	
	return regex.test(PNum);
}




function highlightobject(eventobj){
if (previous!=''){
if (checkel(previous))
previous.style.backgroundColor=''
previous=eventobj
if (checkel(eventobj))
eventobj.style.backgroundColor=highlightcolor
}
else{
if (checkel(eventobj))
eventobj.style.backgroundColor=highlightcolor
previous=eventobj
}
}

//Function to highlight form element
function highlight(e){
eventobj=ns6? e.target : event.srcElement
if (previous!=''){
if (checkel(previous))
previous.style.backgroundColor=''
previous=eventobj
if (checkel(eventobj))
if (eventobj.type!="select-one") {
eventobj.style.backgroundColor=highlightcolor ;}
else
{ 
// code for select boxes goes here	
	}
}
else{
if (checkel(eventobj))
eventobj.style.backgroundColor=highlightcolor
previous=eventobj
}
}
function clearfield(eventobj) {
eventobj.style.backgroundColor='';
}

function checkel(which)
{
	if ( which.style && intended.test(which.tagName) ) {
		if (ns6 && eventobj.nodeType == 3)
			eventobj = eventobj.parentNode.parentNode
		return true
	}
	else
		return false
}



function entsub(scripttorun) {
  if (window.event && window.event.keyCode == 13)
  {
	alert('return ' + scripttorun)
    eval('return ' + scripttorun)
	return false;
	}
  else{
    return true;}
}

function changeCase(frmObj) {
   var index;
   var tmpStr;
   var tmpChar;
   var preString;
   var postString;
   var strlen;
   tmpStr = frmObj.value.toLowerCase();
   strLen = tmpStr.length;
   if (strLen > 0)  {
      for (index = 0; index < strLen; index++)  {
          if (index == 0)  {
              tmpChar = tmpStr.substring(0,1).toUpperCase();
              postString = tmpStr.substring(1,strLen);
              tmpStr = tmpChar + postString;
          }
          else {
              tmpChar = tmpStr.substring(index, index+1);
              if (tmpChar == " " && index < (strLen-1))  {
                   tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
                   preString = tmpStr.substring(0, index+1); 
                   postString = tmpStr.substring(index+2,strLen);
                   tmpStr = preString + tmpChar + postString;
              }
          }
      }
   }
frmObj.value = tmpStr;
}

function Validate_Email_Address(obj){
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(obj)) {
testresults=true
}
else
{
testresults=false
}
return (testresults)
}

function loweremail() {
	document.form.email.value=document.form.email.value.toLowerCase();
}


/*
function submitform() {
    if (document.form.name.value=='') {
	   alert("Please enter your name in the field provided.");
	   document.form.name.focus();
	   document.form.name.select();
	   document.form.name.style.backgroundColor=highlightcolor;
	   return false;
	}
	if (document.form.phone.value=='' && document.form.email.value=='') {
	   alert("Either an email address or phone number must be provided.");
	   document.form.email.focus();
	   document.form.email.select();
	   document.form.email.style.backgroundColor=highlightcolor;
	   return false;
	}
	if (document.form.email.value!='') {
	   if (Validate_Email_Address(document.form.email.value)) {
	   }
	   else {
	      alert("Please enter a valid email address before submitting.");
		  document.form.email.focus();
		  document.form.email.select();
		  document.form.email.style.backgroundColor=highlightcolor;
		  return false;
	   }
	}
	if (document.form.phone.value!='') {
	   if (ValidPhoneNumber(document.form.phone.value)) {
	   }
	   else {
	      alert("Please enter a valid phone number before submitting.");
		  document.form.phone.focus();
		  document.form.phone.select();
		  document.form.phone.style.backgroundColor=highlightcolor;
		  return false;
	   }
	}        
	if (document.form.subject.value=='') {
	   alert("Please enter the subject of your message in the field provided.");
	   document.form.subject.focus();
	   document.form.subject.select();
	   document.form.subject.style.backgroundColor=highlightcolor;
	   return false;
	}   
	if (document.form.message.value=='') {
	   alert("Please enter your message in the field provided.");
	   document.form.message.focus();
	   document.form.message.select();
	   document.form.message.style.backgroundColor=highlightcolor;
	   return false;
	} 
    document.form.submit();
}
*/
