// do a form level validation
function validate()
{
	f = document.forms[0];
	for	(var i = 0; i < f.length; i++)
	{
	    var e = f.elements[i];
	    var fs = e.id;				// field string
	    if (fs.indexOf("*") != -1)	// optional field
	    {
			continue;
		}
		else if (fs.indexOf("Number") != -1)	// required phone number field
		{
			if	(!checkPhone(e.value, fs))
			{
				e.focus();
				return false;
			}
		}
		else if (fs.indexOf("Debt") != -1)	// required debt field
		{
			if	(e.type == "select") continue;	// no validation for dropdown
			if (fs.indexOf("Name") != -1)		// text entry
			{
				if (!dataRequired(e.value, fs))
				{
					e.focus();
					return false;
				}
			}
			else		// only dollar values remaining
			{
				if (!checkMoney(e.value, fs))
				{
					e.focus();
					return false;
				}
			}
		}
		else if (fs.indexOf("Zip") != -1)	// required phone number field
		{
			if	(!checkZip(e.value))
			{
				e.focus();
				return false;
			}
		}
		else		// make sure something entered in remaining fields
		{
			if (!dataRequired(e.value, fs))
			{
				e.focus();
				return false;
			}
		}
	}
    return true;
}

	// Browser Detection
	isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
	NS4 = (document.layers) ? true : false;
	IEmac = ((document.all)&&(isMac)) ? true : false;
	IE4plus = (document.all) ? true : false;
	IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
	IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
	ver4 = (NS4 || IE4plus) ? true : false;
	NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;

	// Body onload utility (supports multiple onload functions)
	var gSafeOnload = new Array();
	function SafeAddOnload(f)
	{
		if (IEmac && IE4)  // IE 4.5 blows out on testing window.onload
		{
			window.onload = SafeOnload;
			gSafeOnload[gSafeOnload.length] = f;
		}
		else if  (window.onload)
		{
			if (window.onload != SafeOnload)
			{
				gSafeOnload[0] = window.onload;
				window.onload = SafeOnload;
			}		
			gSafeOnload[gSafeOnload.length] = f;
		}
		else
			window.onload = f;
	}
	function SafeOnload()
	{
		for (var i=0;i<gSafeOnload.length;i++)
			gSafeOnload[i]();
	}
	
	//validation functions
	function isdate(strDate){
	var aryDate = new Array();
	var bolMonth = false
	var bolDay = false
	var bolLeapYear = false
	var monthMax = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	var patternLeapYear = /\d\d00/
	
	aryDate = strDate.split(/\s*\/\s*/);
	
	if ((aryDate[0] <= 12) && (aryDate[0] >= 1)){
		bolMonth = true;
		//year divide by 4 or 400
		aryDate[2] = parseInt(aryDate[2]);
		if ((aryDate[2] >= 0) && (aryDate[2] <= 99)) {
			aryDate[2] = aryDate[2] + 2000;
			}
					
		if(patternLeapYear.test(aryDate[2])) {
			if ((aryDate[2] % 400)== 0 ) {
				bolLeapYear = true
				}
		
			else {
				if ((aryDate[2]%4) == 0){
					bolLeapYear = true
					}
				}
		if (bolLeapYear) {
			monthMax[2] = monthMax[2] + 1
			}		
						
			if (aryDate[1] <= monthMax[aryDate[0]]) {
			bolDay = true
			}
		}
		
		if ((bolMonth) && (bolDay)) {
			return true
		}
	alert('Please enter a valid date in the format of "MM/DD/YY"!');
	return false
	}
}
function dataRequired(strText, strLabel){
	var regText = /[\w, \d]+/;
	
	if (regText.test(strText)) 
		return true;
	else {
		alert('Please fill out ' + strLabel + ' field.')
		return false;
		}
}

function checkZip(strText){
	var regText = /\d{5}(-\d{4})?/	
	
	if (regText.test(strText)) 
		return true;
	else {
		alert('Please enter a valid zip code in either the 12345 or 12345-1234 zip code formats.')
		return false;
		}
}

