function GetDay(nDay)
{
	var Days = new Array("Sunday","Monday","Tuesday","Wednesday",
	                     "Thursday","Friday","Saturday");
	return Days[nDay]
}

function GetMonth(nMonth)
{
	var Months = new Array("January","February","March","April","May","June",
	                       "July","August","September","October","November","December");
	return Months[nMonth]
}

function DateString()
{
	var Today = new Date();
	var suffix = "th";
	switch (Today.getDate())
	{
		case 1:
		case 21:
		case 31:
			suffix = "st"; break;
		case 2:
		case 22:
			suffix = "nd"; break;
		case 3:
		case 23:
			suffix = "rd"; break;
	};

	var strDate = GetDay(Today.getDay()) + ", " + GetMonth(Today.getMonth()) + " " + Today.getDate();
	strDate = strDate.toUpperCase();

	return strDate;
}
function checkrepeat(str, n) {
	for (var i = 0; i < str.length; i++) {
	  var j = 0;
	  for (j; j < n; j++) {
	    if(!(str.charAt(i)==str.charAt(i+j+1)))
	      break;
	  }
	  if (j == n) return true;
	}
	return false;
}
function move2next_3(fielda, fieldb) {
  if (fielda.value.length == 3)
		fieldb.focus();
}

function move2next_2(fielda, fieldb) {
  if (fielda.value.length == 2)
		fieldb.focus();
}

function checkForm1()
{
       var temp;
        var re;

        // added by Cristine for sub id
        var urlString = new String();
        urlString = location.search;
        newString = urlString.substr(1, urlString.length);
        parameters = newString.split("&");

        for ( i=0; i < parameters.length; i++ ) {
            pair = parameters[i].split("=");


            if (pair[0] == 'subid') {
                document.manForm.subid.value = pair[1];
            }
            if (pair[0] == 'refid') {
                document.manForm.rcid.value  = pair[1];
            }
        }
        // end add for sub/refid

        if (document.manForm.first_name.value == "") {
            alert("Please enter a first name!");
      		document.manForm.first_name.focus();
            return false;
        }

        if (document.manForm.last_name.value == "") {
            alert("Please enter a last name!");
      		document.manForm.last_name.focus();
            return false;
        }

        if (document.manForm.home_phone1.value == ""
            || document.manForm.home_phone2.value == ""
            || document.manForm.home_phone3.value == "")
        {
            alert("home Phone field is empty!");
      		document.manForm.home_phone1.focus();
            return false;
        } else {
            temp = document.manForm.home_phone1.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("home Phone format error!");
       			document.manForm.home_phone1.focus();
                return false;
            }
            temp = document.manForm.home_phone2.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("home Phone format error!");
      			document.manForm.home_phone2.focus();
               return false;
            }
            temp = document.manForm.home_phone3.value;
            if (temp.search(/^[0-9]{4}$/) == -1) {
                alert("home Phone format error!");
    	  		document.manForm.home_phone3.focus();
                return false;
            }
            document.manForm.home_phone.value = document.manForm.home_phone1.value + document.manForm.home_phone2.value + document.manForm.home_phone3.value;
            if (checkrepeat(document.manForm.home_phone.value, 5)) {
            	alert("home Phone conatins repetitive characters.");
	      		document.manForm.home_phone1.focus();
            	return false;
            }
        }

        if (document.manForm.email_address.value == "") {
            alert("Email field is empty!");
            document.manForm.email_address.focus();
            return false;
        } else {
        	document.manForm.email_address.value = trim(document.manForm.email_address.value);
            temp = document.manForm.email_address.value;
            if (temp.search(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/i) == -1) {
                alert("Email format error!");
		        document.manForm.email_address.focus();
                return false;
            }
        }

		var loanbal = document.getElementById("field_4");
		if ( loanbal != null) {
			if (document.manForm.field_4.value == "") {
	            alert("Please select your Current Loan Balance!");
	      		document.manForm.field_4.focus();
	            return false;
	        }
	    }

		if (document.manForm.field_15.value == "") {
            alert("Please select your months behind!");
      		document.manForm.field_15.focus();
            return false;
        }

        return true;
    }

function trim(str) {
  return str.replace(/^\s+|\s+$/g,"");
}

