// AJAX

Function.prototype.bind = function(obj, args_pre)  {
	var fn = this;
	if(args_pre)return function() {return fn.apply(obj, args_pre)}
	else		return function() {return fn.apply(obj, arguments)}
}

function createRequestObject()
{
    if (window.XMLHttpRequest) {
        try {
            return new XMLHttpRequest();
        } catch (e){}
    } else if (window.ActiveXObject) {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e){
          try {
              return new ActiveXObject('Microsoft.XMLHTTP');
          } catch (e){}
        }
    }
    return null;
}

function request_warranty_price() {
	ax.dom.$('calc_phase2').addClass('progress');
	
	price = document.getElementById('calc_price').value;
	catname = document.getElementById('calc_catname').value;
	make = document.getElementById('calc_make').value;
	
	var XMLHttp = createRequestObject();
	XMLHttp.onreadystatechange = reply_warranty_price.bind(XMLHttp);
	url = "/calc.php?warranty_price&price=" + price + "&catname=" + catname + "&make=" + make;
	XMLHttp.open("GET", url, true);
	XMLHttp.send("");
}

function reply_warranty_price() {
	try {
	    if (this.readyState == 4 && this.status == 200) {
			eval('reply=' + this.responseText);
			ax.dom.$('calc_phase2').removeClass('progress');
			document.getElementById('price_installation').innerHTML = reply['installation'];
			document.getElementById('price_catname').innerHTML = document.getElementById('calc_catname').value;
			document.getElementById('price_make').innerHTML = document.getElementById('calc_make').value;
			document.getElementById('price_price').innerHTML = document.getElementById('calc_price').value || 0;
			document.getElementById('price_years1').innerHTML = reply[1];
			document.getElementById('price_years2').innerHTML = reply[2];
			document.getElementById('price_years3').innerHTML = reply[3];
			show_years();
	    }
	} catch(e) {alert(e.message)}
}

function request_check_state() {
	ax.dom.$('check_phase2').addClass('progress');
	
	code = document.getElementById('check_code').value;
	
	var XMLHttp = createRequestObject();
	XMLHttp.onreadystatechange = reply_check_state.bind(XMLHttp);
	url = "/calc.php?check&code=" + code;
	XMLHttp.open("GET", url, true);
	XMLHttp.send("");
}

function reply_check_state() {
	try {
	    if (this.readyState == 4 && this.status == 200) {
			ax.dom.$('check_phase2').removeClass('progress');
//			eval('reply=' + this.responseText);
			document.getElementById('check_phase2').innerHTML = this.responseText;
			check();
	    }
	} catch(e) {alert(e.message)}
}


// FX

transition_speed = 1000
flare_enable = true;

function flare() {
	if(!flare_enable) return;
	
	setTimeout(function(){ax.dom.$('calc_button1').addClass('flare')}, transition_speed);
	setTimeout(function(){ax.dom.$('calc_button1').removeClass('flare')}, transition_speed+2*transition_speed/20);
	setTimeout(function(){ax.dom.$('calc_button2').addClass('flare')}, transition_speed+2*transition_speed/20);
	setTimeout(function(){ax.dom.$('calc_button2').removeClass('flare')}, transition_speed+4*transition_speed/20);
	setTimeout(function(){ax.dom.$('calc_button3').addClass('flare')}, transition_speed+4*transition_speed/20);
	setTimeout(function(){ax.dom.$('calc_button3').removeClass('flare')}, transition_speed+6*transition_speed/20);

	setTimeout(flare, transition_speed*2);
}

function show_years() {
 	ph2 = ax.dom.$('calc_phase2');
 	
	installation = ax.dom.$('calc_installation').checked
	warranty = ax.dom.$('calc_warranty').checked

 	ph2.removeClass('on_warranty');
 	ph2.removeClass('on_installation');
 	if (installation) ph2.addClass('on_installation');
 	if (warranty) ph2.addClass('on_warranty');
 	
 	if (installation && !warranty)
 		calc_order(0);
 	
	if (ph2.hasClass('hidden')) {
		document.location.hash = 'calc';
		ph2.removeClass('hidden');
		ax.smooth({
			obj: ph2.style,
			param: 'top',
			add:'px',
			from: -ph2.clientHeight + (warranty?60:0),
			to: 0,
			fora: transition_speed,
			transition: sine_transition 
		})
		ax.smooth({
			obj: ph2.style,
			param: 'top',
			add:'px',
			from: -ph2.clientHeight + (warranty?60:0),
			to: 0,
			fora: transition_speed,
			transition: sine_transition 
		})
	}

	flare();
}

function calc_order(years) {
 	ph3 = ax.dom.$('calc_phase3');
 	
 	// set years
 	t = ax.dom.$('price_term_select_table')
 	t.removeClass('years1')
 	t.removeClass('years2')
 	t.removeClass('years3')
 	t.addClass('years'+years)
 	ax.dom.$('price_years').value = years

	// show phase 3 form
	if (ph3.hasClass('hidden')) {
		document.location.hash = 'calc';
		ph3.removeClass('hidden');
		ax.smooth({
			obj: ph3.style,
			param: 'top',
			add:'px',
			from: -ph3.clientHeight+60,
			to: 0,
			fora: transition_speed,
			transition: sine_transition 
		})
	}
	flare_enable = false;
}

 function check() {
 	ph = ax.dom.$('check_phase2');
 	
	if (ph.hasClass('hidden')) {
		ph.removeClass('hidden');
		ax.smooth({
			obj: ph.style,
			param: 'top',
			add:'px',
			from: -ph.clientHeight+60,
			to: 0,
			fora: transition_speed,
			transition: sine_transition 
		})
	}
}
 
 function sine_transition(x) {
	return (Math.PI / 2) * Math.sin(x * Math.PI)
}
 
 
