function swap_img(id,img_src) 
{
		document.images[id].src=img_src;			
}
function register_check(theform){
	if(theform.email.value=="") {alert("Please Enter the Email Address"); theform.email.focus(); return false;}
	if(theform.password.value=="") {alert("Please Enter Password"); theform.password.focus(); return false;}
	if(theform.confirm_password.value=="") {alert("Please Confirm Password"); theform.confirm_password.focus(); return false;}
	if(theform.confirm_text.value=="") {alert("Please Confirm Text"); theform.confirm_text.focus(); return false;}
	if(theform.confirm_text.value!=theform.original_text.value) {alert("Please Confirmation Text does not match the original"); theform.confirm_text.focus(); return false;}
	if(theform.first_name.value=="") {alert("Please Enter Your First Name"); theform.first_name.focus(); return false;}
	if(theform.last_name.value=="") {alert("Please Enter Your Last Name"); theform.last_name.focus(); return false;}
	if(theform.agreement.value=="") {alert("Please Read and agree to the RFQglobal.com Terms of Use Agreement"); theform.agreement.focus(); return false;}
}
function string_length(str, id){
	id.value = str.length;
	return str.length;	
}
function pw_check(theform){
	if(theform.old_pw.value=="") {alert("Please Enter Old Password"); theform.r_title.focus(); return false;}
	if(theform.new_pw.value=="") {alert("Please Enter New Password"); theform.new_pw.focus(); return false;}
	if(theform.new_pw1.value=="") {alert("Please Re-enter New Password"); theform.new_pw1.focus(); return false;}
	if(theform.new_pw.value!=theform.new_pw1.value) {alert("Password do not match"); theform.new_pw1.focus(); return false;}
}

function confirm_delete(new_link, seq)
{
	var answer = confirm ("Are you willing to delete?");
	if (answer) window.location.href= new_link+'&seq='+seq +'&action=delete&act=delete';
	else{
		alert("Canceled");
	}

}

function confirm_delete2(new_link, seq)
{
	var answer = confirm ("Are you willing to delete?");
	if (answer) window.location.href= new_link+'&seq='+seq +'&act=delete';
	else{
		alert("Canceled");
	}

}

function confirm_answer(id, message, url, val, hide_name)
{
	var answer = confirm (message);
	if(answer && hide_name) hide(hide_name);
	if (answer && url){
	  try { 
		  var win = window.open(url,"",'');
		  win.focus(); 
		} 
	  catch(e) { 
		  alert("Pop-up Blocked!"); 
		} 
	}
	else if (answer && !url){
		if(val=='y'){
			eval("document." + id + "[1].checked=true;");
		}
		else eval("document." + id + "[0].checked=true;");
	}
}

function submit_form(id)
{
     	eval("document.all." + id + ".submit();");
}

function send_form(obj)
{
     	obj.submit();
}
function print_submit(the_form, type){
	if(type==1) {
		open_window('', "yes","yes",'640', '700',20,50 ,'n_window');
		update_var('status', '');
		update_var('action', 'print');
		the_form.target = "n_window";
	}
	else if(type==2) {
		n_window = open_window('print.php', "yes","yes",'640', '700',20,50 ,'n_window');
		the_form.target = n_window;
	}
	else{
		update_var('status', 'submit');
		update_var('action', 'new');
		the_form.target = "_self";
	}
}
function nwin_submit_form(act, the_form){
	the_form.target = "_blank";
    the_form.action=act;
}
function reset_submit_form(act, the_form){
	the_form.target = "";
    the_form.action=act;
}

function show(id)
{
   	eval("document.all." + id + ".style.display = 'block';");
}

function hide(id)
{
   	eval("document.all." + id + ".style.display = 'none';");
}

