// JavaScript Document
Object.prototype.isArray = function() 
{   
	return this.constructor == Array;
}

function trim(s)
{
	if (typeof s != 'string') return "";
	return s.trim();
}

String.prototype.trim = function() {   
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {   
	return this.replace(/^\s+/g,"");
}

String.prototype.rtrim = function() 
{   
	return this.replace(/\s+$/g,"");
}

function ajaxObject(url, callbackFunction) {  
		var that=this;        
		this.updating = false;  
		
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		} catch (e) {
			//alert("Permission UniversalBrowserRead denied.");
		}
		
		this.abort = function() {    
			if (that.updating) {      
				that.updating=false;      
				that.AJAX.abort();      
				that.AJAX=null;    
			}  
		}  
		this.update = function(passData,postMethod) {     
			if (that.updating) { 
				return false; 
			}    
			that.AJAX = null;                              
			if (window.XMLHttpRequest) {                    
				that.AJAX=new XMLHttpRequest();                  
			} else {                                        
				that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");    
			}                                                 
			if (that.AJAX==null) {                                   
				return false;                                   
			} else {      
				that.AJAX.onreadystatechange = function() {          
					if (that.AJAX.readyState==4) {                       
						that.updating=false;                          
						that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);                  
						that.AJAX=null;                                                 
					}                                                            
				}                                                              
				that.updating = new Date();                                    
				if (/post/i.test(postMethod)) {        
					var uri=urlCall+'?'+that.updating.getTime();    
					that.AJAX.open("POST", uri, true);        
					that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");        
					that.AJAX.send(passData);      
				} else {        
					var uri=urlCall+'?'+passData;//+'&timestamp='+(that.updating.getTime());        
					 document.getElementById('debug').innerHTML = uri;
					that.AJAX.open("GET", uri, true);                                     
					that.AJAX.send(null);                                               
				}                    
				return true;                                                 
			}                                                                             
		}  
		var urlCall = url;          
		this.callback = callbackFunction || function () { };
	}
	
var colorred = "red", colorblack = "black";

function number_validate(fld, str_event){
  var forecolor;
  var isbad = my_field_is_bad(fld, true)

  if (isbad){ forecolor = colorred;}
  else{forecolor = colorblack; }

  fld.style.color = forecolor;
  
}

function clear_comma(value){
  var myvalue = value + "";
  myvalue = myvalue.replace(",","");
  myvalue = myvalue.replace(",","");
  myvalue = myvalue.replace(",","");
  return(myvalue);
}

function my_field_is_bad(field,with_zero){
  var value =clear_comma(field.value);
  if (isNaN(value) )  return(true);
  if (!with_zero)
    if (parseFloat(value)<=0) return(true);
  else
    if (parseFloat(value)<0) return(true);
  return(false);
}

		
function my_event(obj, pevent){

  var fld = obj; 
  switch(pevent ){
 
    case 1: // onchange
      
      number_validate(fld, "onchange")
      if (fld.style.color == colorred){
        fld.focus();
        alert("Value is bad")
        fld.style.color = colorred;
        fld.focus();
      }else{
        fld.value = my_format(fld.value, true, 2, false);
      }
      break;
     
    case 2: // onkeyup
      number_validate(fld, "onkeyup")
      break;
     
    case 3: // onblur
      if (fld.value != ""){ 
        number_validate(fld, "onblur")
        if (fld.style.color == colorred){
          fld.focus();
          alert("Value is bad")
          fld.style.color = colorred;
          fld.focus();
        }else{
          if (obj.id == "mileage") {var dig =0; $width=false;}
          else {var dig = 2;  $width = true;}
          fld.value = my_format(fld.value, $width, dig, false);
        }
      }
      break;
     
  }
}

