/**
 * Function to attach event handler to the jump select box if
 * it is present and remove the "go" button"
 *
 * @author James R. Carr
 * @param HTMLElement
 * @return void
 */
function addNavigationJump(jump)
	{
	if(jump)
		{
		var button = document.getElementById('jump_button');
		button.style.display = 'none';
		var jumpToThePages = function(e)
			{
			button.click();		
			}
		AttachEvent(jump, 'change', jumpToThePages, false);
		}
	}

/**
 * Creates a popup for the "download pdf" link. This could actually
 * be used to create a popup for any link actually.
 * 
 * @author James R. Carr
 * @param HTMLElement
 * @return void
 */
function makeLinkPopup(link)
	{
	if(!link) return false;
	var url = link.href;
	
	var openWindow = function (e)
		{
		stopDefaultAction(e);
		window.open(url);
		}
	AttachEvent(link, 'click', openWindow, false);
	}
/**
 * Displays a number of specified nodes containign a specified
 * tag name in a spaecified nodeset based on the value of the specified
 * element.
 *
 * @author James R. Carr
 * @param HTMLElement el
 * @param HTMLCollection nodeset
 * @param string tagname
 * @return void
 */
function displayNodesFromElValue(el, nodeset, tagName)
	{
	var j = 0; 
	for(i = 0; i < nodeset.length; i++)
		{
		if(nodeset[i].tagName && nodeset[i].style)
			{
			if(nodeset[i].tagName.toUpperCase() == tagName.toUpperCase())
				{
				if(j < parseInt(el.value))
					{
					nodeset[i].style.display = 'block';
					}
				else
					{
					nodeset[i].style.display = 'none';
					}
				j++;
				}
			}
		}
	}

function callback()
	{
	var element = null;
	this.nodeset = null;
	this.tagName = null;
	this.getElement = function()
		{
		return this.element;
		}
	}
callback.prototype.callbackFunc = function (e)
	{
	alert(this.getElement() + " " + this.nodeset + " " + this.tagName);
	alert(this.self.toSource());
	}

/**
 * Used to create a callback on change for elements in relation
 * to a select element's value
 *
 * @author James R. Carr
 * @param HTMLSelectElement selEl
 * @param HTMLElement container
 * @param string tag
 * @return void
 */	
function displayBoxes(selEl, container, tag)
	{
	if(container)
		{
		nodeset = container.childNodes;
		}
	if(selEl && nodeset)
		{
		// create callback
		foo = new callback();
		foo.element = selEl.id;
		foo.nodeset = container.id;
		foo.tagName = tag;
		alert(foo.toSource());
		AttachEvent(selEl, 'change', foo.callbackFunc, false);
		displayNodesFromElValue(selEl, nodeset, tag);
		}
	}
	
function main()
	{
	addNavigationJump(document.getElementById('jump'));
	makeLinkPopup(document.getElementById('application_download'));
	
	/*// dyanmic lists
	displayBoxes(document.getElementById('other_debts'), document.getElementById('debtBoxes'), 'fieldset');
	displayBoxes(document.getElementById('num_assets'), document.getElementById('otherAssetBoxes'), 'fieldset');
	displayBoxes(document.getElementById('number_autos'), document.getElementById('autos'), 'fieldset');*/
	}
	
AttachEvent(window, 'load', main, false);

function validate(formName)
{
	if(formName == 'introduction')
	{
		MM_validateIntroduction('ProceedsUsedFor','','R','AmountRequested','','R');
		return document.MM_returnValue;
	}
	else if(formName == 'borrower')
	{
		MM_validateBorrower('FirstName','','R','MiddleName','','R','LastName','','R','SocialSecurityNumber','','R');
		return document.MM_returnValue;
	}
	else if(formName == 'coborrower')
	{
		MM_validateBorrower('FirstName','','R','MiddleName','','R','LastName','','R','SocialSecurityNumber','','R');
		return document.MM_returnValue;
	}
	else if(formName == 'signature')
	{
		MM_validateSignature();
		return document.MM_returnValue;
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateIntroduction()
{ //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateIntroduction.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } 
  
	myOption = -1;
	for (i=document.introduction.secured.length-1; i > -1; i--)
	{
		if (document.introduction.secured[i].checked)
			myOption = i;
	}

	if (myOption == -1)
		errors += '- Secured Credit is a required field. \n';
	
	
	myOption = -1;
	for (i=document.introduction.apply_type.length-1; i > -1; i--)
	{
		if (document.introduction.apply_type[i].checked)
			myOption = i;
	}

	if (myOption == -1)	
		errors += '- Type of Application is a required field. \n';

	if(document.introduction.PaymentDateDesired.value == '0')
	{
		errors += '- Payment Date Desired is a required field. \n';
	}

	if (errors)
		 alert('The following error(s) occurred:\n'+errors);
	
	document.MM_returnValue = (errors == '');
}


function MM_validateBorrower()
{ //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateBorrower.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } 
	
	if (errors)
		 alert('The following error(s) occurred:\n'+errors);
	
	document.MM_returnValue = (errors == '');
}

function MM_validateSignature()
{
	var errors ='';
	var main = document.getElementById('checkboxes1');
	var collection = main.getElementsByTagName('input');
	
	if(!collection[0].checked)
	{
		errors += '- The applicant must agree to the terms and conditions before the application can be processed.\n';
	}
	
	
	if(document.getElementById('checkboxes2'))
	{
		var main2 = document.getElementById('checkboxes2');
		var collection2 = main2.getElementsByTagName('input');
		
		if(!collection2[0].checked)
		{
			errors += '- The coapplicant must agree to the terms and conditions before the application can be processed.\n';
		}
	}
	
	if (errors)
		 alert('The following error(s) occurred:\n'+errors);
	
	document.MM_returnValue = (errors == '');
}