function open_window(url, scroll_bars, resizable, width, height, top, left, name)
{
  try { 
	  var win = window.open(url,name,"scrollbars="+ scroll_bars +",toolbar=no,location=no,directories=no,status=no,resizable="+ resizable +",menubar=no,width="+ width +", height="+ height +", top="+ top +",left=" + left);
	  win.focus(); 
	} 
  catch(e) { 
	  alert("Pop-up Blocked!"); 
	} 
	return win;
}
function guide_window(url)
{
	open_window(url, "yes","yes",'800', '700',20,50,'');	
}
function small_window(url)
{
	open_window(url, "yes","yes",'600', '420',20,50,'');	
}
function medium_window(url)
{
	open_window(url, "yes","yes",'680', '700',20,50,'');	
}
function tiny_window(url)
{
	open_window(url, "yes","yes",'570', '320',20,50,'');	
}
function homepage_window(url)
{
	open_window(url, "yes","yes",'800', '700',20,50,'');	
}
function update_opener(type, id, val)
{
	if(type=='i') opener.document.getElementById(id).innerHTML= val;
	else opener.document.getElementById(id).value = val;
}
function img_select_update(val, txt)
{
		myselect= document.upload_form.img_list;
		if(myselect.options) var len= myselect.options.length * 1; 
		else len=0;
		
		myselect.options[len] = new Option(txt,val);

}
function select_list_update(id, val, txt, type)
{
	myselect= document.getElementById(id);
	if(myselect.options) var len= myselect.options.length * 1; 
	else len=0;
		
	myselect.options[len] = new Option(txt,val);
}
function get_select_list(id)
{
	myselect= document.getElementById(id);
	if(myselect.options) var len= myselect.options.length * 1; 

	var result;
	result = ",";
	for(var i=0; i < len ; i++){
		result = result + myselect.options[i].value + ',';
	}
	return result;
}
function image_update(list_name, txt_name, url)
{
	if(!list_name) list_name='file_list';
	if(!txt_name) txt_name='txt_file_list';
	var num=document.getElementById('num').value * 1;
	var val='';
	var txt_val='<div style=\"width:480\">';
	for(var i=1; i<=num; i++){
		if(document.all.item("image_url"+i)){ 
			img_url= document.getElementById("image_url"+i).value;
			if(img_url) {
				if(val=='') val= img_url;
				else val+=','+img_url;
				
				txt_val += '<div style=\"float:left; margin-left:3\"><img src=\"' + url + img_url + '\" width=60 height=40 style=\'border: cccccc solid 1px\'></div>';
			}
		}
	}
	if(val) opener.document.getElementById(list_name).value=val;
	if(txt_val) opener.document.getElementById(txt_name).innerHTML= txt_val + '</div><br>';
	window.close();
}
function openwin(url)
{
  window.open(url,"","scrollbars=yes,toolbar=no,location=no,directories=no,status=no,resizable=yes,menubar=no,width=640, height=700,top=10,left=4");
}
function generalwin(url)
{
  window.open(url,"","scrollbars=yes,toolbar=yes,location=no,directories=no,status=no,resizable=yes,menubar=no,width=640, height=700,top=10,left=4");
}
function aspacewin(url)
{
  $result = window.open(url,"","scrollbars=no,toolbar=no,location=no,directories=no,status=no,resizable=yes,menubar=no,width=900, height=600,top=100,left=260");
  return $result;
}

function openpic_byid(imgName, path)
{
	var new_pic;
	img_src = document.images[imgName].src;		
	oimg=new Image();
	 oimg.src=img_src;
	 width =oimg.width;
	height=oimg.height;

	url= path+"templates/popup_img.php?image=" + img_src;
	 new_pic= window.open(url,new_pic,"scrollbars=no,toolbar=no,location=no,directories=no,status=no, width="+width + ", height=" + height + ", resizable=yes,menubar=no,top=100,left=260").focus();
}

function openpic_byurl(img_url, path)
{
	oimg=new Image();
	 oimg.src=img_url;
	 width =oimg.width+30;
	height=oimg.height+20;
	url= path+"templates/popup_img.php?image=../" + path+img_url;
	window.open(url,"","scrollbars=no,toolbar=no,location=no,directories=no,status=no, width="+width + ", height=" + height + ", resizable=yes,menubar=no,top=0,left=260").focus();
}

function get_cookie(name) { 
	var Found = false 
	var start, end 
	var i = 0 
	
	while(i <= document.cookie.length) { 
	start = i 
	end = start + name.length 
	
	if(document.cookie.substring(start, end) == name) { 
	Found = true 
	break 
	} 
	i++ 
	} 
	if(Found == true) { 
	start = end + 1 
	end = document.cookie.indexOf(";", start) 
	if(end < start) 
	end = document.cookie.length 
	return document.cookie.substring(start, end) 
	} 
	return "" 
} 


function set_cookie( name, value, expiredays ) 
{ 
	var todayDate = new Date(); 
	todayDate.setDate( todayDate.getDate() + expiredays ); 
	document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";" 
} 

function closeWin(id) {
{ 
	if ( document.cnjform.notice.checked ) 
	set_cookie(id, "no" , 1); 
	} 
	top.close();
}

function go(id) 
{
	window.location=document.getElementById(id).value
}


function update_bool_var(id, val)
{ 
	unset_sort();
	if(val=='1') document.getElementById(id).value=0;
	else if(val=='0'&& val!='') document.getElementById(id).value=1;
	else{
		document.getElementById(id).value=1;
	}
} 

