// JavaScript Document
function closeWindow() {
	window.close();
}
function popup(url){
	window.open(
		url,
		'window',
		'width=600, height=600, resizable=yes, scrollbars=yes, location=no,  toolbar=no, menubar=no, status=no'
	);
}
function openWindow(url,w,h,shortcut){
	if ( shortcut )
	{
		url += "?shortcut=" + shortcut + "&";
		newWindow=window.open(url,'thewindow','width=' + w + ',height=' + h);  newWindow.focus();
	} else {
		newWindow=window.open(url,'_blank','width=' + w + ',height=' + h);  newWindow.focus();
	}
}

// Date functions
function GetMonth(nMonth) {
	var Months = new Array("January","February","March","April","May","June",
	                       "July","August","September","October","November","December");
	return Months[nMonth] 	  	 
}

function GetDay(nDay) {
	var Days = new Array("Sunday","Monday","Tuesday","Wednesday",
	                     "Thursday","Friday","Saturday");
	return Days[nDay]
}

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() + suffix + ", " + Today.getFullYear();	return strDate
}
function yearString(publishedYear){
	var Today = new Date();
	var strYear = Today.getFullYear();           
	if (!publishedYear) {
				publishedYear = strYear;
	}
	if (strYear == publishedYear) {
				return strYear
	} else {
		strObject = publishedYear + "-" + strYear;
		return strObject;
	}           
}

//form valiation
	// trim functions
	function trim(str) { return rtrim(ltrim(str)); }
	function rtrim(str) { return str.replace(/\s+$/, ''); }
	function ltrim(str) { return str.replace(/^\s+/, ''); }
	// determines whether or not the element has anything in it
	function hasContent(el) { return el.value!=''; }
	function hasCheck(el) { return el.checked; }
	function hasSelectionValue(el) {
		for(var i = 0; i < el.length; i++) {
			if(el.options[i].selected) {
				if(el.options[i].value.length) { return true; }
			}
		}
		return false;
	}
	function anyWithCheck(ss) {
		for(var i = 0; i < ss.length; i++) {
			if(ss[i].checked) { return true; }
		}
		return false;
	}
	// special validation functions
	function validEmail(str) { return str.match(/^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}$/i); }
	function validSSN(str) { return str.match(/^\d{3}-*\d{2}-*\d{4}$/); }
	function validDate(str) { return str.match(/^\d{1,2}[-\/]+\d{1,2}[-\/]+(\d{2}|\d{4})$/); }
	function validPhone(str) { return str.match(/^[\+1\( -]*\d{3}[ -\.\)]*\d{3}[ -\.]*\d{4}.*\d*$/); }
	function validZip(str) { return str.match(/^\d{5}-*($|\d{4}$)/); }
	// massage functions
	function massageSSN(el) { el.value=el.value.replace(/[^\d]/g,''); }
	function massagePhone(el) { el.value=el.value.replace(/[^\d]/g,'').replace(/^1/,''); }
	function massageZip(el) { el.value=el.value.replace(/[^\d]/g,''); }
	// functions that talk directly to or are called directly from the form
	function resetForm() { return confirm("Are you sure you want to undo all your changes?"); }
	function confirmDelete() { return confirm("Are you sure you want to delete this item?"); }