function my_format(value,with_decimals,decimals_count,dollar){
  var re = /\./;
//  if (value == "1313.0735"){    value =value;  }
  var myvalue = value + "";
  myvalue =clear_comma(myvalue);
//  var myvalue = "34.56";
  var point = myvalue.search(re);

  var decimals,integer,format_integer,my_str;
  if (point == -1 ){
    decimals="";
    integer = myvalue;
  }else{
    decimals=myvalue.substr(point+1);
    if (decimals == "0") decimals = "00";
    if (decimals == "000") decimals = "00";
    if (decimals == "0000") decimals = "00";
    if (decimals == "") decimals = "00";
//    if (myvalue == "122.825") alert (decimals+" / " +value + " / " +  decimals.substr(decimals.length-1,1) + "/ " + decimals.length)
    if (decimals.length>decimals_count){
      my_str ="1" +get_str("0",decimals.length-decimals_count)
      var my_int =decimals *1;
      my_int = my_int / (my_str*1)
      my_int =Math.round(my_int)+"";
      my_int = get_str("0",decimals_count-my_int.length) +my_int;
      decimals = my_int;
    }
    integer = myvalue.slice(0,point);;
  }
  format_integer = format_integer_number(integer);
  var ret;
  my_str ="";
  if (decimals.length == 0 && with_decimals){
    for (var nm = 1;nm<=decimals_count; nm++) my_str += "0";
    decimals = my_str;
  }
  if (decimals.length<decimals_count  && with_decimals){
    my_str =get_str("0",decimals_count-decimals.length)
//    for (var nm = decimals.length+1;nm<=decimals_count; nm++) my_str += "0";
    decimals = decimals + my_str;
  }

  if ((decimals.length == 0) || (decimals_count ==0)) ret = format_integer;
  else ret = format_integer + "." + decimals;
  if ((dollar !=undefined) && (ret != "")){
    if (dollar)  ret ="$" + ret;
  }
  return(ret);
}
function format_integer_number(integer){
  var my_integer = integer + "";
  if (my_integer=="null") return("");
  var len = my_integer.length;
  var ret ="";
  if (len <= 3 ) ret = my_integer;
  if (ret == "" && len <= 6 ){
    ret = my_integer.slice(0,len-3) + "," + my_integer.slice(len-3);
  }
  if (ret == "" && len <= 9 ){
    ret = my_integer.slice(0,len-6) + "," + my_integer.slice(len-6,len-3) + "," + my_integer.slice(len-3);
//    alert("format_integer_number:  " + len + " / " + ret + " / " + my_integer);
  }
  return(ret);
}

function get_str(simb,count){
  var nm,str="";
  for (nm = 1;nm<=count; nm++) str +=simb;
  return(str);
}
function myFormat(value) {
    return my_format(value, true, 2, false);
}

function round2Digit(value) {
    
    //return my_format(value, true, 2, false);
	return Math.round(value * 100) / 100;
}

function getInputValue(id) {
	var value = document.getElementById(id).value;
	value = value.replace(/,/g, "");
	return parseFloat(value);
}

function getInputValueHTML(id) {
	var value = document.getElementById(id).innerHTML;
	value = value.replace(/,/g, "");
	return parseFloat(value);
}

function getElement(eId) {
	return document.getElementById(eId);	
}

/****/