function update_var(id, val)
{ 
	document.getElementById(id).value=val;
}

function update_var1(id, name, val)
{ 
	eval("document." + id + "." + name + ".value = '"+ val + "'");
}

function update_sel_list(id, val)
{ 
	var url="templates/print_select_list.php?id"+id + "&keyword=" + val;
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}

function select_add(id, val, txt)
{
		myselect= document.getElementById(id);
		if(myselect.options) var len= myselect.options.length * 1; 
		else len=0;
		
		myselect.options[len] = new Option(txt,val);

}

function select_del(id)
{
		myselect= document.getElementById(id);
		for (i = myselect.length - 1; i>=0; i--) {
			if (myselect.options[i].selected) {
			  myselect.remove(i);
			}
		  }
}

function fitPic() {
      if (window.innerWidth){
          iWidth = window.innerWidth;
          iHeight = window.innerHeight;
      }else{
          iWidth = document.body.clientWidth;
          iHeight =document.body.clientHeight;
      }
      iWidth = document.images[0].width - iWidth;
      iHeight = document.images[0].height - iHeight;
      window.resizeBy(iWidth, iHeight);
 };
 
function MakeFlash(Url,Width,Height){                 
  document.writeln("<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=\"" + Width + "\" height=\"" + Height + "\">"); 
  document.writeln("<param name=\"movie\" value=\"" + Url + "\">"); 
  document.writeln("<param name=\"quality\" value=\"high\" />");     
  document.writeln("<param name=\"wmode\" value=\"transparent\">"); 
  document.writeln("<embed src=\"" + Url + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"" + Width + "\"  height=\"" + Height + "\">"); 
  document.writeln("</object>");     
} 

function automate_used(theform){
	var price = theform.selling_price.value* 1.0;	
	var down = theform.down.value* 1.0;
	var term = theform.term.value* 1.0;
	var interest_rate = theform.interest_rate.value* 1.0;
	var doc_fee = theform.doc_fee.value* 1.0;
	var smog_fee = theform.smog_fee.value* 1.0;
	var used_license_fee = theform.used_license_fee.value* 1.0;
	var used_license_rate = theform.used_license_rate.value* 1.0;
	var tax_rate = theform.tax_rate.value* 1.0;
	var accessories = theform.accessories.value* 1.0;
	var days = theform.days.value;
    var first_payment = theform.first_payment.value;
	var last_payment = theform.last_payment.value;
	var total = 0.0;
	var tax = 0.0;
    var cash_price = 0.0;
	var total_down = 0.0;
	var f_charge = 0.0;
	var total_payment = 0.0;
	var total_sale = 0.0;

    var first = new Date();   
    first.setMonth(first.getMonth() + 2); 
	if( first.getMonth() * 1 == 0 ){
		first_month = 12;
	}
	else { first_month = first.getMonth(); }
	theform.first_payment.value = first_month + '-' + first.getDate()+ '-' + first.getYear();
	
	var last = new Date(); 
	last.setMonth(last.getMonth() + term+1);
	if( last.getMonth() * 1 == 0 ){
		last_month = 12;
	}
	else { last_month = last.getMonth(); }
	theform.last_payment.value = last_month + '-' + last.getDate()+ '-' + last.getYear();
	
	
	if(theform.used_license_expire.checked ){ 
		used_license_fee = used_license_fee + price * used_license_rate/100;
		theform.license_fee.value = used_license_fee.toFixed(2);
	}
	else {
		used_license_fee = theform.license_fee.value* 1.0;
	}
	tax = (price + doc_fee + smog_fee + accessories) * tax_rate / 100;
	total =  price + tax + used_license_fee + accessories + smog_fee + doc_fee;
	total_amount = total - down;
	
	cash_price = price + tax + accessories + doc_fee + smog_fee;
	total_down = down;
	
	
	theform.tax.value = tax.toFixed(2);
	theform.cash_price.value = cash_price.toFixed(2);
	theform.total_down.value = total_down.toFixed(2);
	theform.out_door.value = total.toFixed(2);
	theform.amount.value = total_amount.toFixed(2);
	
	var m_rate = theform.interest_rate.value / 12.0 /100;
	var payment = total_amount * Math.pow(1 + m_rate, term) * m_rate / (Math.pow(1+m_rate, term) -1);
		
	total_payment = payment * term;
	f_charge = total_payment - total_amount;
	total_sale = total + f_charge; 
    
   if(days=='45'){
		var payment = total_amount * Math.pow(1 + m_rate, term - 0.2825) * m_rate / (Math.pow(1+m_rate, term - 0.2825) -1);
		total_payment = payment * term;
		f_charge = total_payment - total_amount;
		total_sale = total + f_charge;		
	}
	
	theform.monthly_payment.value = payment.toFixed(2);
	theform.total_payment.value = total_payment.toFixed(2);
	theform.f_charge.value = f_charge.toFixed(2);
	theform.total_sale.value = total_sale.toFixed(2);
}

