var start=0;
var end=0;

var pos1=0;
var pos2=0;
var speed1 = 3;
var id='el1';
var timeoutID='';

function getBounds(element)
{
  var left = element.offsetLeft;
  var top = element.offsetTop;
  for (var parent = element.offsetParent; parent; parent = parent.offsetParent)
  {
    left += parent.offsetLeft;
    top += parent.offsetTop;
  }
  return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}

function set_coord(id_p,id)
{
	i=1;
	el_id=id_p+''+i;
	
	while(document.getElementById(el_id))
	{
		var element = document.getElementById(el_id);
        var bounds = getBounds(element);
        
		arrow=document.getElementById(id+''+i);
		arrow.style.top=bounds.top+9;
		arrow.style.left=(bounds.left+bounds.width)-9;
		
		pos1=(bounds.left+bounds.width)-9;
		
		//start=bounds.left+60;
		end=(bounds.left+bounds.width)-9;
		
		i++;
		el_id=id_p+''+i;
	}
}

function move(nid){
	id=nid;
	
	var element = document.getElementById(nid+'_1');
    var bounds = getBounds(element);
	start=bounds.left;
	
	next();
}
function next() {
	pos1 -= speed1;
    if (pos1 < start)
	{
		pos2=pos1;
		pos1=end;
		next_back();
	}
	else
	{
		document.getElementById(id).style.left = pos1;
	    timeoutID=window.setTimeout("next();",10);
	}
}

function next_back() {
	pos2 += speed1;	
    if (pos2 > end) {}
	else
	{
    	document.getElementById(id).style.left = pos2;
		timeoutID=window.setTimeout("next_back();",10);
	}	
 }
function back() {
    clearTimeout(timeoutID);
	pos1=end;
	document.getElementById(id).style.left = end;
}


function trim(value){
	return value.replace(/^\s+$/, '');
}

function validate_apply(){
	if(trim(document.getElementById('company').value)=='')
	{
		alert('Company name not set');
		return false;
	}
	else if(trim(document.getElementById('first_name').value)=='')
	{
		alert('First name not set');
		return false;
	}
	else if(trim(document.getElementById('last_name').value)=='')
	{
		alert('Last name not set');
		return false;
	}
	else if(trim(document.getElementById('email').value)=='')
	{
		alert('E-mail address not set');
		return false;
	}
	else if(trim(document.getElementById('phone').value)=='')
	{
		alert('Phone not set');
		return false;
	}
	else if(document.getElementById('visa_ms').options[document.getElementById('visa_ms').selectedIndex].value=='')
	{
		alert('Not set How much do you process in Visa/Mastercard from your customers each month');
		return false;
	}
	return true;
}
function validate_contact()	{
	if(trim(document.getElementById('company').value)=='')
	{
		alert('Company name not set');
		return false;
	}
	else if(trim(document.getElementById('first_name').value)=='')
	{
		alert('First name not set');
		return false;
	}
	else if(trim(document.getElementById('last_name').value)=='')
	{
		alert('Last name not set');
		return false;
	}
	else if(trim(document.getElementById('email').value)=='')
	{
		alert('E-mail address not set');
		return false;
	}
	else if(trim(document.getElementById('phone').value)=='')
	{
		alert('Phone not set');
		return false;
	}
	return true;
}
function validate_partner()	{
	if(trim(document.getElementById('company').value)=='')
	{
		alert('Company name not set');
		return false;
	}
	else if(trim(document.getElementById('first_name').value)=='')
	{
		alert('First name not set');
		return false;
	}
	else if(trim(document.getElementById('last_name').value)=='')
	{
		alert('Last name not set');
		return false;
	}
	else if(trim(document.getElementById('email').value)=='')
	{
		alert('E-mail address not set');
		return false;
	}
	else if(trim(document.getElementById('phone').value)=='')
	{
		alert('Phone not set');
		return false;
	}
	else if(document.getElementById('prev_merchant_y').checked)
	if(trim(document.getElementById('prev_cash_company').value)=='')
	{
		alert('Not set for which company');
		return false;
	}
	return true;
}

function gomenu(file, param)
{
var p1='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="181" height="151"><param name="movie" value="';
var p2='"><param name="flashvars" value="'+param+'"><param name="quality" value="high">';
var p3='<embed src="';
var p4='" flashvars="'+param+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="181" height="151"></embed></object>';
var tag=p1 + file + p2 + p3 + file + p4;
document.write (tag);
}

