//local vars to store totals for updating cal

var _pc_min_charge = 225;
var _pc_total = 0;
var _jk_total = 0;
var _cc_total = 0;
var _wc_total = 0;
var _pool_total = 0;
var _tree_bool = false;
var _lawn_bool = false;
var _pwash_bool = false;

//common stings
var callUS = 'Contact RealtyClean for Estimate';

// This function initialzes all the form elements to default values.
function InitForm(cart) {
   	//document.cart.submitit.enabled = false;	
	this._pc_total = this._pc_min_charge ;
		
}


function calculatePC(inputItem) {
  with (inputItem.form) {   
    var temp_total = this._pc_min_charge; //this sets up the fixed value

	var temp_area = 0;
    //setup the values
    var pccost=new Array();
		pccost[0]=0; //the base rate is the min order to the 1000sq
		pccost[1]=0.15;
		pccost[2]=0.14;
		pccost[3]=0.13;
		pccost[4]=0.12;
		pccost[5]=0.11;
    
    if (inputItem.type == "text") {   // Process radio buttons.     
			temp_area = eval(inputItem.value);
    }
    
    var i = 0;
    
    while (eval(temp_area) > eval(999)) {
		temp_area = eval(temp_area) - eval(1000);
		if (i > 5) {
			temp_total = eval(temp_total + ( pccost[5] * 1000));
		} else {
			temp_total = eval(temp_total + ( pccost[i] * 1000));
		}
		i++;
	}
	
	if ((i > 0) && (eval(temp_area) > 0) && (eval(temp_area) < 1000) ) {
		if (i > 5) {
			temp_total = eval(temp_total + ( pccost[5] * eval(temp_area)));
		} else {
			temp_total = eval(temp_total + ( pccost[i] * eval(temp_area)));
		}
	}
    

	pc_amount.value = eval(temp_total);
	document.getElementById("pc_total_text").innerHTML = (formatCurrency(eval(pc_amount.value))) ;
	this._pc_total = temp_total;
  }
  	UpdateTotal();	
}

function calculateJK(inputItem) {
  with (inputItem.form) {   
    
    var temp_total = 0;
    
    //setup the values
    var jkcost=new Array();
		jkcost[0]= 117.00; //min rate
		jkcost[1]= 231.00;//1/4 load
		jkcost[2]= 339.00;//1/2 load
		jkcost[3]= 439.00;//3/4 load
		jkcost[4]= 527.00;//Full load

	var jkpic=new Array();
		jkpic[0]= 'images/minload.jpg'; //min rate
		jkpic[1]= 'images/1-4load.jpg';//1/4 load
		jkpic[2]= 'images/1-2load.jpg';//1/2 load
		jkpic[3]= 'images/3-4load.jpg';//3/4 load
		jkpic[4]= 'images/fullload.jpg';//Full load
        
    var i = -1;
    
    if (inputItem.type == "radio") {   // Process radio buttons.     
			i = eval(inputItem.value);
    }

	if (i >= 0) {
		temp_total = eval(jkcost[i]);
		ImageOnly.src = jkpic[i];
	}

    jk_set.value = eval(1);
	this._jk_total = eval(temp_total);
	jk_amount.value = eval(temp_total);

    // Return total value.
	document.getElementById("jk_total_text").innerHTML = (formatCurrency(eval(jk_amount.value))) ;
  }
  	UpdateTotal();	
}

function calculateWC(inputItem) {
  with (inputItem.form) {   
    
    var temp_total = 0;
    
    //setup the values
    var wccost=new Array();
		wccost[0]= 9.00; //1-10 windows
		wccost[1]= 5.00;//11+ winodws
		
    var i = -1;
    
    if (inputItem.type == "text") {   // Process radio buttons.     
			i = eval(inputItem.value);
    }

	if ((i >= 0) && (i <= 10))  {
		temp_total = (eval(i) * eval(wccost[0])); //number of windows less then or = 10 X the rate
	} 
	if (i >= 11) {
		temp_total = (10 * eval(wccost[0]))+(eval(i-10) * eval(wccost[1])); //number of windows less then or = 10 X the rate
	}
    
	wc_amount.value = eval(temp_total);
	document.getElementById("wc_total_text").innerHTML = (formatCurrency(eval(wc_amount.value))) ;
	this._wc_total = eval(temp_total);
	}
  	UpdateTotal();
}

function calculateCC(inputItem) {
  with (inputItem.form) {   
    var temp_total = 0;
    var temp_total1 = 0;
    var temp_total2 = 0;
    //setup the values
    var cccost=new Array();
		cccost[0]= 35.00; //Room Cost
	
	var o_room = 0;	
    var r_room = 0;

		if (cc_num1.value.length != 0 ) {
			r_room = eval(cc_num1.value);
			temp_total1 = eval(r_room) * eval(cccost[0]);		
		} 
			
		temp_total = eval(temp_total1);	
		if (cc_num2.value.length != 0 ) {
			o_room = eval(cc_num2.value);
			temp_total2 = (eval(o_room) * eval(2)) * eval(cccost[0]);		
		} 
		
		temp_total = eval(eval(temp_total1) + eval(temp_total2)); 	 
	cc_amount.value = eval(temp_total);
	this._cc_total = eval(temp_total);

    // Return total value.
	document.getElementById("cc_total_text").innerHTML = (formatCurrency(eval(cc_amount.value))) ;
  }
  UpdateTotal();
}