function automate_purchase(theform){
	var price = theform.selling_price.value* 1.0;
	var rebate = theform.rebate.value* 1.0;
	var down = theform.down.value* 1.0;
	var term = theform.term.value* 1.0;
	var interest_rate = theform.interest_rate.value* 1.0;
	var doc_fee = theform.doc_fee.value* 1.0;
	var license_fee = theform.license_fee.value* 1.0;
	var purchase_license_fee = theform.purchase_license_fee.value* 1.0;
	var purchase_license_rate = theform.purchase_license_rate.value* 1.0;
	var tax_rate = theform.tax_rate.value* 1.0;
	var accessories = theform.accessories.value* 1.0;
	var days = theform.days.value;
    var first_payment = theform.first_payment.value;
	var last_payment = theform.last_payment.value;
	
	var total = 0.0;
	var tax = 0.0;
	var cash_price = 0.0;
	var total_down = 0.0;
    var f_charge = 0.0;
	var total_payment = 0.0;
	var total_sale = 0.0;
	
	 var first = new Date();   
    first.setMonth(first.getMonth() + 2); 
	if( first.getMonth() * 1 == 0 ){
		first_month = 12;
	}
	else { first_month = first.getMonth(); }
	theform.first_payment.value = first_month + '-' + first.getDate()+ '-' + first.getYear();
	
	var last = new Date(); 
	last.setMonth(last.getMonth() + term+1);
	if( last.getMonth() * 1 == 0 ){
		last_month = 12;
	}
	else { last_month = last.getMonth(); }
	theform.last_payment.value = last_month + '-' + last.getDate()+ '-' + last.getYear();
	
	if(license_fee){
		purchase_license_fee = license_fee;
	}
	else{
		purchase_license_fee = purchase_license_fee + price * purchase_license_rate/100;
	}
	tax = (price + doc_fee + accessories) * tax_rate / 100;
	total =  price + tax + purchase_license_fee + accessories + doc_fee;
	total_amount = total - down - rebate;
	
	cash_price = price + tax + accessories + doc_fee;
	total_down = down + rebate;
	
	theform.tax.value = tax.toFixed(2);
	theform.cash_price.value = cash_price.toFixed(2);
	theform.total_down.value = total_down.toFixed(2);
	theform.license_fee.value = purchase_license_fee.toFixed(2);
	theform.out_door.value = total.toFixed(2);
	
	theform.amount.value = total_amount.toFixed(2);
	
	var m_rate = theform.interest_rate.value / 12.0 /100;

	var payment = total_amount * Math.pow(1 + m_rate, term - 0.2825) * m_rate / (Math.pow(1+m_rate, term - 0.2825) -1);
	
	total_payment = payment * term;
	f_charge = total_payment - total_amount;
	total_sale = total + f_charge;
	
	 if(days=='30'){
		var payment = total_amount * Math.pow(1 + m_rate, term) * m_rate / (Math.pow(1+m_rate, term) -1);
		total_payment = payment * term;
		f_charge = total_payment - total_amount;
		total_sale = total + f_charge;		
	}
	
	theform.monthly_payment.value = payment.toFixed(2);
	theform.total_payment.value = total_payment.toFixed(2);
	theform.f_charge.value = f_charge.toFixed(2);
	theform.total_sale.value = total_sale.toFixed(2);
}

