function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}


function formValidator(theForm)
{
  var checkPhNum = "0123456789-()";
  var checkAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var checkEmail = ".@0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var valid = "0123456789-";
  var hyphencount = 0;
  var allValid = true;
  var result = false;
  var bname = theForm.bname.value;
  var fname = theForm.fname.value;
  var lname = theForm.lname.value;
  var addr = theForm.addr.value;
  var apt = theForm.apt.value;
  var city = theForm.city.value;
  var state = theForm.state.value;
  var zip = theForm.zip.value;
  var resphone = theForm.resphone.value;
	var busphone = theForm.busphone.value;
	var notes = theForm.notes.value;
	var wdate = theForm.wdate.value;
	var schtime = theForm.schtime.value;
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;
	var dt=theForm.wdate

  //CheckBusinessName

  if (bname == "")
  {
    alert("Please enter your company name for the \"Business Name\" field or \"Home\" for non-business.");
    theForm.bname.focus();
    return (false);
  }

  //CheckFirstName

  if (fname == "")
  {
    alert("Please enter your first name for the \"First Name\" field.");
    theForm.fname.focus();
    return (false);
  }




  //CheckLastName

  if (lname == "")
  {
    alert("Please enter your last name for the \"Last Name\" field.");
    theForm.lname.focus();
    return (false);
  }




  //CheckAdress

  if (addr == "")
  {
    alert("Please enter an address for the \"Street Address\" field.");
    theForm.addr.focus();
    return (false);
  }



  //CheckCity

  if (city == "")
  {
    alert("Please enter a City for the \"City\" field.");
    theForm.city.focus();
    return (false);
  }




  //CheckState
  
  if (theForm.state.selectedIndex == "0")
  {
    alert("Please select a State");
    theForm.state.focus();
    return (false);
  }


  //CheckZip

if ((zip.length!=5) && (zip.length!=10)) {
alert("Please enter your 5 digit or 5 digit+4 zip code.");
theForm.zip.focus();
return false;
}
for (var i=0; i < zip.length; i++) {
temp = "" + zip.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code.  Please try again.");
theForm.zip.focus();
return false;
}
if ((hyphencount > 1) || ((zip.length==10) && ""+zip.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
theForm.zip.focus();
return false;
   }
}

	  //Check Res Phone
  
  if (resphone == "")
  {
    alert("Please enter a valid Phone Number for the \"Phone Number\" field.");
    theForm.resphone.focus();
    return (false);
  }
  else
	{
	  for (i=0; i<resphone.length; i++)
	     {
	       var ch = resphone.charAt(i);
	       var findCh=checkPhNum.indexOf(ch);
	       if (findCh==-1)
	       {
	          allValid=false;
	          break;
	       }
	    }
  }
	
  if (!allValid)
  {
	   alert("Please enter a valid Phone Number, (ex. 555-555-5555)");
	   theForm.resphone.focus();
	   return (false);
  }

  //Check Bus Phone
  
  if (busphone == "")
  {
    alert("Please enter a valid Phone Number for the \"Phone Number\" field.");
    theForm.busphone.focus();
    return (false);
  }
  else
	{
	  for (i=0; i<busphone.length; i++)
	     {
	       var ch = busphone.charAt(i);
	       var findCh=checkPhNum.indexOf(ch);
	       if (findCh==-1)
	       {
	          allValid=false;
	          break;
	       }
	    }
  }
	
  if (!allValid)
  {
	   alert("Please enter a valid Phone Number, (ex. 555-555-5555)");
	   theForm.busphone.focus();
	   return (false);
  }
	
	  //CheckEmail

  if (theForm.email.value == "")
  {
    alert("Please enter a value for the \"email\" field.");
    theForm.email.focus();
    return (false);
  }

  if (!isEmailAddr(theForm.email.value))
  {
    alert("Please enter a complete email address in the form:  yourname@yourdomain.com");
    theForm.email.focus();
    return (false);
  }
   
  if (theForm.email.value.length < 6)
  {
    alert("Please enter at least 6 characters in the \"email\" field.");
    theForm.email.focus();
    return (false);
  }

	
	//Check Work
  if (notes == "")
  {
    alert("Please enter the work you would like to have done.");
    theForm.notes.focus();
    return (false);
  }
	
	//Check Date
	if (wdate == "")
  {
    alert("Please enter the date you would like to have the work done.");
    theForm.wdate.focus();
    return (false);
  }
	
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}

	//Check Time
	if (schtime == "")
  {
    alert("Please enter the time you would like to have the work done.");
    theForm.schtime.focus();
    return (false);
	}
	
	//document.theForm.submit();

  return (true);  // you have passed all the validations tests so
		  // return "true" to the onSubmit event handler
		  // in the FORM tag*/
}

function loginValidator(loginForm)
{
  var name = loginForm.txtUserName.value;
  var pass = loginForm.password.value;
  
  if (name == "")
  {
    alert("Please enter a UserID for the \"UserID\" field.");
    loginForm.txtUserName.focus();
    return (false);
  }
  
  if (pass == "")
  {
    alert("Please enter a Password for the \"Password\" field.");
    loginForm.password.focus();
    return (false);
  }
	return (true);
}

function movepic(img_name,img_src) {
document[img_name].src=img_src;
}
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function searchValidator(formSearch)
{
  var name = formSearch.txtName.value;
  var company = formSearch.txtCompany.value;
  
  if ((name != "")&&(company != ""))
  {
    alert("Please enter only one item to search by.");
    formSearch.txtName.focus();
    return (false);
  }
	return (true);
}