// File: smartfield.js
// Description: Form-fill helper
// Site: mortgage loan
// Version "Major.Minor.DDMMYY.Build":
//  1.0.060227.1: 

var m_c; // To register mouse click


link_form();
prepopForm(document.forms.frm);


if(document.forms.frm.elements.length > 0) document.forms.frm.elements[0].focus(); // Set default focus to first form element


function prepopForm(frm)
// prepop form with information from querystring
{
	if(window.location.toString().indexOf('?') == -1) return;
	
	var pair =  window.location.toString().split('?')[1].split('&'); // Parse querystring
	
	for(var ip = 0; ip < pair.length; ip++) {
		if(pair[ip].indexOf('=') != -1) {
			var key = pair[ip].split('=')[0];
			var val = unescape(pair[ip].split('=')[1]);
			
			for(var ie = 0; ie < frm.elements.length; ie++) {
				x = frm.elements[ie];
				if(x.name == key) {
					switch(x.type) {
						case 'radio':	 if(x.value == val) x.checked = true; break;
						case 'checkbox': if(x.value == val) x.checked = true; break;
						default:		 x.value = val;			
					}
				}			
			}
		}
	}
}


function link_form()
{
	if(document.forms.frm.elements.length == 0) return;

	o1 = document.forms.frm.elements[0];
	for(var i = 1; i < document.forms.frm.elements.length; i++ ) {
		var o2 = document.forms.frm.elements[i];
		var type1 = o1.type;
		var type2 = o2.type;

		if(o2.type != 'hidden') {
			if(o1.name == 'app_email' || o1.name == 'email') {
				em_(o1, o2);
			}
			else if(o1.name.match(/app_date_dob|inc_next_date|inc_next_date2/i)) {
				dt_(o1, o2);
			}
			else if(type1 == 'text') {
				txt_(o1, o2);
			}
			else if(type1 == 'select-one') {
				cbo_(o1, o2);
			}
			o1 = o2;	
		}

	}
}

function txt_(oT1, oT2)
{
	if(oT1 != null && oT2 != null) {
		oT1.onkeyup = function(event) 
		{ 
			if (!event) event = window.event;
			if(!isCtrlChr(event.keyCode) && oT1.value.length == oT1.maxLength) {
				if(oT2.focus) oT2.focus();
				if(oT2.select) oT2.select();
			}
		};
	}
}

function cbo_(oC1, oC2)
{
	if(oC1 != null && oC2 != null) {
		oC1.onfocus  = function(event) { m_c = false; }
		oC1.onclick  = function(event) { m_c = true; }
		oC1.onchange = function(event) 
		{ 
			if(m_c)	{
				if(oC2.focus) oC2.focus();
				if(oC2.select) oC2.select();
			}
			
			m_c = false;
		}
	}
}

function dt_(oD, oT)
{
	if(oD != null && oT != null) {
		oD.onkeyup = function(event)
		{
			if (!event) event = window.event;
			if(!isCtrlChr(event.keyCode) && oD.value.match(/\d{2}\/\d{2}\/\d{4}/)) {
				if(oT.focus) oT.focus();
				if(oT.select) oT.select();			
			}
		};
	}
}

function em_(oEm, oT)
{
	if(oEm != null && oT != null) {
		oEm.onkeyup = function(event)
		{
			if (!event) event = window.event;
			if(!isCtrlChr(event.keyCode) && oEm.value.match(/@(edebitpay\.com|netscape\.com|myway\.com|aim\.com|excite\.com|adelphia\.net|gmail\.com|cox\.net|verizon\.net|netzero\.com|charter\.net|peoplepc\.com|yahoo\.com|hotmail\.com|msn\.com|comcast\.net|sbcglobal\.net|aol\.com|bellsouth\.net|earthlink\.net)/)) {
				if(oT.focus) oT.focus();
				if(oT.select) oT.select();			
			}
		};
	}
}

function isCtrlChr(k) {	return (k >= 35 && k <= 40) || k == 8 || k == 9 || k == 46 || k == 16; }