function calculatePO(inputItem) {
  with (inputItem.form) {   
    var temp_total = 0;
    //setup the values
    var poolcost=new Array();
		poolcost[0]= 195.00; //pool clean cost
	 
	 	if (document.cart.pool.checked) {
			pool_amount.value = eval(poolcost[0]);		
			document.getElementById("pool_total_text").innerHTML = (formatCurrency(eval(pool_amount.value))) ;
			this._pool_total = eval(poolcost[0]);
			pool_set.value = eval(1);
		} else {
			pool_amount.value = ''; 
			document.getElementById("pool_total_text").innerHTML = 'Not Selected' ;
			pool_set.value = eval(0);
			this._pool_total = eval(0);
		}
  }
  	UpdateTotal();
}

function calculateTree(inputItem) {
  with (inputItem.form) {   
    var temp_total = 0;
    //setup the values
 	if (document.cart.tree.checked) {
		document.getElementById("tree_total_text").innerHTML = this.callUS;
		pool_set.value = eval(1);
		this._tree_bool = true;
	} else {
		document.getElementById("tree_total_text").innerHTML = 'Not Selected' ;
		pool_set.value = eval(0);
		this._tree_bool = false;
	}
  }
  UpdateTotal();
}

function calculateLawn(inputItem) {
  with (inputItem.form) {   
    var temp_total = 0;
    //setup the values
 	if (document.cart.lawn.checked) {
		document.getElementById("lawn_total_text").innerHTML = this.callUS;
		pwash_set.value = eval(1);
		this._lawn_bool = true;
	} else {
		document.getElementById("lawn_total_text").innerHTML = 'Not Selected' ;
		pwash_set.value = eval(0);
		this._lawn_bool = false;
	}
  }
  UpdateTotal();
}

function calculatePWash(inputItem) {
  with (inputItem.form) {   
    var temp_total = 0;
    //setup the values
 	if (document.cart.pwash.checked) {
		document.getElementById("pwash_total_text").innerHTML = this.callUS;
		pwash_set.value = eval(1);
		this._pwash_bool = true;
	} else {
		document.getElementById("pwash_total_text").innerHTML = 'Not Selected' ;
		pwash_set.value = eval(0);
		this._pwash_bool = false;
	}
  }
  UpdateTotal();
}


// Format a value as currency.
function formatCurrency(num) {
	var cents;
	var sign;
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
     num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
      cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function formvalidation(cart)
{
	
	if (document.cart.Sauce[0].checked || document.cart.Sauce[1].checked || document.cart.Sauce[2].checked|| document.cart.Sauce[3].checked|| document.cart.Sauce[4].checked)
	{
		cart.submit();
	}
	else
	{
		alert('Please make a selection.');
	}
}

function confirmClear (cart) {
	var temp = confirm('Are you sure you want to clear the form?');
	if (temp) {
		with (cart.form) {
			//property clear code
			pc_amount.value = "225";		
			document.getElementById("pc_total_text").innerHTML = '$225.00' ;

			//junk removal clear code
			jk_amount.value = "";
			jk_set.value = '0';
			document.getElementById("jk_total_text").innerHTML = 'Not Selected' ;

			//carpet clear code
			cc_amount.value = "";
			document.getElementById("cc_total_text").innerHTML = 'Not Selected' ;

			//windows clear code
			wc_amount.value = "";		
			document.getElementById("wc_total_text").innerHTML = 'Not Selected' ;

			//pool clear code
			pool_amount.value = "";
			pool_set.value = "0";
			document.getElementById("pool_total_text").innerHTML = 'Not Selected' ;

			//tree care clear code
			tree_set.value = "0";
			document.getElementById("tree_total_text").innerHTML = 'Not Selected' ;

			//lawn care clear code
			lawn_set.value = "0";
			document.getElementById("lawn_total_text").innerHTML = 'Not Selected' ;

			//lawn care clear code
			pwash_set.value = "0";
			document.getElementById("pwash_total_text").innerHTML = 'Not Selected' ;

						
			//grand total
			document.getElementById("total_text").innerHTML = '225.00' ;
			document.getElementById("total_amount").value = '225.00' ;

			this._pc_total = 0;
			this._jk_total = 0;
			this._cc_total = 0;
			this._wc_total = 0;
			this._pool_total = 0;

			this._tree_bool = false;
			this._lawn_bool = false;
			this._pwash_bool = false;

			//UpdateTotal()
		}
	}

	return temp;
}

function UpdateTotal() {
	var gTotal = 0;
	gTotal = eval(this._pc_total);
	gTotal = eval(gTotal +	this._jk_total); 
	gTotal = eval(gTotal +	this._cc_total); 		
	gTotal = eval(gTotal +	this._wc_total); 
	gTotal = eval(gTotal +	this._pool_total); 
	if ((this._tree_bool) || (this._lawn_bool) || (this._pwash_bool)) {
		document.getElementById("total_text").innerHTML = callUS;			
	} else {
		document.getElementById("total_text").innerHTML = (formatCurrency(eval(gTotal))) ;
	}
}

function clearRB(buttonGroup)
{
   for (i=0; i < buttonGroup.length; i++) {
   
       if (buttonGroup[i].checked == true) { // if a button in group is checked,
             buttonGroup[i].checked = false;   // uncheck it
      }
      
   }
	this._jk_total = 0;
	document.getElementById("jk_amount").value = '0';
	document.getElementById("jk_set").value = '0';
	document.getElementById("jk_total_text").innerHTML = 'Not Selected' ;
	UpdateTotal();
}