var _PAL_CAL_ = {
	
	opt: null,
	amount: null,
	data:null,
	
	update: function () {
		this.getInput();
		var vArray = this.calValue();
		this.displayValue(vArray);
		this.displayTitleDescription();
	},
	
	getInput: function() {
		this.amount = getInputValue('amountTF');
		this.opt = getInputValue('leastTypeSel');
		this.data = PAY_DATA.data;
	},
	
	calValue: function() {
		var term;
		var rates;
		var from;
		var to;
		var amt = this.amount;
		var resultArray = [];
		var myData = this.data[this.opt];
		
		if (amt < 1500) {
			resultArray = [  [12, -1, -1], 
							 [24, -1, -1],
							 [36, -1, -1],
							 [48, -1, -1],
							 [60, -1, -1],
							 [72, -1, -1],
							 [84, -1, -1]  ];
		} else {
			for (var i=0; i<myData.rates.length; i++) {
				rates = myData.rates[i];	
				var tObj = null;
				if (rates.value_low <= amt && amt < rates.value_up) {
					for (var j=0; j<rates.terms.length; j++) {
						term = rates.terms[j];
						from = (term.from * amt) / 1000
						to = (term.to * amt) / 1000
						resultArray.push([term.months, from, to]);	
					} // end for
				}
			} // end for 
		}
		
		return resultArray;
	},
	
	displayValue: function (input)  {
		var term = null;
		var from = null;
		var to = null;

		for (var i=0; i<input.length; i++) {
			
			term = input[i][0];
			from = input[i][1];
			to = input[i][2];
			if (term == 12) {
				// do nothing
			} else if (term == 72 || term == 84) {
				
				if (from <= 0 || to <= 0) {
					
					getElement('col_'+term).style.display="none";
				} else {
					getElement('col_'+term).style.display="";
					getElement('from_'+term).innerHTML = "$" + myFormat(round2Digit(from));
					getElement('to_'+term).innerHTML = "$" + myFormat(round2Digit(to));
				}
			} else {
				if (from <= 0){
					getElement('from_'+term).innerHTML = "NA";
				} else {
					getElement('from_'+term).innerHTML = "$" + myFormat(round2Digit(from));
				}
				
				if (to <=0) {
					getElement('to_'+term).innerHTML = "NA";
				} else {
					getElement('to_'+term).innerHTML = "$" + myFormat(round2Digit(to));
				}
			}
		}
	}, 
	
	displayTitleDescription: function () {
		getElement('newTitle').innerHTML = this.data[this.opt].title;
		getElement('newAmount').innerHTML = myFormat(round2Digit(this.amount));
		getElement('newDesc').innerHTML = this.data[this.opt].description;
	},
	
	popupLeaseTypeExplain: function() {
		var theURL = 'leasetypeexplain.php';
		var features = 'menubar=yes, scrollbars=yes,width=500,height=400';
		window.open(theURL,'',features);
	}, 
	
	printPDF: function () {
		var features = 'menubar=yes, scrollbars=yes,width=670,height=400';
		var query = "newTitle=" + getElement('newTitle').innerHTML;
		query += "&newAmount=" + getElement('newAmount').innerHTML;
		query += "&newDesc=" + getElement('newDesc').innerHTML;
		
		query += "&from_24=" + getElement('from_24').innerHTML;
		query += "&to_24=" + getElement('to_24').innerHTML;
		query += "&from_36=" + getElement('from_36').innerHTML;
		query += "&to_36=" + getElement('to_36').innerHTML;
		query += "&from_48=" + getElement('from_48').innerHTML;
		query += "&to_48=" + getElement('to_48').innerHTML;
		query += "&from_60=" + getElement('from_60').innerHTML;
		query += "&to_60=" + getElement('to_60').innerHTML;
		query += "&from_72=" + getElement('from_72').innerHTML;
		query += "&to_72=" + getElement('to_72').innerHTML;
		query += "&from_84=" + getElement('from_84').innerHTML;
		query += "&to_84=" + getElement('to_84').innerHTML;
		query += "&col_72=" + getElement('col_72').style.display;
		query += "&col_84=" + getElement('col_84').style.display;
		
		var theURL = "payment_print_pdf.php?" + encodeURI(query);
		window.open(theURL,'',features);
	}, 
	
	printHTML: function () {
		var features = 'menubar=yes, scrollbars=yes,width=670,height=400';
		var query = "newTitle=" + getElement('newTitle').innerHTML;
		query += "&newAmount=" + getElement('newAmount').innerHTML;
		query += "&newDesc=" + getElement('newDesc').innerHTML;
		
		query += "&from_24=" + getElement('from_24').innerHTML;
		query += "&to_24=" + getElement('to_24').innerHTML;
		query += "&from_36=" + getElement('from_36').innerHTML;
		query += "&to_36=" + getElement('to_36').innerHTML;
		query += "&from_48=" + getElement('from_48').innerHTML;
		query += "&to_48=" + getElement('to_48').innerHTML;
		query += "&from_60=" + getElement('from_60').innerHTML;
		query += "&to_60=" + getElement('to_60').innerHTML;
		query += "&from_72=" + getElement('from_72').innerHTML;
		query += "&to_72=" + getElement('to_72').innerHTML;
		query += "&from_84=" + getElement('from_84').innerHTML;
		query += "&to_84=" + getElement('to_84').innerHTML;
		query += "&col_72=" + getElement('col_72').style.display;
		query += "&col_84=" + getElement('col_84').style.display;
		
		var theURL = "payment_print.html?" + encodeURI(query);
		window.open(theURL,'',features);
	}, 
	
	showPrintPage: function() {
		getElement('newTitle').innerHTML = decodeURI(this.getURLParameter(window.location.href, "newTitle"));
		getElement('newAmount').innerHTML = decodeURI(this.getURLParameter(window.location.href, "newAmount"));
		getElement('newDesc').innerHTML = decodeURI(this.getURLParameter(window.location.href, "newDesc"));
		
		getElement('from_24').innerHTML = decodeURI(this.getURLParameter(window.location.href, "from_24"));
		getElement('to_24').innerHTML = decodeURI(this.getURLParameter(window.location.href, "to_24"));
		getElement('from_36').innerHTML = decodeURI(this.getURLParameter(window.location.href, "from_36"));
		getElement('to_36').innerHTML = decodeURI(this.getURLParameter(window.location.href, "to_36"));
		getElement('from_48').innerHTML = decodeURI(this.getURLParameter(window.location.href, "from_48"));
		getElement('to_48').innerHTML = decodeURI(this.getURLParameter(window.location.href, "to_48"));
		getElement('from_60').innerHTML = decodeURI(this.getURLParameter(window.location.href, "from_60"));
		getElement('to_60').innerHTML = decodeURI(this.getURLParameter(window.location.href, "to_60"));
		getElement('from_72').innerHTML = decodeURI(this.getURLParameter(window.location.href, "from_72"));
		getElement('to_72').innerHTML = decodeURI(this.getURLParameter(window.location.href, "to_72"));
		getElement('from_84').innerHTML = decodeURI(this.getURLParameter(window.location.href, "from_84"));
		getElement('to_84').innerHTML = decodeURI(this.getURLParameter(window.location.href, "to_84"));
		getElement('col_72').style.display = decodeURI(this.getURLParameter(window.location.href, "col_72"));
		getElement('col_84').style.display = decodeURI(this.getURLParameter(window.location.href, "col_84"));
	},
	
	getURLParameter: function(source,  name ){  
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
		var regexS = "[\\?&]"+name+"=([^&#]*)";  
		var regex = new RegExp( regexS );  
		var results = regex.exec( source );  
		if( results == null )    return "";  
		else    return results[1];
	}
}






