/**
 * @author Ryo
 */
window.onload = startup;
var openedForm;
function startup(){
	init();
	document.getElementById('test').onclick = getWindow;
	document.getElementById('calculate').onclick = getData;
	var tab = document.getElementById('carTable');
	if(tab){
		openedForm = document.getElementById('form4');	
		var center = document.getElementById('centerText');
		var inputs = center.getElementsByTagName('input');	
		for(i=0;i<inputs.length;i++){
			inputs[i].onclick = getOrderWindow;
		}
	}
	else if(document.getElementById('but9')){
		document.getElementById('but9').onclick = getWindowDop1;
		document.getElementById('but10').onclick = getWindowDop2;
		document.getElementById('but11').onclick = getWindowDop3;
		document.getElementById('but12').onclick = getWindowDop4;
		document.getElementById('but13').onclick = getWindowDop5;
		document.getElementById('but14').onclick = getWindowDop6;
	}
	else if(document.getElementById('orderButton')){
		openedForm = document.getElementById('form4');	
		document.getElementById('orderButton').onclick = getOrderWindow2;
	}	
}
	
function identificateId(e){
	try {
		obj = e.target; 
	} 
	catch (e) {
		obj = window.event.srcElement;
	}
	return(obj);
}
function getPosition(e){
	identificateId(e);	
	var data = {nameX:getName(obj),topY:getAbsolutePosition(obj)};
	return data;	
}
function getPosition2(e){
	identificateId(e);	
	var data = {nameX:getName2(obj),topY:getAbsolutePosition(obj)};
	return data;	
}
function getAbsolutePosition(el){	
	var r = el.offsetTop;	
	if (el.offsetParent) {		
		var tmp = getAbsolutePosition(el.offsetParent);
		r+= tmp;		
	}		
	return r;	
}
function getName(obj){
	var parentTd = obj.parentNode.parentNode;
	var nameX = parentTd.getElementsByTagName('B')[0].innerHTML;
	return nameX;
}
function getName2(obj){
	var parentTd = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
	var nameX = parentTd.getElementsByTagName('H1')[0].innerHTML;
	return nameX;
}
function clearForm(){
	if(openedForm){
		openedForm.style.display = 'none';
	}
}
function getData2(){	
	var needs = openedForm.getElementsByTagName('input');
	var ordName = needs[0].value;
	var ordTel = needs[1].value;
	if(ordName && ordTel){
		openedForm.style.visibility = 'hidden';
	}
	else{
		alert("Заполните обязательные поля");
	}
}
function getWindow(){
	if (!document.getElementById('win')) {
	var win = document.createElement('div');
	win.style.position = 'absolute';
	win.style.top = 350 + 'px';
	win.style.left = 650 + 'px';
	win.style.background = '#cccccc';
	win.style.borderTop = '2px solid #e5e5e5';
	win.style.borderLeft = '2px solid #e5e5e5';
	win.style.borderBottom = '2px solid #656565';
	win.style.borderRight = '2px solid #656565';
	win.id = 'win';
	win.className = 'formOrder';
	
	document.body.appendChild(win);	
	win.innerHTML = document.getElementById('formOrder').innerHTML;
	
	document.body.removeChild(document.getElementById('formOrder'));
	document.getElementById('closer').onclick = delWin;
	document.getElementById('close').onclick = getData3;
	
	}
	else{
	document.getElementById('win').style.visibility = 'visible';
	}
}
function delWin(){
	document.getElementById('win').style.visibility = 'hidden';
	document.getElementById('total').style.display = 'none';
}
function getOrderWindow(e){
	clearForm();
	var data = getPosition(e);
	var win = document.createElement('div');
	win.style.width = 220 + 'px';
	win.style.position = 'absolute';
	win.style.top = data.topY - 257 + 'px';
	win.style.left = 450 + 'px';
	win.style.background = '#cccccc';
	win.style.borderTop = '2px solid #e5e5e5';
	win.style.borderLeft = '2px solid #e5e5e5';
	win.style.borderBottom = '2px solid #656565';
	win.style.borderRight = '2px solid #656565';
	win.id = 'win' + data.nameX;
	win.className = 'formOrder1';	
	document.body.appendChild(win);		
	win.innerHTML = openedForm.innerHTML;		
	document.body.removeChild(openedForm);
	document.getElementById('formTitle').innerHTML = "заказ " + data.nameX;
	win.getElementsByTagName('input')[4].onclick = delWin1;
	document.getElementById('hidden').value = data.nameX;
	openedForm = document.getElementById('win'+data.nameX);
	win.getElementsByTagName('input')[3].onclick = getData2;	
}
function getOrderWindow2(e){
	var data = getPosition2(e);
	var win = document.createElement('div');
	win.style.width = 220 + 'px';
	win.style.position = 'absolute';
	win.style.top = data.topY - 110 + 'px';
	win.style.left = 400 + 'px';
	win.style.background = '#cccccc';
	win.style.borderTop = '2px solid #e5e5e5';
	win.style.borderLeft = '2px solid #e5e5e5';
	win.style.borderBottom = '2px solid #656565';
	win.style.borderRight = '2px solid #656565';
	win.id = 'win' + data.nameX;
	win.className = 'formOrder1';	
	document.body.appendChild(win);		
	win.innerHTML = openedForm.innerHTML;		
	document.body.removeChild(openedForm);
	document.getElementById('formTitle').innerHTML = "заказ " + data.nameX;
	win.getElementsByTagName('input')[4].onclick = delWin1;
	openedForm = document.getElementById('win'+data.nameX);
	win.getElementsByTagName('input')[3].onclick = getData2;	
}
function delWin1(){
	openedForm.style.visibility = 'hidden';
}
function getData(){	
		var aCar = document.getElementById('orderCar').selectedIndex;
		var aTime = DoIt("time");			
	calc2(aCar, aTime);	
}
function getData3(){
		var aName = document.getElementById('orderName').value;
		var aTel = document.getElementById('orderTel').value;
	if(aName && aTel){		
		var aEmail = document.getElementById('orderEmail').value;
		var aCar = document.getElementById('orderCar').selectedIndex;
		var aOption = document.getElementById('orderOption').value;
		var aPlace = document.getElementById('orderPlace').value;
		var aTime = DoIt("time");
	}
	else{
		alert("Вы не заполнили обязательные поля!");
	}	
}