function checkAppForm(f) {
	// Set flag for requiring mortgage amount
	var reqMortgage=false;
	for(var r=0; r < f.radOwnHome.length; r++) {
		if(f.radOwnHome[r].checked && f.radOwnHome[r].value=='1') {
			reqMortgage=true;
		}
	}
	// Trim all form elements
	for(var i=0; i < f.elements.length; i++) {
		if(f.elements[i].type=='text' || f.elements[i].type=='textarea') {
			f.elements[i].value=trim(f.elements[i].value);
		}
	}
	// Declare msg variable for the message
	var msg='';
	// US citizen
	(!anyWithCheck(f.radUScit)) && 				(msg+="  - You must select whether or not you are a U.S. citizen");
	// First Name
	(!hasContent(f.txtFirstName)) && 			(msg+= "\r\n  - First Name is required");
	// Last Name
	(!hasContent(f.txtLastName)) && 			(msg+= "\r\n  - Last Name is required");
	// SSN
	(!hasContent(f.txtSSN)) && 					(msg+= "\r\n  - Social Security Number is required");
	if(hasContent(f.txtSSN)) {
		(!validSSN(f.txtSSN.value)) &&			(msg+= "\r\n  - Social Security Number should be in the format ddd-dd-dddd or ddddddddd");
	}
	// DOB
	(!hasContent(f.txtDOB)) &&					(msg+= "\r\n  - Date of Birth is required");
	if(hasContent(f.txtDOB)) {
		(!validDate(f.txtDOB.value)) &&			(msg+= "\r\n  - A valid Date of Birth is required (mm/dd/yyyy or m/d/yy)");
	}
	// Email Address
	(!hasContent(f.txtEmail)) &&				(msg+= "\r\n  - Email Address is required");
	if(hasContent(f.txtEmail)) {
		(!validEmail(f.txtEmail.value)) &&		(msg+= "\r\n  - A valid email address is required");
	}
	// Evening Phone
	(!hasContent(f.txtHomePhone)) &&			(msg+= "\r\n  - Home Phone is required");
	if(hasContent(f.txtHomePhone)) {
		(!validPhone(f.txtHomePhone.value)) &&	(msg+= "\r\n  - Home Phone should be in the format (ddd) ddd-dddd xxxxx\r\n          NOTE: Extension (xxxxx) can be any length");
	}
	// Monthly Income
	(!hasContent(f.txtMonthlyIncome)) &&		(msg+= "\r\n  - Monthly Income is required");
	(!anyWithCheck(f.radOwnHome)) && 			(msg+= "\r\n  - You must select whether you rent or own your house");
	(!anyWithCheck(f.radOtherProp)) && 			(msg+= "\r\n  - You must select whether or not you own other property");
	
	// Own Home/Mortgage Payment
	(!hasContent(f.txtMonthlyMortgage) && reqMortgage) &&
												(msg+= "\r\n  - Monthly Mortgage Payment is required");
	// Residence Validation
	(!hasContent(f.txtAddressA)) &&				(msg+= "\r\n  - Address Line is required");
	(!hasContent(f.txtCity)) && 				(msg+= "\r\n  - City is required");
	(!hasContent(f.txtZip)) &&					(msg+= "\r\n  - Zip is required");
	(!validZip(f.txtZip.value)) &&				(msg+= "\r\n  - Zip must be in the format ddddd or ddddd-dddd");
	(!hasContent(f.txtResLength)) &&			(msg+= "\r\n  - Time at current address is required");
	(isNaN(f.txtResLength.value)) &&			(msg+= "\r\n  - Time at current address must be a number");
	if(hasContent(f.txtResLength) && !isNaN(f.txtResLength.value) && f.txtResLength.value < 5) {
		(!hasContent(f.txtAddressB)) &&				(msg+= "\r\n  - Prev. Address Line is required");
		(!hasContent(f.txtCityB)) && 				(msg+= "\r\n  - Prev. City is required");
		(!hasContent(f.txtZipB)) &&					(msg+= "\r\n  - Prev. Zip is required");
		(!validZip(f.txtZipB.value)) &&				(msg+= "\r\n  - Prev. Zip must be in the format ddddd or ddddd-dddd");
	}
	
	// Employer Info Validation
	(!anyWithCheck(f.radSelfEmp)) && 			(msg+= "\r\n  - You must select whether or not you are self employeed");
	(!hasContent(f.txtEmpName)) &&				(msg+= "\r\n  - Employer name is required");
	(!hasContent(f.txtOccupation)) &&			(msg+= "\r\n  - Occupation is required");
	(!hasContent(f.txtEmpLength)) &&			(msg+= "\r\n  - Time at Current Employer is required");
	(isNaN(f.txtEmpLength.value)) &&			(msg+= "\r\n  - Time at Current Employer must be a number");
	(isNaN(f.txtPrevEmpLength.value)) &&		(msg+= "\r\n  - Time at Previous Employer must be a number");
	
	(isNaN(f.txtDependents.value)) &&			(msg+= "\r\n  - Number of Dependents must be a number");
	(!hasContent(f.txtLoanAmount)) &&			(msg+= "\r\n  - The Loan Amount is required");
	(!hasSelectionValue(f.selOffice)) &&		(msg+= "\r\n  - You must select the branch you want to apply with");
	
	(!anyWithCheck(f.radVehicle)) && 			(msg+= "\r\n  - You must select whether or not you own a vehicle");
	(isNaN(f.txtVehicleYear.value)) &&			(msg+= "\r\n  - The Vehicle Year must be a number");
	(isNaN(f.txtVehicleBYear.value)) &&			(msg+= "\r\n  - The 2nd Vehicle Year must be a number");
	
	//Co-Applicant validation
	(!anyWithCheck(f.radJoint)) && 			(msg+= "\r\n  - You must select whether this is a joint application or not");
	if(hasCheck(f.radJointYes)) {
		(!hasContent(eval(f.txtCoFirstName))) &&		(msg+= "\r\n  - Co-applicant First Name is required");
		(!hasContent(f.txtCoLastName)) && 				(msg+= "\r\n  - Co-applicant Last Name is required");
		// Co-applicant SSN
		(!hasContent(f.txtCoSSN)) && 					(msg+= "\r\n  - Co-applicant Social Security Number is required");
		if(hasContent(f.txtCoSSN)) {
			(!validSSN(f.txtCoSSN.value)) &&			(msg+= "\r\n  - Co-applicant Social Security Number should be in the format ddd-dd-dddd or ddddddddd");
		}
		// Co-applicant DOB
		(!hasContent(f.txtCoDOB)) &&					(msg+= "\r\n  - Co-applicant Date of Birth is required");
		if(hasContent(f.txtCoDOB)) {
			(!validDate(f.txtCoDOB.value)) &&			(msg+= "\r\n  - A valid Co-applicant Date of Birth is required (mm/dd/yyyy or m/d/yy)");
		}
		// Co-applicant Email Address
		(!hasContent(f.txtCoEmail)) &&					(msg+= "\r\n  - Co-applicant Email Address is required");
		if(hasContent(f.txtCoEmail)) {
			(!validEmail(f.txtCoEmail.value)) &&		(msg+= "\r\n  - A valid Co-applicant email address is required");
		}
		// Co-applicant Home Phone
		(!hasContent(f.txtCoHomePhone)) &&				(msg+= "\r\n  - Co-applicant Home Phone is required");
		if(hasContent(f.txtCoHomePhone)) {
			(!validPhone(f.txtCoHomePhone.value)) &&	(msg+= "\r\n  - Co-applicant Home Phone should be in the format (ddd) ddd-dddd xxxxx\r\n          NOTE: Extension (xxxxx) can be any length");
		}
		// Co-applicant Residence Validation
		(!hasContent(f.txtCoAddressA)) &&				(msg+= "\r\n  - Co-applicant Address Line is required");
		(!hasContent(f.txtCoCity)) && 				(msg+= "\r\n  - Co-applicant City is required");
		(!hasContent(f.txtCoZip)) &&					(msg+= "\r\n  - Co-applicant Zip is required");
		(!validZip(f.txtCoZip.value)) &&				(msg+= "\r\n  - Co-applicant Zip must be in the format ddddd or ddddd-dddd");
		(!hasContent(f.txtCoResLength)) &&			(msg+= "\r\n  - Co-applicant Time at current address is required");
		(isNaN(f.txtCoResLength.value)) &&			(msg+= "\r\n  - Co-applicant Time at current address must be a number");
		// Co-applicant Employer Info Validation
		(!anyWithCheck(f.radCoSelfEmp)) && 			(msg+= "\r\n  - You must select whether or not the Co-applicant is self employeed");
		(!hasContent(f.txtCoEmpName)) &&				(msg+= "\r\n  - Co-applicant Employer name is required");
		(!hasContent(f.txtCoOccupation)) &&			(msg+= "\r\n  - Co-applicant Occupation is required");
		(!hasContent(f.txtCoEmpLength)) &&			(msg+= "\r\n  - Co-applicant Time at Current Employer is required");
		(isNaN(f.txtCoEmpLength.value)) &&			(msg+= "\r\n  - Co-applicant Time at Current Employer must be a number");
		(isNaN(f.txtCoPrevEmpLength.value)) &&		(msg+= "\r\n  - Co-applicant Time at Previous Employer must be a number");
	}
	
	//creditors validation
	for (var i = 1; i<=10; i++) {
		if(hasContent(eval("f.txtCredPayment_" + i)) || hasContent(eval("f.txtCredBal_" + i))) {
			(!hasContent(eval("f.txtCredName_" + i))) &&		(msg+= "\r\n  - You must enter a Creditors Name into Creditor #" + i + ".");
		}
	}
	
	if(msg != '') {
		// show message
		alert('The form has the following errors on it.\r\n' + msg + '\r\n\r\nPlease correct them and resubmit.');
	} else {
		// massage data
		(validSSN(f.txtSSN.value)) && 			(massageSSN(f.txtSSN));
		(validPhone(f.txtHomePhone.value)) &&	(massagePhone(f.txtHomePhone));
		(validPhone(f.txtCellPhone.value)) && 	(massagePhone(f.txtCellPhone));
		(validZip(f.txtZip.value)) &&			(massageZip(f.txtZip));
	}
	//return false;
	return (msg == '');
}