function automate_lease(theform){
	var price = theform.cap_cost.value* 1.0;	
	var service_contract = theform.service_contract.value* 1.0;
	var cap_reduction = theform.cap_reduction.value* 1.0;
	var residual_rate = theform.residual_rate.value / 100.0;
	var msrp = theform.msrp.value* 1.0;
	var money_factor = theform.money_factor.value * 1.0;
	var bank_fee = theform.bank_fee.value* 1.0;
	var term = theform.term.value* 1.0;
	var rebate = theform.rebate.value* 1.0;
	var other = theform.other.value* 1.0;
	var tax_rate = theform.tax_rate.value* 1.0;
	var license_fee = theform.license_fee.value* 1.0;
	var lease_license_fee = theform.lease_license_fee.value* 1.0;
	var lease_license_rate = theform.lease_license_rate.value* 1.0;
	var security_deposit = theform.security_deposit.value* 1.0;
	var b_drive_off = theform.b_drive_off.value* 1.0;
	var c_drive_off = theform.c_drive_off.value* 1.0;
	var residual = theform.residual.value* 1.0;
	var first_payment = theform.first_payment.value;
	var last_payment = theform.last_payment.value;

	var no_drive_off;
	var net = 0.0;
	var depreciation = 0.0;
	var m_depreciation = 0.0;
	var b_tax = 0.0;
	var n_tax = 0.0;
	var c_tax = 0.0;
	var monthly_fee = 0.0;
	var other_tax = 0.0;
	var total_drive_off = 0.0;
	
	var b_monthly_payment = 0.0;
	var b_monthly_payment2 = 0.0;
	var n_monthly_payment = 0.0;
	var n_monthly_payment2 = 0.0;
	var c_monthly_payment = 0.0;
	var c_monthly_payment2 = 0.0;
	
	 var first = new Date();   
    first.setMonth(first.getMonth() + 2); 
	if( first.getMonth() * 1 == 0 ){
		first_month = 12;
	}
	else { first_month = first.getMonth(); }
	theform.first_payment.value = first_month + '-' + first.getDate()+ '-' + first.getYear();
	
	var last = new Date(); 
	last.setMonth(last.getMonth() + term+1);
	if( last.getMonth() * 1 == 0 ){
		last_month = 12;
	}
	else { last_month = last.getMonth(); }
	theform.last_payment.value = last_month + '-' + last.getDate()+ '-' + last.getYear();
	
	if(license_fee){
	lease_license_fee = license_fee; 
	}else {
    lease_license_fee = lease_license_fee + price * lease_license_rate/100;
	}
	
	net = price + service_contract - cap_reduction - rebate;
	if(residual ==0){
	   residual = msrp * residual_rate;
	}
	depreciation = net - residual;
	m_depreciation = depreciation / term;
	monthly_fee = (net + residual) * money_factor;
	other_tax = (cap_reduction + other) * tax_rate / 100;
	b_monthly_payment = m_depreciation + monthly_fee;
	b_tax = b_monthly_payment * tax_rate / 100;
	b_monthly_payment2 = b_monthly_payment + b_tax;
	
	theform.license_fee.value = lease_license_fee.toFixed(2);
	theform.net.value = net.toFixed(2);
	theform.residual.value = residual.toFixed(2);
	theform.depreciation.value = depreciation.toFixed(2);
	theform.m_depreciation.value = m_depreciation.toFixed(2);
	theform.monthly_fee.value = monthly_fee.toFixed(2);
	theform.other_tax.value = other_tax.toFixed(2);
	theform.b_monthly_payment.value = b_monthly_payment.toFixed(2);
	theform.b_tax.value = b_tax.toFixed(2);
	theform.b_monthly_payment2.value = b_monthly_payment2.toFixed(2);
	
	var b_drive_off = bank_fee + lease_license_fee + security_deposit + b_monthly_payment2 + other_tax + cap_reduction;
	
	theform.b_drive_off.value = b_drive_off.toFixed(2);
	
	var difference = c_drive_off - b_drive_off;
	
    if(c_drive_off) {
	net = price + service_contract - cap_reduction - rebate - difference;	
		if(residual == 0){
			residual = msrp * residual_rate;
		}
	depreciation = net - residual;
	m_depreciation = depreciation / term;
	monthly_fee = (net + residual) * money_factor;
	c_monthly_payment = m_depreciation + monthly_fee;
	c_tax = c_monthly_payment * tax_rate / 100;
	c_monthly_payment2 = c_monthly_payment + c_tax;
	
	theform.c_monthly_payment.value = c_monthly_payment.toFixed(2);
	theform.c_tax.value = c_tax.toFixed(2);
	theform.c_monthly_payment2.value = c_monthly_payment2.toFixed(2);
	}
	
	if(theform.no_drive_off.checked){	
	net = price + service_contract - cap_reduction - rebate + b_drive_off;	
		if(residual == 0){
			residual = msrp * residual_rate;
		}
	depreciation = net - residual;
	m_depreciation = depreciation / term;
	monthly_fee = (net + residual) * money_factor;
	n_monthly_payment = m_depreciation + monthly_fee;
	n_tax = n_monthly_payment * tax_rate / 100;
	n_monthly_payment2 = n_monthly_payment + n_tax;
	
	theform.n_monthly_payment.value = n_monthly_payment.toFixed(2);
	theform.n_tax.value = n_tax.toFixed(2);
	theform.n_monthly_payment2.value = n_monthly_payment2.toFixed(2);
	}

}

function lease_info(val, id){
	if(val=='Lease') show(id);
	else hide(id);
	
}