if('undefined' == typeof String.prototype.trim) {
	String.prototype.trim = function() {
		return this.replace(/^\s+/, '').replace(/\s+$/, '');
  	}
}

function trim(value){
	return value.replace(/^\s+$/, '');
}

function show_w_icon(errors)
{
    for(x in errors)
    {
        document.getElementById(errors[x]).innerHTML='<img src="images/apf_er.gif" align="absmiddle">';
    }
}

function cp_lname(cbox){
	if(cbox.checked)
	{
		document.getElementById('b_legal_name').value = document.getElementById('b_dba').value;
	}
	else
	{
		document.getElementById('b_legal_name').value = '';
	}
}

function cp_laddress(cbox){
	if(cbox.checked)
	{
		document.getElementById('b_legal_address').value = document.getElementById('p_address').value;
		document.getElementById('b_city').value = document.getElementById('p_city').value;
		document.getElementById('b_zip').value = document.getElementById('p_zip').value;
		
		for(i=0; i<document.getElementById('b_state').options.length; i++)
		{
			if(document.getElementById('b_state').options[i].value == document.getElementById('p_state').value) document.getElementById('b_state').selectedIndex = i;
		}
	}
	else
	{
		document.getElementById('b_legal_address').value = '';
		document.getElementById('b_city').value = '';
		document.getElementById('b_state').selectedIndex = 0;
		document.getElementById('b_zip').value = '';
	}
}

var show_box = false;
function check_ownership_perc(){
	if(parseFloat(document.getElementById('o1_percentage').value) < 51)
	{
		document.getElementById('add_owner_box').className='visible';
		show_box = true;
		document.getElementById('owner2_exist').value=1;
	}
	else
	{
		document.getElementById('add_owner_box').className='hidden';
		show_box = false;
		document.getElementById('owner2_exist').value=0;
	}
}

function validMoney(money){
	if(/^[\d]{1,}(,[\d]{3})?(\.[\d]{1,2})?$/.test(money))
	{
		return true;
	}
	return false;
}

function input_signed(){
	if(document.getElementById('signed').value.trim()!='')
	{
		document.getElementById('terms_cond').disabled='';
	}
	else
	{
		document.getElementById('terms_cond').disabled='disabled';
		document.getElementById('terms_cond').checked='';
	}
}

function check_c(){
	document.getElementById('qualify_for').value = '';
	
	m_1 = document.getElementById('1_month').value.trim();
	m_2 = document.getElementById('2_month').value.trim();
	m_3 = document.getElementById('3_month').value.trim();
	m_4 = document.getElementById('4_month').value.trim();
	
	if(m_1!='' && validMoney(m_1) && m_2!='' && validMoney(m_2) && m_3!='' && validMoney(m_3) && m_4!='' && validMoney(m_3))
	{
		m1 = parseFloat(m_1.replace(/,/, ''));
    	m2 = parseFloat(m_2.replace(/,/, ''));
    	m3 = parseFloat(m_3.replace(/,/, ''));
	    m4 = parseFloat(m_4.replace(/,/, ''));
	
	    q =round_f((((m1+m2+m3+m4))/4)*1.25);
        document.getElementById('qualify_for').value = q;
	
		document.getElementById('calculate').className = 'visible';
	}
	else
	{
		document.getElementById('calculate').className = 'hidden';
		document.getElementById('qualifyBox').style.visibility = 'hidden';
		document.getElementById('qualify_for').value = '';
	}
	
}

function  setDays(yId,mId,dId){
	day_el=document.getElementById(dId);
	
	selected = document.getElementById(dId).selectedIndex;
		
	for(i=day_el.options.length; i>0; i--) day_el.remove(i);
	
	if(document.getElementById(mId).selectedIndex!=0)
	{
		if(document.getElementById(yId).selectedIndex==0)
		{
			y = new Date().getFullYear();
		}
		else
		{
			y = document.getElementById(yId).options[document.getElementById(yId).selectedIndex].value;
		}
		m = document.getElementById(mId).options[document.getElementById(mId).selectedIndex].value;
		daysMonth = new Date(y,m,0).getDate();
		
		for(i=1;i<=daysMonth;i++)
		{
			var new_opt=document.createElement('option');	
			new_opt.setAttribute("value",i);
			new_opt.appendChild(document.createTextNode(i));
			day_el.appendChild(new_opt);
		}
		if(document.getElementById(dId).options[selected]) day_el.selectedIndex=selected;
		else day_el.selectedIndex=0;
	}
}