function checkPhone(strText, strLabel){
	
	var regText = /\d{10}/;
	 
		
	if (regText.test(strText.replace(/[^\d]/g, ''))) 
		return true;
	else {
		alert('Please fill out ' + strLabel + ' with area code and 7 digit telephone number.')
		return false;
		}
}

function checkMoney(objtxt){
	
	var regText = /[^\d\.\$]/;
	var wsText = /\s/
	var fldName
	var errorMsg 
	var cashAry
		
	if ( objtxt.name == 'multifield1')
		fldName = 'Balance'
	else
		fldName = 'Payment'
		
	errorMsg = 'Please fill out ' + fldName + ' with a cash value in the format of $1234.12.' 
	
	objtxt.value = objtxt.value.replace(/,/g, '');
	objtxt.value = objtxt.value.replace(/\$/g, '');
	
	if (!regText.test(objtxt.value)) {
		if (objtxt.value.replace(/\$/g, '') == ''){
			alert(errorMsg)
			return false;
		}
		objtxt.value = objtxt.value.replace(/\$/g, '');
		if (isNaN(Math.round(objtxt.value * 100))){
			alert(errorMsg)
			return false;
		}
		objtxt.value = Math.round(objtxt.value * 100)
		cashAry = objtxt.value.split(".")
	
		objtxt.value = '$' + objtxt.value.slice(0, objtxt.value.length -2) + '.' + objtxt.value.slice(objtxt.value.length -2, objtxt.value.length)
		return true;
		}
	else {
		alert(errorMsg)
		return false;
		}
}

	//
	// Main Form Functions
	//

	// This array holds our form values when we need to regenerate the form
	var gFieldValues1 = new Array(1); 
	var gFieldValues2 = new Array(1); 
	var gFieldValues3 = new Array(1);
	var gPersInfo = new Array(18);
	/*var amcheck = '';
	var pmcheck = '';
	var hmcheck = '';
	var wkcheck = '';
	var emcheck = '';*/
	var call_timeValues = {"":"Select", "7am-10am":"7am-10am","10am-12pm":"10am-12pm", "12pm-3pm":"12pm-3pm", "3pm-5pm":"3pm-5pm", "5pm-7pm":"5pm-7pm", "7pm-9pm":"7pm-9pm"};
	
	var call_placeValues ={"":"Select", "home":"Home", "work":"Work", "email":"Email"};
	var call_time = '';
	var call_place = '';
	
	var referral_options = {"no-reply" : "No Reply", "tv" : "TV Commercial", "newspaper" : "Newspaper", "friend" : "Friend / Relative", "google" : "Google", "yahoo" : "Yahoo", "msn" : "MSN", "other" : "Other Source"};
	var referred_from = '';


	
	
	var	i;

	for (i=0;i<gPersInfo.length;i++)
		gPersInfo[i] = "";

	gFieldValues1[0]="";
		
	for (var i=0;i<gFieldValues1.length;i++){
		gFieldValues1[i]="";
		gFieldValues2[i]="";
		gFieldValues3[i]="";
	}
		
	function GetFormHTML()
	{
		var htmlStr = '';
		htmlStr += '<form id="dynoform" name="dynoform" action="quote2.php" method="POST" onSubmit="return validate(this);">';
//		htmlStr += '<input type=hidden name=ary1 id=ary1 value="' + gOptionValues + '">'
//		htmlStr += '<input type=hidden name=ary2 id=ary2 value="' + gOptionText + '">'
//	//	htmlStr += '<input type=hidden name=ary3 id=ary3 value="' + gOptPercent + '">'
//	//	htmlStr += '<input type=hidden name=ary7 id=ary7 value="' + gOptRate + '">'
		htmlStr += '<u>Basic Personal Information:</u><br><br>'
		htmlStr += '<table width="550">'
		htmlStr += '<tr><td style="color:#000000;">First&nbsp;Name,&nbsp;Middle <br>Initial, Last Name:</td><td><input type="text" size="19" maxlength="19" name="persInfo1" id="First Name" value="' + gPersInfo[1] + '"></td><td><input type="text" size="2" maxlength="2" name="persInfo2" id="*Middle Name" value="' + gPersInfo[2] + '"></td><td><input type="text" size="19" maxlength="19" name="persInfo3" id="Last Name" value="' + gPersInfo[3] + '"></td></tr>';
		htmlStr += '<tr><td style="color:#000000;">Address:</td><td  colspan="3"><input type="text" size="50" maxlength="50" name="persInfo4" id="Address" value="' + gPersInfo[4] + '"></td></tr>';
		htmlStr += '<tr>'
		htmlStr += '<td style="color:#000000;">City:</td><td  colspan="3" style="color:#000000;"><input type="text" maxlength="50" name="persInfo5" id="City" value="' + gPersInfo[5] + '">';
		//htmlStr += 'State:<input type="text" Size="2" maxlength="2" name="persInfo6" id="State" value="' + gPersInfo[6] + '">';
		htmlStr += '<tr>'
		htmlStr += '<td style="color:#000000;">State:</td><td  colspan="3" style="color:#000000;"><select name="persInfo6" id="State">';
		for(var i=0;i<states.length; i++) {
			htmlStr += '<option value="' + states[i]["key"] + '"';

			if (gPersInfo[6] == states[i]["key"])
			{
				htmlStr += ' Selected '
			}
			htmlStr += '>' + states[i]["value"] + '</option>';
			
		}
		htmlStr += '</select></td></tr>';
		
		htmlStr += '<tr>'
		htmlStr += '<td style="color:#000000;">Zip:</td><td  colspan="3" style="color:#000000;"><input type="text" Size="10" maxlength="10" name="persInfo7" id="Zip" value="' + gPersInfo[7] + '"></td>';
		htmlStr += '</tr>'
		htmlStr += '<tr><td style="color:#000000;">E-mail Address:</td><td colspan="3"><input type="text" Size="50" maxlength="50" name="persInfo12" id="email" value="' + gPersInfo[12] + '"></td></tr>';
		htmlStr += '<tr>'
		htmlStr += '<td style="color:#000000;">Home Number:</td><td colspan="3" style="color:#000000;"><input type="text" size="12" name="persInfo8" id="Home Number" value="' + gPersInfo[8] + '">';
		htmlStr += '<font color="red">*</font><font color="black">Work Number:</font><input type="text" size="12" name="persInfo9" id="*Work Number" value="' + gPersInfo[9] + '"></td>';
		htmlStr += '</tr>'
		htmlStr += '<tr>'
		htmlStr += '<td><font color="red">*</font><font color="black">Cell Phone:</font></td><td colspan="3" style="color:#000000;"><input type="text" size="12" name="persInfo10" id="*Cell Number" value="' + gPersInfo[10] + '">';
		htmlStr += '<font color="red">*</font><font color="black">Fax Number:</font><input type="text" size="12" name="persInfo11" id="*Fax Number" value="' + gPersInfo[11] + '"></td>';
		htmlStr += '</tr>';
		
		
		
/*		htmlStr += '<tr>'
		htmlStr += '<td colspan="4"><br><font color="red">*</font><font color="black">If you would like to be called, please click on AM or PM and where you would like to be called. If you would prefer to be e-mailed click that box.</font><br><input name="amcheck" type="checkbox" value="AM" '+ amcheck + ' ><font color="black">AM</font><br><input name="pmcheck" type="checkbox" value="PM"  '+ pmcheck + ' ><font color="black">PM</font><br>'
		htmlStr += '<input name="hmcheck" type="checkbox" value="Home" '+ hmcheck + ' ><font color="black">Home</font><input name="wkcheck" type="checkbox" value="Work"  '+ wkcheck + ' ><font color="black">Work</font><input name="emcheck" type="checkbox" value="Email"  '+ emcheck + '><font color="black">Email</font>   </td></tr>';*/
		
		// Time to Call
		htmlStr += '<tr>';
		htmlStr += '<td colspan="4">Best Time to Call: <br><select name="call_time">';
		for (var val in call_timeValues) {
			htmlStr += '<option value="' + val + '" ';
			if(val == call_time) {
				htmlStr += ' selected ';
			}
			htmlStr += '>' + call_timeValues[val] + '</option>';
		}
		htmlStr += '</select><br><br></td>';
		htmlStr += '</tr>';
		
		// Place to Call
		htmlStr += '<tr>';
		htmlStr += '<td colspan="4">Best Place to Call: <br><select name="call_place">';
		for (var val in call_placeValues) {
			htmlStr += '<option value="' + val + '" ';
			if(val == call_place) {
				htmlStr += ' selected ';
			}
			htmlStr += '>' + call_placeValues[val] + '</option>';
		}
		htmlStr += '</select><br><br></td>';
		htmlStr += '</tr>';
		
		//Where did you hear about us?
		htmlStr += '<tr>';
		htmlStr += '<td colspan="4">How did you hear about us?<br><select name="referred_from">';
		for (var val in referral_options) {
			htmlStr += '<option value="' + val + '" ';
			
			if(val == referred_from) {
				htmlStr += ' selected ';
			}
			htmlStr += '>' + referral_options[val] + '</option>';
		}
		htmlStr += '</select></td>';
		htmlStr += '</tr>';
		
		
		htmlStr += '<tr>'
		htmlStr += '<tr><td colspan="4" bgcolor="#999999" ><span style="color:white;font-weight:bold;">Free Debt Consolidation Relief Quote Creditor List:</span><br><span ><ul><li style="color:white;">If your credit card account is not listed please choose "Credit card account not listed" on the free debt relief quote drop down creditor list.</li><li style="color:white;">If your personal bank loan is not listed please choose "Personal Bank Loans" on the free debt relief quote drop down creditor list.</li><li style="color:white;">If your accounts are in collections and not listed please chose the category "Collection Account" under the free debt relief quote Creditor List when completing the free quote form </li><li style="color:white;">If you are including a Bank of America account you must include ALL of your credit card and unsecured personal loan accounts regardless if they are Bank of America accounts or not (even business accounts) in order for Bank of America to accept you into the program. If that is not acceptable to you, please do not add any Bank of America accounts to your quote. </li></ul></span></td></tr></table>';
		
		htmlStr += '<br><u>Creditors:</u>'
		htmlStr += '<table width="550"><tr>'
		htmlStr += '<td></td><td><font color="black">Type</font></td><td><font color="black">Balance</font></td><td><font color="black">Minimum<br>Monthly<br>Payment</font></td>'
		htmlStr += '</tr>'
		for (var i=0;i<gFieldValues1.length;i++){
			htmlStr += '<tr>'
			htmlStr +=  '<td>' + (i+1);
			htmlStr += '<td><SELECT id="multifield3[]" name="multifield3[]" id="Debt Type" >'
			for (var j=0;j<gOptionValues.length;j++)
			{
				htmlStr += '<OPTION value="'+ gOptionValues[j] + '"'
				
				if (gFieldValues3[i] == gOptionValues[j])
				{
//					alert(gFieldValues3[i] + '/' + gOptionValues[j])
					htmlStr += ' Selected '
				}
//				htmlStr += '>' + gOptionText[j] + ', ' + gOptPercent[j] + ', ' + gOptRate[j] + '</OPTION>'
				htmlStr += '>' + gOptionText[j] + '</OPTION>'
			}		
			htmlStr += '</SELECT></td>'
			htmlStr += '<td><input type="text" size="8" maxlength="10" name="multifield1[]" id="multifield1" onchange="checkMoney(this)" value="' + gFieldValues1[i] + '"></td>'
			htmlStr += '<td><input type="text" size="8" maxlength="10" name="multifield2[]" id="multifield2" onchange="checkMoney(this)" value="' + gFieldValues2[i] + '"></td>'
			htmlStr += '</tr>'
			}
		htmlStr += '<tr><td colspan="2">';	
		htmlStr += '<input type="button" value="Add New Line" onClick="AddField()"></td>';
		if (gFieldValues1.length > 1)		// add delete button
		{
			htmlStr += '<td colspan="2">';	
			htmlStr += '<input type="button" value="Delete Last Line" onClick="DelField()" id=Delbutton name=DelButton>';
			htmlStr += '</td>';
		}
		htmlStr += '</tr><tr><td colspan="4" style="font-size:12pt;"><br><b>To insure you receive an accurate quote, please list each account seperately and review the balances you have entered prior to hitting the "Get Free Quote" button. The minimum monthly payment amount you enter should be what you normally pay without any past due amounts added.<font color="blue"> If you would like to keep a copy of the above listed creditors Click on your print button Now.</font></b></td>';
		htmlStr += '</tr><tr><td colspan="4" style="font-size:12pt;"><br><b><font color="red"> If you do not get your quote immediately online after clicking the get free quote link, please call us at 888-357-8683 and let us know. </b></font></td>';
		htmlStr += '<tr><td colspan="4">';
		htmlStr += '<input type="submit" value="Get Free Quote" id=submit1 name=submit1>';
		htmlStr += '</td></tr>';
		htmlStr += '</table>'
		htmlStr += '</form>';
		

		return htmlStr;
	}

	function GetFormObj()
	{
		var returnObj = null;
		
		if (IE4plus)
		{
			returnObj =  document.dynoform;
		}
		else if (NS4)
		{
			returnObj =  document.formlayer.document.dynof
orm;
		}
		else if (NS6)
		{	
			returnObj =  document.getElementById("dynoform");
		}
		else {
			returnObj = document.getElementById("dynoform");
		}
		return returnObj;
	}

	function AddField()
	{
		// Save previously entered data here
		var formObj = GetFormObj();
		for (var i=0;i<gFieldValues1.length;i++)
		{
			if (gFieldValues1.length>1)
			{
				gFieldValues1[i]= formObj['multifield1[]'][i].value
				gFieldValues2[i]= formObj['multifield2[]'][i].value
				gFieldValues3[i]= formObj['multifield3[]'][i].value
			}
			else
			{
				gFieldValues1[i]= formObj['multifield1[]'].value
				gFieldValues2[i]= formObj['multifield2[]'].value
				gFieldValues3[i]= formObj['multifield3[]'].value
			}
		}
			gPersInfo[1] = formObj.persInfo1.value
			gPersInfo[2] = formObj.persInfo2.value
			gPersInfo[3] = formObj.persInfo3.value
			gPersInfo[4] = formObj.persInfo4.value
			gPersInfo[5] = formObj.persInfo5.value
			gPersInfo[6] = formObj.persInfo6.value
			gPersInfo[7] = formObj.persInfo7.value
			gPersInfo[8] = formObj.persInfo8.value
			gPersInfo[9] = formObj.persInfo9.value
			gPersInfo[10] = formObj.persInfo10.value
			gPersInfo[11] = formObj.persInfo11.value
			gPersInfo[12] = formObj.persInfo12.value //email box
			
			call_time = formObj.call_time.value;
			call_place = formObj.call_place.value;
			referred_from = formObj.referred_from.value;
			
			
			/*if (formObj.amcheck.checked) 
			  amcheck = 'checked';
			else if (! formObj.amcheck.checked)
			  amcheck = "";
			  
			if (formObj.pmcheck.checked) 
			  pmcheck = 'checked';
			else if (! formObj.pmcheck.checked)
			  pmcheck = "";
			  
			if (formObj.hmcheck.checked) 
			  hmcheck = 'checked';
			else if (! formObj.hmcheck.checked)
			  hmcheck = "";
			
			if (formObj.wkcheck.checked) 
			  wkcheck = 'checked';
			else if (! formObj.wkcheck.checked)
			  wkcheck = "";
			  
			if (formObj.emcheck.checked) 
			  emcheck = 'checked';
			else if (! formObj.emcheck.checked)
			  emcheck = "";*/
			
			
		{
		// Create the new field
		gFieldValues1[gFieldValues1.length]=""
		gFieldValues2[gFieldValues2.length]=""
		gFieldValues3[gFieldValues3.length]=""
		UpdateForm();
		}
	}

	function DelField()
	{
		if	(gFieldValues1.length > 1)
		{
			// Save previously entered data here
			var formObj = GetFormObj();
			for (var i=0;i<gFieldValues1.length-1;i++)
			{
				gFieldValues1[i]= formObj['multifield1[]'][i].value
				gFieldValues2[i]= formObj['multifield2[]'][i].value
				gFieldValues3[i]= formObj['multifield3[]'][i].value
			}
			gPersInfo[1] = formObj.persInfo1.value
			gPersInfo[2] = formObj.persInfo2.value
			gPersInfo[3] = formObj.persInfo3.value
			gPersInfo[4] = formObj.persInfo4.value
			gPersInfo[5] = formObj.persInfo5.value
			gPersInfo[6] = formObj.persInfo6.value
			gPersInfo[7] = formObj.persInfo7.value
			gPersInfo[8] = formObj.persInfo8.value
			gPersInfo[9] = formObj.persInfo9.value
			gPersInfo[10] = formObj.persInfo10.value
			gPersInfo[11] = formObj.persInfo11.value
			gPersInfo[12] = formObj.persInfo12.value //email box
			
			
			call_time = formObj.call_time.value;
			call_place = formObj.call_place.value;
			referred_from = formObj.referred_from.value;
			
			/*if (formObj.amcheck.checked) 
			  amcheck = 'checked';
			else if (! formObj.amcheck.checked)
			  amcheck = "";
			  
			if (formObj.pmcheck.checked) 
			  pmcheck = 'checked';
			else if (! formObj.pmcheck.checked)
			  pmcheck = "";
			  
			if (formObj.hmcheck.checked) 
			  hmcheck = 'checked';
			else if (! formObj.hmcheck.checked)
			  hmcheck = "";
			
			if (formObj.wkcheck.checked) 
			  wkcheck = 'checked';
			else if (! formObj.wkcheck.checked)
			  wkcheck = "";
			  
			if (formObj.emcheck.checked) 
			  emcheck = 'checked';
			else if (! formObj.emcheck.checked)
			  emcheck = "";*/
			   
			
			
			
			
			// delete the last field
			--gFieldValues1.length;
			--gFieldValues2.length;
			--gFieldValues3.length;
			UpdateForm();
		}
	}
	
	function UpdateForm()
	{
		var htmlStr = GetFormHTML();
		if (IE4plus)
		{
			document.all.formlayer.innerHTML = htmlStr;
		}
		else if (NS4)
		{
			document.formlayer.document.open();
			document.formlayer.document.write(htmlStr);
			document.formlayer.document.close();
		}
		else if (NS6)
		{	
			document.getElementById("formlayer").innerHTML = htmlStr;
		}
		else {
			document.getElementById("formlayer").innerHTML = htmlStr;
		}
	}

	function IncludeForm()
	{
		var htmlStr = GetFormHTML();
		if (IE4plus || NS6)
		{
			
			document.write('<DIV ID=formlayer name=formlayer  STYLE="position:relative; WIDTH=400px; HEIGHT=50px">' + htmlStr + '</DIV>');
		}
		else if (NS4)
		{
			
			// Because NS needs floating layers, we need a placeholder graphic to force anything
			// below the layer content to leave whitespace for the layer.  The position of this
			// graphic is also used in determining the position of the layer.
			document.write('<img name="formlocation" border="0" width="400" height="200" src="../images/spacer.gif">');

		}
		else {
			document.write('<DIV ID=formlayer name=formlayer  STYLE="position:relative; WIDTH=400px; HEIGHT=50px">' + htmlStr + '</DIV>');
		}

	}

	//
	// Netscape 4.x Ineptness
	//
	function HandleOnload()
	{
		if (NS4)
		{
			var width = document.formlocation.width;
			var height = document.formlocation.height;
			
			nL=new Layer(width);
			nL.name = "formlayer";
			nL.left=document.formlocation.x;
			nL.top=document.formlocation.y;
			nL.bgColor = "white";
			nL.clip.width=width;
			nL.clip.height=height;
			nL.document.open();
			nL.document.write(GetFormHTML());
			nL.document.close();
			nL.visibility = 'show';

			document.formlayer = nL;

		}
	}

	function HandleResize()
	{
		location.reload();
		return false;
	}

	if (NS4)
	{
		SafeAddOnload(HandleOnload);
		window.captureEvents(Event.RESIZE);
		window.onresize = HandleResize;
	}
	

