﻿function mouseoverStar(sender, id) {
	sender.style.backgroundImage = "url(css/image/" + id.toString() + ")";
}

function mouseoverStar2(sender, id) {
	sender.style.backgroundImage = "url(../css/image/" + id.toString() + ")";
}

function buttonOver(sender) {
	sender.className = 'buttonOver';
}

function buttonOut(sender) {
	sender.className = 'button';
}

function buttonRed(sender) {
	sender.className = 'buttonRed';
}

function goHotel() {
	document.location = 'hotel/hotel-table.php';
}

function goMetro() {
	document.location = 'hotel/hotel-ring.php';
}

function goService() {
	document.location = 'hotel/hotel-service.php';
}

function goApartment() {
	document.location = 'hotel/apartment.php';
}

function sendOrder() {
	if (checkNullable()) {
		hideWarning();
		pxDOM.byId('go').className = 'hide';
		sendDemand();
	}
}

function checkNumbers(input) {
	if (!checkNumber(pxDOM.byId('period'))) return;
	if (!checkNumber(pxDOM.byId('amount'))) return;	
	if (!checkNumber(pxDOM.byId('min'))) return;		
	if (!checkNumber(pxDOM.byId('max'))) return;			
	return true;
}

function checkNullable() {
//	if (!checkInputValue('last_name')) return;
	if (!checkInputValue('first_name')) return;
//	if (!checkInputValue('email')) return;
	if (!checkInputValue('phone')) return;
	if (!checkInputValue('day')) return;
//	if (!checkInputValue('month')) return;
	if (!checkInputValue('period')) return;
	if (!checkInputValue('amount')) return;
	return true;
}

function getTextValue(id) {
	var input = pxDOM.byId(id);
	return input.value;
}

function checkInputValue(id) {
	var input = pxDOM.byId(id);
	var value = input.value;
	if (value == '') {
		showWarning();
		setWarningText('Заполните все поля помеченные звездочкой (*).');
		input.focus();
		return false;
	}
	return true;
}

var monthList = new Array('январь', 'февраль', 'март', 'апрель', 'май', 'июнь', 'июль', 'август', 'сентябрь', 'октябрь', 'ноябрь', 'декабрь');

function getOption(owner, month, index) {
	var name = monthList[month];
	month = parseInt(month) + 1;	
	var option = pxDOM.newElement('option', '', owner, name);
}

function hideWarning() {
	pxDOM.byId('warning').className = 'hide';
}

function showWarning() {
	pxDOM.byId('warning').className = '';
}

function showInfo() {
	pxDOM.byId('info').className = '';
}

function init() {}

function checkNumber(input) {
	if (isNaN(input.value)) {
		setWarningText('Значение [' + input.value + '] не является числом.');
		input.focus();
	}
}

function setWarningText(value) {
	pxDOM.byId('warningText').innerHTML = value;
	showWarning();
}

function setInfoText(value) {
	pxDOM.byId('infoText').innerHTML = value;
	showInfo();
}

function getValueItem(id) {
	var value = getTextValue(id);
	return "&" + id + "=" + value;
}

function readFormValue() {
	var month = pxDOM.byId('month');
	month = month.options[month.selectedIndex].innerHTML;
	var result = '';
	result = result + getValueItem('last_name');
	result = result + getValueItem('first_name');
	result = result + getValueItem('phone');
	result = result + getValueItem('email');
	result = result + getValueItem('day');
	result = result + getValueItem('period');
	result = result + getValueItem('amount');
	result = result + getValueItem('min');
	result = result + getValueItem('max');
	result = result + '&month=' + month;	
	return result;
}

function sendDemand() {
	document.Request = pxReq.newRequest(afterSend);
	if (!document.Request) return;
  	
	pxReq.post(document.Request, 'script/req.php');
	document.Request.send("id=14" + readFormValue());
}

function afterSend() {
	if (pxReq.state(document.Request) != 0) return;
	var result = document.Request.responseText;	
	if (result && isNaN(result)) {
		setWarningText(result);
		pxDOM.byId('go').className = '';
		return;
	}
	setInfoText('Ваша заявка ' + result + ' принята. Мы свяжемся с Вами в течение часа');
	pxDOM.byId('day').value = '';
	pxDOM.byId('period').value = '';
	pxDOM.byId('amount').value = '';
	pxDOM.byId('min').value = '';
	pxDOM.byId('max').value = '';	
//	pxDOM.byId('go').className = '';
}


function openCallForm() {
	var left = getLeftFormCall();
	pxDOM.byId("formCall").style.left = left + 'px';
	pxDOM.byId("formCall").className = '';
}

function getLeftFormCall() {
	if (window.innerWidth) {
		return Math.ceil(window.innerWidth / 2) - 270;
	}
	
	if (document.documentElement && document.documentElement.clientWidth) {	
		return Math.ceil(document.documentElement.clientWidth / 2) - 263;
	}
	
	if (document.body.clientWidth) {
		return Math.ceil(document.body.clientWidth / 2) - 263;
	}
	
	return 400;
}

function hideCallForm() {
	pxDOM.byId("formCall").className = 'hide';
}

function sendCallForm() {
	this.Request = pxReq.newRequest(this.afterSend);	
	document.editor = this;  

	if (!this.Request) return;
	pxReq.post(Request, '../script/req.php');
	this.Request.send("id=24&callPeople=" + pxDOM.byId('callPeople').value + "&callPhone=" + pxDOM.byId('callPhone').value + "&comment=" + pxDOM.byId('comment').value);	
};

function afterSend() {
	if (pxReq.state(document.editor.Request) != 0) return;
	var result = document.editor.Request.responseText;	
	if (result && result != "") {
		alert(result);
		return;
	}
	hideCallForm();
	alert("Ваша заявка отправлена.");	
};