function SetSelDay(dId)
{
	day_el = document.getElementById(dId);
	if(document.getElementById(dId+'_s')!='')
	{
		selected = document.getElementById(dId+'_s').value;
		if(day_el.options[selected]) day_el.selectedIndex=selected;
	}
}

function ValidatePhone(fId){
	
	currElId = '';
	if(document.activeElement) currElId = document.activeElement.id
	
	p1 = document.getElementById(fId+'1').value;
	p2 = document.getElementById(fId+'2').value;
	p3 = document.getElementById(fId+'3').value;
	p4Elt = document.getElementById(fId+'4');
	p4 = p4Elt ? pfElt.value : '';
	
	if(p1=='' && p2=='' && p3=='') phoneValue = '';
	else phoneValue = p1+'-'+p2+'-'+p3;
	
	if(p4!='') phoneValue=phoneValue+'-'+p4;
	
	if(currElId!=fId+'1' && currElId!=fId+'2' && currElId!=fId+'3' && currElId!=fId+'4')
	ValidateField(fId, phoneValue);
}

function ValidateCellFax(fId){
	currElId = '';
	if(document.activeElement) currElId = document.activeElement.id
	
	p1 = document.getElementById(fId+'1').value;
	p2 = document.getElementById(fId+'2').value;
	p3 = document.getElementById(fId+'3').value;
	
	if(p1=='' && p2=='' && p3=='') phoneValue = '';
	else phoneValue = p1+'-'+p2+'-'+p3;
	
	if(currElId!=fId+'1' && currElId!=fId+'2' && currElId!=fId+'3')
	ValidateField(fId, phoneValue);
}

function ValidateFedTaxId(fId, entityTypeId){
	
	entityTypeElt = document.getElementById(entityTypeId);
	entityType = entityTypeElt.options[entityTypeElt.selectedIndex].value;
	
	currElId = '';
	if(document.activeElement) currElId = document.activeElement.id
	
	f1 = document.getElementById(fId+'1').value;
	f2 = document.getElementById(fId+'2').value;
	
	if(f1=='' && f2=='') fedtaxid = '';
	else fedtaxid = f1+'-'+f2;
	
	if(currElId!=fId+'1' && currElId!=fId+'2')
	ValidateField(fId, fedtaxid, entityType);
}

function ValidateSSN(fId){
	currElId = '';
	if(document.activeElement) currElId = document.activeElement.id
	
	f1 = document.getElementById(fId+'1').value;
	f2 = document.getElementById(fId+'2').value;
	f3 = document.getElementById(fId+'3').value;
	
	if(f1=='' && f2=='' && f3=='') ssn = '';
	else ssn = f1+'-'+f2+'-'+f3;
	
	if(currElId!=fId+'1' && currElId!=fId+'2' && currElId!=fId+'3')
	ValidateField(fId, ssn);
}

function ValidateDate(fId){
	currElId = '';
	if(document.activeElement) currElId = document.activeElement.id
	
	m = document.getElementById(fId+'_m').options[document.getElementById(fId+'_m').selectedIndex].value;
	d = document.getElementById(fId+'_d').options[document.getElementById(fId+'_d').selectedIndex].value;
	y = document.getElementById(fId+'_y').options[document.getElementById(fId+'_y').selectedIndex].value;
	
	if(m=='' && d=='' && y=='') date = '';
	else date = m+'-'+d+'-'+y;
	
	if(currElId!=fId+'_m' && currElId!=fId+'_d' && currElId!=fId+'_y')
	ValidateField(fId, date);
}

function round_f(v){
	v = v*100;
	v = Math.round(v);
	v = v/100;
	return v;
}

function calcQualify(){
	if(q>=5000)
	{
	   document.getElementById('qualifyBox').innerHTML='Your business qualifies for up to <br>$'+q;
	}
	else
	{
	   document.getElementById('qualifyBox').innerHTML='Your monthly volume does not meet approval criteria';
	}
	document.getElementById('qualifyBox').style.visibility = 'visible';
}

function nextElement(cID, nID, maxl){
	value = document.getElementById(cID).value.trim();
	if(value.length==maxl && kCode!=9&& kCode!=37 && kCode!=38 && kCode!=39 && kCode!=40)
	{
		document.getElementById(nID).focus();
	}
}

document.onkeydown = registerKey;

var kCode = 0;
function registerKey(e) 
{
    if (!e) e = window.event;
    kCode = e.keyCode;
}
