// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var v_date = new Date();
var minYear = v_date.getFullYear();
var maxYear = minYear + 1;

function ValidateForm() 
{

    document.InspectionForm.txtFirstname.value = trim(document.InspectionForm.txtFirstname.value.titleCase());
    document.InspectionForm.txtLastname.value = trim(document.InspectionForm.txtLastname.value.titleCase());
    document.InspectionForm.txtAddress.value = trim(document.InspectionForm.txtAddress.value.titleCase());
    document.InspectionForm.txtCity.value = trim(document.InspectionForm.txtCity.value.titleCase());
    document.InspectionForm.txtState.value = trim(document.InspectionForm.txtState.value);
    document.InspectionForm.txtZip.value = trim(document.InspectionForm.txtZip.value);
    document.InspectionForm.dtApptDate.value = trim(document.InspectionForm.dtApptDate.value);
    document.InspectionForm.txtApptTime.value = trim(document.InspectionForm.txtApptTime.value);
    document.InspectionForm.txtPhone.value = trim(document.InspectionForm.txtPhone.value);
    document.InspectionForm.txtEmail.value = trim(document.InspectionForm.txtEmail.value);

    var flag = true;
    var msg = "The following fields are required:\r";

    if (document.InspectionForm.txtFirstname.value == "") 
    {
         msg += "- First Name\r";
         flag = false;
    }

    if (document.InspectionForm.txtLastname.value == "") 
    {
         msg += "- Last Name\r";
         flag = false;
    }

    if (document.InspectionForm.txtAddress.value == "") 
    {
         msg += "- Address\r";
         flag = false;
    }

    if (document.InspectionForm.txtCity.value == "") 
    {
         msg += "- City\r";
         flag = false;
    }

    if (document.InspectionForm.txtState.value == "") 
    {
         msg += "- State\r";
         flag = false;
    }

    if (document.InspectionForm.txtZip.value == "") 
    {
         msg += "- Zip\r";
         flag = false;
    }

    if (document.InspectionForm.dtApptDate.value == "") 
    {
         msg += "- Desired Date of appt\r";
         flag = false;
    }

    if (document.InspectionForm.txtApptTime.value == "") 
    {
         msg += "- Desired Time of appt\r";
         flag = false;
    }

    if (document.InspectionForm.txtPhone.value == "") 
    {
         msg += "- Daytime Phone\r";
         flag = false;
    }

    if (document.InspectionForm.txtEmail.value == "") 
    {
         msg += "- Email Address\r";
         flag = false;
    }
    
    if (flag == false)
    {
        alert(msg);
        return flag;
    }
    
	if (isDate(document.InspectionForm.dtApptDate.value) == false)
	{
		document.InspectionForm.dtApptDate.focus();
		flag = false;
		return flag;
	}
	
	if (isValidEmail(document.InspectionForm.txtEmail.value) == false)
	{
	    document.InspectionForm.txtEmail.focus();
	    alert("Please enter a valid email address");
	    flag = false;
	    return flag;
	}
    
    alert("Thank you, " + document.InspectionForm.txtFirstname.value + ". \rYour information has been successfully submitted.");
    return flag;
}

function ValidateForm2() 
{

    document.ReportForm.txtFirstname.value = trim(document.ReportForm.txtFirstname.value.titleCase());
    document.ReportForm.txtLastname.value = trim(document.ReportForm.txtLastname.value.titleCase());
    document.ReportForm.txtEmail.value = trim(document.ReportForm.txtEmail.value);

    var flag = true;
    var msg = "The following fields are required:\r";

    if (document.ReportForm.txtFirstname.value == "") 
    {
         msg += "- First Name\r";
         flag = false;
    }

    if (document.ReportForm.txtLastname.value == "") 
    {
         msg += "- Last Name\r";
         flag = false;
    }

    if (document.ReportForm.txtEmail.value == "") 
    {
         msg += "- Email Address\r";
         flag = false;
    }
    
    if (flag == false)
    {
        alert(msg);
        return flag;
    }
    
	if (isValidEmail(document.ReportForm.txtEmail.value) == false)
	{
	    document.ReportForm.txtEmail.focus();
	    alert("Please enter a valid email address");
	    flag = false;
	    return flag;
	}
    
    alert("Thank you, " + document.ReportForm.txtFirstname.value + ". Your report request has been received. Check your inbox shortly for your free report. \r\rPlease allow five seconds to process your request. You will be re-directed to the Free Reports page.");
    return flag;
}

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);
	var 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);
		}
	}
	var month = parseInt(strMonth);
	var day = parseInt(strDay);
	var 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 isValidEmail(string) 
{
    return (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

//Below function script changes the name to title case
//It is called by appending the .titleCase() method to the object

String.noLC = new Object
  ({the:1, a:1, an:1, and:1, or:1, but:1, aboard:1,
    about:1, above:1, across:1, after:1, against:1,
    along:1, amid:1, among:1, around:1, as:1, at:1,
    before:1, behind:1, below:1, beneath:1, beside:1,
    besides:1, between:1, beyond:1, but:1, by:1, 'for':1,
    from:1, 'in':1, inside:1, into:1, like:1, minus:1,
    near:1, of:1, off:1, on:1, onto:1, opposite:1,
    outside:1, over:1, past:1, per:1, plus:1,
    regarding:1, since:1, than:1, through:1, to:1,
    toward:1, towards:1, under:1, underneath:1, unlike:1,
    until:1, up:1, upon:1, versus:1, via:1, 'with':1,
    within:1, without:1});
    
String.prototype.titleCase = function () 
{
  var parts = this.split(' ');
  if ( parts.length == 0 ) return '';
  var fixed = new Array();
  for ( var i in parts ) {
    var fix = '';
    if ( String.noLC[parts[i]] )
    {
      fix = parts[i].toLowerCase();
    }
    else if ( parts[i].match(/^([A-Z]\.)+$/i) )
    { // will mess up "i.e." and like
      fix = parts[i].toUpperCase();
    }
    else if ( parts[i].match(/^[^aeiouy]+$/i) )
    { // voweless words are almost always acronyms
      fix = parts[i].toUpperCase();
    }
    else
    {
      fix = parts[i].substr(0,1).toUpperCase() +
                 parts[i].substr(1,parts[i].length);
    }
    fixed.push(fix);
  }
  fixed[0] = fixed[0].substr(0,1).toUpperCase() +
                 fixed[0].substr(1,fixed[0].length);
  return fixed.join(' ');
}