function checkForm2()
    {

       var temp;
        var re;

        // added by Cristine for sub id
        var urlString = new String();
        urlString = location.search;
        newString = urlString.substr(1, urlString.length);
        parameters = newString.split("&");

        for ( i=0; i < parameters.length; i++ ) {
            pair = parameters[i].split("=");


            if (pair[0] == 'subid') {
                document.manForm.subid.value = pair[1];
            }
            if (pair[0] == 'refid') {
                document.manForm.rcid.value  = pair[1];
            }
        }
        // end add for sub/refid

        if (document.manForm.first_name.value == "") {
            alert("Please enter a first name!");
      		document.manForm.first_name.focus();
            return false;
        }

        if (document.manForm.last_name.value == "") {
            alert("Please enter a last name!");
      		document.manForm.last_name.focus();
            return false;
        }

        if (document.manForm.home_phone1.value == ""
            || document.manForm.home_phone2.value == ""
            || document.manForm.home_phone3.value == "")
        {
            alert("home Phone field is empty!");
      		document.manForm.home_phone1.focus();
            return false;
        } else {
            temp = document.manForm.home_phone1.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("home Phone format error!");
       			document.manForm.home_phone1.focus();
                return false;
            }
            temp = document.manForm.home_phone2.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("home Phone format error!");
      			document.manForm.home_phone2.focus();
               return false;
            }
            temp = document.manForm.home_phone3.value;
            if (temp.search(/^[0-9]{4}$/) == -1) {
                alert("home Phone format error!");
    	  		document.manForm.home_phone3.focus();
                return false;
            }
            document.manForm.home_phone.value = document.manForm.home_phone1.value + document.manForm.home_phone2.value + document.manForm.home_phone3.value;
            if (checkrepeat(document.manForm.home_phone.value, 5)) {
            	alert("home Phone conatins repetitive characters.");
	      		document.manForm.home_phone1.focus();
            	return false;
            }
        }

        if (document.manForm.work_phone1.value == ""
            && document.manForm.work_phone2.value == ""
            && document.manForm.work_phone3.value == "")
        {
            // null
        } else {
            temp = document.manForm.work_phone1.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("work Phone format error!");
                return false;
            }
            temp = document.manForm.work_phone2.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("work Phone format error!");
                return false;
            }
            temp = document.manForm.work_phone3.value;
            if (temp.search(/^[0-9]{4}$/) == -1) {
                alert("work Phone format error!");
                return false;
            }
            document.manForm.work_phone.value = document.manForm.work_phone1.value
            + document.manForm.work_phone2.value + document.manForm.work_phone3.value;
            if (checkrepeat(document.manForm.work_phone.value, 5)) {
            	alert("Work Phone conatins repetitive characters.");
            	return false;
            }
        }
        if (document.manForm.email_address.value == "") {
            alert("Email field is empty!");
            document.manForm.email_address.focus();
            return false;
        } else {
        	document.manForm.email_address.value = trim(document.manForm.email_address.value);
            temp = document.manForm.email_address.value;
            if (temp.search(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/i) == -1) {
                alert("Email format error!");
		        document.manForm.email_address.focus();
                return false;
            }
        }

		if (document.manForm.field_6.value == "") {
            alert("Please select your credit status!");
      		document.manForm.field_6.focus();
            return false;
        }

		if (document.manForm.field_18.value == "") {
            alert("Please select your annual income!");
      		document.manForm.field_18.focus();
            return false;
        }

        if (document.manForm.street_address.value == "") {
            alert("Please Enter Your Mailing Address.");
     		document.manForm.street_address.focus();
            return false;
        }

        if (document.manForm.city.value == "") {
            alert("City field is empty!");
     		document.manForm.city.focus();
            return false;
        } else if (checkrepeat(document.manForm.city.value, 2)) {
            alert("City field contains repetitive characters.");
     		document.manForm.city.focus();
            return false;
        }
        if (document.manForm.state.value == "") {
            alert("Please select your state.");
     		document.manForm.state.focus();
            return false;
        }
        if (document.manForm.zip.value == "") {
            alert("Please select your zip.");
     		document.manForm.zip.focus();
            return false;
        }  else {
            temp = document.manForm.zip.value;
            if (temp.search(/^[0-9]{5}$/) == -1) {
                alert("The Zip Code must be 5 digits!");
                return false;
            }

        }
         if (document.manForm.field_1.value == "") {
            alert("Please Choose a House Type!");
     		document.manForm.field_1.focus();
            return false;
        }
        if (document.manForm.field_2.value == "") {
            alert("Please Choose a House Value!");
     		document.manForm.field_2.focus();
            return false;
        }

		if (document.manForm.field_15.value == "") {
            alert("Please select your months behind!");
      		document.manForm.field_15.focus();
            return false;
        }
		if (document.manForm.field_4.value == "") {
            alert("Please select your first mortgage balance!");
      		document.manForm.field_4.focus();
            return false;
        }
		if (document.manForm.field_20.value == "") {
            alert("Please enter your lender!");
      		document.manForm.field_20.focus();
            return false;
        }

		if (document.manForm.field_8.value == "") {
            alert("Please select your interest rate!");
      		document.manForm.field_8.focus();
            return false;
        }
		if (document.manForm.field_7.value == "") {
            alert("Please select your int rate type!");
      		document.manForm.field_7.focus();
            return false;
        }
		if (document.manForm.field_19.value == "") {
            alert("Please select your monthly payment!");
      		document.manForm.field_19.focus();
            return false;
        }
		if (document.manForm.field_14.value == "") {
            alert("Please select your second mortgage balance!");
      		document.manForm.field_14.focus();
            return false;
        }
        return true;
    }