// Flash Javascript
function putFlash(src, width, height, FlashVars) {
    var strObject = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" "
                  + "        codebase=\"https://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" "
                  + "        width=\"" + width + "\" height=\"" + height + "\">"
                  + "    <param name=\"movie\" value=\"" + src + "\" /> "
                  + "    <param name=\"quality\" value=\"high\" /> ";

    if (FlashVars && FlashVars != "") {
        strObject += "<param name=\"FlashVars\" value=\"" + FlashVars + "\" /> ";
    }

    strObject += "<embed src=\"" + src + "\" quality=\"high\" "
                  + "           pluginspage=\"https://www.macromedia.com/go/getflashplayer\" "
                  + "           type=\"application/x-shockwave-flash\" ";

	if (FlashVars && FlashVars != "") {
		strObject += "FlashVars=\"" + FlashVars + "\" ";
	}
	
	strObject += "width=\"" + width + "\" height=\"" + height + "\">"
               + "    </embed>"
               + "</object>";

    document.write(strObject);
}
	//Calling Flash Header
	function putHeader(FlashVars) {
    	putFlash("/flash/header.swf", 766, 348, FlashVars);
}

//Main Nav dropdowns
function showMenu(id) {
	if (document.getElementById) {
		var show_menu=document.getElementById(id);
		show_menu.style.display="block";
	}
}

function hideMenu(id) {
	if (document.getElementById) {
		var hide_menu=document.getElementById(id);
		hide_menu.style.display="none";
	}
}
