/**/
// JavaScript Document
var nav4 = window.Event ? true : false;

//Only Allow Numbers
function onlyNumber(evt){
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
var key = nav4 ? evt.which : evt.keyCode;
return (key <= 13 || (key >= 48 && key <= 57));
}


//Clear form
function clearcalculator(){
    document.getElementById('result_m').value = '$0.0';;
	document.getElementById('result_y1').value = '$0.0';	
	document.getElementById('result_y2').value = '$0.0';	
	document.getElementById('result_y3').value = '$0.0';	
	document.getElementById('space').value = '';  //Espacio Caja de Texto
	document.getElementById('payperiod').selectedIndex=0;//Perido de Pago	
	return false;
}


/* Determines if multiple, returns
what needs to be a multiple of 5 */
function rest_105(space){

    space = String(space);
    	
    if ( space.charAt(space.length-1) == '0' ||  space.charAt(space.length-1) == '5' ||  space.length == 1) {
        return 0;
     
     } else {
     
          if (space.charAt(space.length-1) < 5){
               
          
          	return (5 - space.charAt(space.length-1));
         } 	
          else
         {     
                return (10 - space.charAt(space.length-1));
                
         } 
     
     }
}


function calculate_with_period (r, payperiod){
	  
	  if (payperiod == 0)
	     divisor = 1;
	  else if (payperiod == 1)
	     divisor = 12;
	  else if (payperiod == 3)
	     divisor = 24;
	  else if (payperiod == 6)
	     divisor = 36;
	  
	result = ((r * divisor) - (r * payperiod))/divisor;	
	
     // result = Math.round(result*100)/100;
     result = result;

     return result;

}

/*
	Returns the price as required space
*/

function calculate(){

	var r = 0, space = 0;
	space_input = document.getElementById('space').value;  //Text Box Area
	payperiod = document.getElementById('payperiod').options[document.getElementById('payperiod').selectedIndex].value;//Payment Period

	if (space_input) {
		space = parseFloat(space_input);
	} 


	if (payperiod){
		payperiod = parseInt(payperiod);	
	}else{
		payperiod = 0;
	}
	
	
	if (space){
		//set number to a multiple of 5
		space = space + rest_105(space);
	}

	//Superior a 100
	if (space > 100) {
		//Calculo con 2$
		r = 100 * 2;
			
		// Superior a 100
		if (space > 100 && space >= 200 ){

			//Calculo con 1.75
			r = r + (100 * 1.75);

			// Superior a 200
			if (space > 200 && space >= 300 ){

				//calculo con 1.50
				r = r + (100 * 1.50);

				// Superior a 300
				if (space > 300 && space >= 400 ){

					//calulo con 1.25   
					r = r + (100 * 1.25);

					//Superior a 400
					if (space > 400){
						r = r + (space - 400) * 1;
					}
				}else{
					//calculate the rest  
					r = r  + ((space - 300) * 1.25);

				}

			}else{
				 //calculate the rest
				 r = r  + ((space - 200) * 1.50);
			}

		} else {
			//calculate the rest  
			r = r  + ((space - 100) * 1.75);
		}
	} else {

		/*if (space > 0 &&  space <= 0.5)
		  r = 7;*/

		/*if (space > 0 && space <= 2.5)
		  r = 15;*/

		if (space > 0 &&  space <= 5)
		  r = 25;

		if (space > 5 &&  space <= 10)
		  r = 35;
		  
		if (space > 10 &&  space <= 25)
		  r = 50;
		  
		  
		  cont = 25;
		  _stop = 0;
		  
		while (cont != 100 && _stop == 0){
		
		   cont = cont + 5;
		   
		   if (space > cont-5 && space <= cont ){
			 r = cont * 2;
			 _stop = 1;
		   }
		}  
	} 
	
	//alert("space = "+space+"\nr = "+r);
	
	div_m=document.getElementById('result_m');
	div_y1=document.getElementById('result_y1');
	div_y2=document.getElementById('result_y2');
	div_y3=document.getElementById('result_y3');

	
	rx = calculate_with_period (r, payperiod);
		  
		  
	//Calculated as total monthly cost for years and then
	/*	
		  rm = Math.round(rx*100)/100;
		  ry1 = Math.round(rx*12*100)/100;
		  ry2 = Math.round(rx*24*100)/100;
		  ry3 = Math.round(rx*36*100)/100;
		
	*/	
		//Discount Calculated
	/*	  

		  rm = calculate_with_period (r, payperiod);
		  ry1 = Math.round(calculate_with_period (r, 1)*12*100)/100;
		  ry2 = Math.round(calculate_with_period (r, 3)*24*100)/100;
		  ry3 = Math.round(calculate_with_period (r, 6)*36*100)/100;	
		  
		  
		  */  
		 //No decimals
	rm = Math.round(calculate_with_period (r, payperiod)*100)/100;
	ry1 =calculate_with_period (r, 1)*12;
	ry2 = calculate_with_period (r, 3)*24;
	ry3 = calculate_with_period (r, 6)*36;	
	
	//alert("rm = "+rm+"\nry1 = "+ry1+"\nry2 = "+ry2+"\nry3 = "+ry3);


	if (r) {
		div_m.value = '$'+rm;
		div_y1.value = '$'+ry1;
		div_y2.value = '$'+ry2;
		div_y3.value = '$'+ry3;
	}else{
		div_m.value = '$0.0';	
		div_m.value = '$0.0';
		div_y1.value = '$0.0';
		div_y2.value = '$0.0';
		div_y3.value = '$0.0';		
	}
	
	if (space >= 1000) {
		div_m.value = 'Contact us for special pricing';
		div_y1.value = '';
		div_y2.value = '';
		div_y3.value = '';
	}
	
	return false;
	
}