function DoIt(RXGroup){
     var radioRXGroup = document.getElementsByName(RXGroup);
	  for (var RowR = 0; RowR < radioRXGroup.length; RowR++)
    {
       if(radioRXGroup[RowR].checked)
       {
          return radioRXGroup[RowR].value;
          break;
       }
    }		
     
}

function getCalcInfo(){
		var aCar = document.getElementById('calcCar').selectedIndex;
		if((0 < nDays)&&(nDays <= 2)){
			var Time = 0;
		}
		else if((2 < nDays)&&(nDays <= 6)){
			var Time = 1;
		}	
		else if((6 < nDays)&&(nDays <= 30)){
			var Time = 2;
		}
		else{
			var Time = 3;
		}		
	calc(aCar, Time);
}

var nDays;
function checkNum(){
	nDays = document.getElementById('days').value;
	nDays = nDays.replace('.','');
	nDays = nDays.replace('-','');
	var w = isFinite(nDays);
	if(!w){
		alert("пожалуйста, введите правильно количество дней");
	}
	else{
		getCalcInfo();
	}
}
function calc2(Car, Time){
	var c1 = [2300,2000,1800,1600,10000];
	var c2 = [2200,1900,1700,1500,10000];
	var c3 = [1800,1700,1600,1300,10000];
	var c4 = [1400,1300,1200,1000,6000];
	var c5 = [1400,1300,1200,1000,6000];
	var c6 = [1400,1300,1200,1000,6000];
	var c7 = [2200,2100,1800,1600,10000];
	var costs = [c1,c2,c3,c4,c5,c6,c7];
	document.getElementById('total2').style.visibility = 'visible';
	document.getElementById('total2').style.font = '11px Tahoma';
	document.getElementById('total2').innerHTML = 
	"Стоимость аренды на один день = <br/>" + costs[Car][Time-1] + "р. + " + costs[Car][4] + "р.(залог)";
}
function calc(Car, Time){
	var c1 = [2300,2000,1800,1600,10000];
	var c2 = [2200,1900,1700,1500,10000];
	var c3 = [1800,1700,1600,1300,10000];
	var c4 = [1400,1300,1200,1000,6000];
	var c5 = [1400,1300,1200,1000,6000];
	var c6 = [1400,1300,1200,1000,6000];
	var c7 = [2200,2100,1800,1600,10000];
	var costs = [c1,c2,c3,c4,c5,c6,c7];
	document.getElementById('total').style.display = 'block';
	document.getElementById('total').innerHTML = 
	"Ориентировочная стоимость аренды авто = "
	+costs[Car][Time]*nDays+"р.<br/>" + costs[Car][4] + "р. - залог</br></br>Для получения скидок: (812)917 51 51";
}
