/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}

function printField(id) {
   var s = $(id).value;
  
   var regExp=/\n/gi;
   s = s.replace(regExp,'<br>');
   pWin = window.open('','pWin','location=yes, menubar=yes, toolbar=yes');
   pWin.document.open();
   pWin.document.write('<html><head></head><body>');
   pWin.document.write(s);
   pWin.document.write('</body></html>');
   pWin.print();
   pWin.document.close();
   pWin.close();
   
}


function disableCheck(id){

	var a = $(id);
	if(a.checked = true){
		a.checked=false;
	}else{
		a.checked=true;
	}
	
}

function checkTandC(){
	
	var read = $('readTandC').value;
	var checked = $('tandcagree').checked;
	if(read == 'false' || !checked){
		alert('You must scroll to the bottom of the Terms & Conditions section, and check the I Have Read and Accept This Agreement checkbox before continuing to the next section of the Application.');
	}else{
		$('rates').submit();
	}

}

function checkAnnualVolume(vol, acctType){

	var int;
	int = parseInt(vol);
		
	if(int <= 4999 || vol == ""){
		$('amex_disc_rate').disabled = true;
		$('amex_disc_rate_monthly').disabled = false;
		$('amex_disc_rate_monthly').checked = true;
	}else{
		$('amex_disc_rate_monthly').disabled = true;
		$('amex_disc_rate').disabled = false;
		$('amex_disc_rate').checked = true;
		if(acctType == 'Retail'){
			$('txn_fee_retail').checked = true;
		}
	}
	
}

function checkSigs( checkACH ){

	var consent_1_check = $('consent_1_check').checked;
	var consent_2_check = ($('consent_2_check') == null) ? "true" : $('consent_2_check').checked;	
	var agree_resolution = ($('agree_resolution') == null) ? "true" : $('agree_resolution').checked;
	var consent_1_check_guaranty = $('consent_1_check_guaranty').checked;
	//var consent_2_check_guaranty = ($('consent_2_check_guaranty') == null) ? "true" : $('consent_2_check_guaranty').checked;
	var consent_1_check_disc = $('consent_1_check_disc').checked;
		
	var app_1_name = parseInt($('applicant_1_name').value.length);
	var app_1_title = parseInt($('applicant_1_title').value.length);
	//var app_2_name = parseInt(($('applicant_2_name') == null) ? "1" : $('applicant_2_name').value.length);
	//var app_2_title = parseInt(($('applicant_2_title') == null) ? "1" : $('applicant_2_title').value.length);
	var officer_sig = parseInt(($('officer_sig') == null) ? "1" : $('officer_sig').value.length);
	var app_1_name_guaranty = parseInt($('applicant_1_name_guaranty').value.length);
	//var app_2_name_guaranty = parseInt(($('applicant_2_name_guaranty') == null) ? "1" : $('applicant_2_name_guaranty').value.length);
	var app_1_name_disc = parseInt($('applicant_1_name_disc').value.length);
	
	
	var good = "true";
	
	if(app_1_name == 0 || app_1_title == 0 || !(consent_1_check))
	{
		alert('Applicant 1 must fill in their name and title AND check the I Agree to this Acknowledgement checkbox for the Acknowledgements section.');
		good = "false";
	}
	/*
	if(app_2_name == 0 || app_2_title == 0 || !(consent_2_check))
	{
		alert('Applicant 2 must fill in their name and title AND check the I Agree to this Acknowledgement checkbox for the Acknowledgements section.');
		good = "false";
	}
	*/
	if(app_1_name_guaranty == 0 || !(consent_1_check_guaranty))
	{
		alert('Applicant 1 must fill in their name AND check the I Agree to this Acknowledgement checkbox for the Individual Guaranty section.');
		good = "false";
	}
	
	/*
	if(app_2_name_guaranty == 0 || !(consent_2_check_guaranty))
	{
		alert('Applicant 2 must fill in their name AND check the I Agree to this Acknowledgement checkbox for the Individual Guaranty section.');
		good = "false";
	}
	*/
	
	if(app_1_name_disc == 0 || !(consent_1_check_disc))
	{
		alert('Applicant 1 must fill in their name AND check the I Agree to this Disclosure checkbox for the Disclosure section.');
		good = "false";
	}
	
	if (checkACH == 'true')
	{
		var ach_app1_name = parseInt($('applicant_1_name_ach').value.length);
		var ach_app1_check = $('consent_1_check_ach').checked;
		
		if(ach_app1_name == 0 || !(ach_app1_check))
		{
			alert('Applicant 1 must fill in their name AND Check the I Accept this ACH Authorization for the GoEmerchant ACH Authorization section.');
			good = "false"
		}
		
	}
	
	if(good == "true")
	{
		//alert('ready to go');
		confirmSubmit();
	}
}

function updateField( field, val )
{
	$(field).value = val;
}

function confirmSubmit()
{
	var x=window.confirm("By clicking this button you are submitting your application, you will not be able to modify this further.  Are you sure you are finished modifying your application?")
	if (x)
	{
		$('esig').submit();
		return true;
	}
	else
	{
		return false;
	}
}

function setDefaultCheckbox( id, val )
{
	
	if(id)
	{
		if(val == 'True')
		{
			document.getElementById(id).checked = true;
		}
	}
}

