function frmSubscribe_onFocus(o)
{
    if (o.value == o.title)
    {
        o.style.color = '#000000';
        o.value = '';
    }
}

function frmSubscribe_onBlur(o)
{
    if ('' == o.value)
    {
        o.style.color = '#999999';
        o.value = o.title;
    }
}

function frmSubscribe_onSubmit(f)
{
    var sFocus = '';
    var sError = '';

    if (typeof(f.persone) != 'undefined' &&
        ('' == f.persone.value || f.persone.value == f.persone.title)
    )
    {
        sError = f.persone.title;
        sFocus = 'persone';
    }
    else if ('' == f.email.value || f.email.value == f.email.title)
    {
        sError = f.email.title;
        sFocus = 'email';
    }
    else if (false == validateEmail(f.email.value))
    {
        sError = 'Введен неверный Email (шаблон: имя@домен.зона)';
        sFocus = 'email';
    }
    else if (typeof(f.format) != 'undefined' && '' == f.format.value)
    {
        sError = 'Выберите формат рассылки';
        sFocus = 'format';
    }

    if ('' != sError)
    {
        alert(sError);
        f.elements[sFocus].focus();
        return false;
    }

    return true;
}

function frmLogin_onSubmit(f)
{
    var sFocus = '';
    var sError = '';

    if (typeof(f.login) != 'undefined' &&
        ('' == f.login.value || f.login.value == f.login.title)
    )
    {
        sError = f.login.title;
        sFocus = 'login';
    }
    else if ('' == f.pswd.value || f.pswd.value == f.pswd.title)
    {
        sError = f.pswd.title;
        sFocus = 'pswd';
    }

    if ('' != sError)
    {
        alert(sError);
        f.elements[sFocus].focus();
        return false;
    }

    return true;
}

function frmSearch_onSubmit(f)
{
    if ('' == f.q.value)
    {
        alert(f.q.title);
        f.q.focus();
        return false;
    }
    return true;
    
}

function mailus()
{
    self.location = 'mailto:editor@nakormim.spb.ru';
}

function validateEmail(sEmail)
{
    if (null == sEmail.match(/^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$/))
        return false;
    else
        return true;
}

function onChangeCity(sel)
{
	location.href=sel.value;
}

function frmOpros_onSubmit(f)
{
    var sFocus = '';
    var sError = '';
    if (typeof(f.email) != 'undefined' &&
        ('' == f.email.value || f.email.value == f.email.title)
    )
    {
        sError = f.email.title;
        sFocus = 'email';
    }
    if (typeof(f.fio) != 'undefined' &&
        ('' == f.fio.value || f.fio.value == f.fio.title)
    )
    {
        sError = f.fio.title;
        sFocus = 'fio';
    }
    if ('' != sError)
    {
        alert(sError);
        f.elements[sFocus].focus();
        return false;
    }
    return true;
}

function frmOrder_onSubmit(f)
{
    var sFocus = '';
    var sError = '';
    if (typeof(f.address) != 'undefined' &&
        ('' == f.address.value || f.address.value == f.address.title)
    )
    {
        sError = f.address.title;
        sFocus = 'address';
    }
    if (typeof(f.phone) != 'undefined' &&
        ('' == f.phone.value || f.phone.value == f.phone.title)
    )
    {
        sError = f.phone.title;
        sFocus = 'phone';
    }
    if (typeof(f.fullName) != 'undefined' &&
        ('' == f.fullName.value || f.fullName.value == f.fullName.title)
    )
    {
        sError = f.fullName.title;
        sFocus = 'fullName';
    }
    if ('' != sError)
    {
        alert(sError);
        f.elements[sFocus].focus();
        return false;
    }
    return true;
}

var ky_price=new Array();

function addPrice(id,price) {
	ky_price[id]=price;
}

function recalc(id) {

	var c = document.getElementById("count"+id);
	var r = document.getElementById("costs"+id);
	var rl = document.getElementById("costs"+id+"_label");
	var s = document.getElementById("summa");

	if(c.value=='' || c.value=='NaN')
		c.value=0;

	var v=ky_price[id]*parseInt(c.value);
	if(v=='NaN')
		v=0;

	v=v.toString();

	if(v.lastIndexOf('.')>0) {
		v=v.substring(0,v.lastIndexOf('.')+3);
	}
	
	r.value=v;
	rl.innerHTML=v;

	var j=0;

	for(i in ky_price) {
		j+=parseFloat(document.getElementById("costs"+i).value);
	}

	j=j.toString();

	if(j.lastIndexOf('.')>0) {
		j=j.substring(0,j.lastIndexOf('.')+3);
	}

	s.innerHTML=j;
	document.getElementById('summ').value=j;

	//return true;

}

function plus(id) {
	var o=document.getElementById("count"+id);
	o.value=parseInt(o.value)+1;
	document.getElementById('tableRow_'+id).className='selectedRow';
	recalc(id);
}

function minus(id) {
	var o=document.getElementById("count"+id);
	var v=o.value;
	if(parseInt(o.value)>0) 
		o.value=parseInt(o.value)-1;
	if(parseInt(o.value)>0) 
		document.getElementById('tableRow_'+id).className='selectedRow';
	else 
		document.getElementById('tableRow_'+id).className='';
	recalc(id);
}

function keypress(o) {
	recalc(o.id.substring(5));
}

function onOrder() {
	if(parseFloat(document.getElementById('summ').value)<500) {
		alert("Минимальный заказ на сайте 500 рублей.\n\nЗаказ обедов на сумму меньше 500 рублей по телефонам:\n337-26-69, 291-12-20, 544-1-911");
		return false;
	}
	return true;
}

function mailto() {
	window.location.href='mai'+'lto:'+'mail'+'@kyl'+'inarov.'+'ru';
}

function bookMark() {
	var tempAgent = navigator.userAgent;
	if(tempAgent.indexOf('MSIE')>=0) {
		window.external.AddFavorite(location.href,document.title);
		return false;
	}
}

function winOpen(url, w, h) {
	var width,height;
	width=w+25;
	height=h+25;
	window.open(url, "", "width="+width+",height="+height+",status=no,toolbar=no,menubar=no,scrollbars=no");
}

function set_season(roomID, id, season, begin, end, price) {
	//alert('roomID:'+roomID+'; id:'+id+'; season:'+season+'; begin:'+begin+'; end:'+end+'; price:'+price);
	var b=begin.split('-');
	var begDate=new Date(b[0],b[1]-1,b[2]);
	var e=end.split('-');
	var endDate=new Date(e[0],e[1]-1,e[2]);
	if(seasons[roomID]==undefined)
		seasons[roomID]=new Array();
	if(seasons[roomID][id]==undefined)
		seasons[roomID][id]={'season': season, 'begin':begDate, 'end':endDate, 'price': price};

}

function calc(begin,end,persons) {
	if(begin>end) {
		alert('Дата начала больше даты конца');
		return;
	}
	//alert(begin+"\n"+end);
	var roomID=document.getElementById('roomID').value;
	//alert(roomID);
	var dates=new Array();
	var price=0;
	var end2=new Date();
	end2.setTime(end.getTime()-(60*60*24*1000));
	dates[0]=begin;
	if(begin.getTime()!=end.getTime()) {
		while(dates[dates.length-1]<end2) {
			var d=new Date();
			d.setTime(dates[dates.length-1].getTime()+(60*60*24*1000));
			//alert(d);
			dates[dates.length]=d;
		}
	} else
		dates = new Array();
	
	//alert(dates.length);
	var i=0;
	for(i in dates) {
		if (i<dates.length) {
			//alert(price);
			for(var s in seasons[roomID]) {
				//alert(seasons[roomID][s].begin);
				if(dates[i].getTime()>=seasons[roomID][s].begin.getTime() && dates[i].getTime()<=seasons[roomID][s].end.getTime()) {
					price+=parseFloat(seasons[roomID][s].price);
					break;
				}
				//alert('calc5');
			}
			//alert(price);
		}
	}
	
	//alert(price);
	return price*persons;
}

function recalc_form() {
	var beg=new Date(document.getElementById('zayezd_year').value,document.getElementById('zayezd_month').value-1,document.getElementById('zayezd_day').value);
	var end=new Date(document.getElementById('otyezd_year').value,document.getElementById('otyezd_month').value-1,document.getElementById('otyezd_day').value);
	if(beg>end) {
		alert('Дата отъезда должна быть позже даты заезда.');
		return;
	}
	var selBox=document.getElementById('persons');
	var pers=selBox.options[selBox.selectedIndex].value;
	//alert(pers);
	var res = calc(beg,end,pers);
	//alert('asdf');
	//alert(res);
	res=res.toString();
	if(res.lastIndexOf('.')>0)
		res=res.substring(0,res.lastIndexOf('.')+3);
	var cc = document.getElementById('count');
	cc.value=res;
	var c = document.getElementById('counted');
	c.innerHTML=res;
	return true;
}

function validate_order(f) {
	var v=true;
	if(f.fio.value=='') {
		v=false;
		alert('Введите ваше Ф.И.О.');
	}
	if(f.email.value=='' || !validateEmail(f.email.value)) {
		v=false;
		alert('Введите корректный e-mail!');
	}
	if(f.phone.value=='') {
		v=false;
		alert('Введите корректный номер телефона!');
	}
	if(!f.condition.checked) {
		v=false;
		alert('Вы должны быть согласны с условиями!');
	}
	if(document.getElementById('visa_supp').checked) {
		//if(f.title.value=='') {
		//	v=false;
		//	alert('Введите корректный title!');
		//}
		if(f.passport_no.value=='') {
			v=false;
			alert('Введите номер паспорта!');
		}
		if(f.country.value=='') {
			v=false;
			alert('Введите корректный страна!');
		}
	}
	if(document.getElementById('transfer_supp').checked) {
		if(f.address.value=='') {
			v=false;
			alert('Введите корректный адрес!');
		}
	}
	var beg=new Date(document.getElementById('zayezd_year').value,document.getElementById('zayezd_month').value-1,document.getElementById('zayezd_day').value);
	var end=new Date(document.getElementById('otyezd_year').value,document.getElementById('otyezd_month').value-1,document.getElementById('otyezd_day').value);
	if(beg.getTime()>=end.getTime()) {
		alert('Дата отъезда должна быть позже даты заезда.');
		v=false;
	}
	return v;
}

function validate_exc(f) {
	var v = true;
	if(f.fio.value=='') {
		v=false;
		alert('Введите имя!');
	}
	if(f.email.value=='' || !validateEmail(f.email.value)) {
		v=false;
		alert('Введите корректный e-mail!');
	}
	if(f.phone.value=='') {
		v=false;
		alert('Введите корректный номер телефона!');
	}
	return v;
}

function ryabina(lH,lA,h,a) {
	if(document.getElementById(lH).checked) {
		//alert ('sad');
		document.getElementById(h).style.display = 'inline';
		document.getElementById(a).style.display = 'none';
		document.getElementById(h).disabled = false;
		document.getElementById(a).disabled = true;
	} else {
		document.getElementById(a).style.display = 'inline';
		document.getElementById(h).style.display = 'none';
		document.getElementById(a).disabled = false;
		document.getElementById(h).disabled = true;
	}
}

function ryabina1(lH,lA,h,a) {
		document.getElementById(h).style.display = 'inline';
		document.getElementById(h).disabled = false;
		document.getElementById(a).style.display = 'none';
		document.getElementById(a).disabled = true;
}

function reloadform(path,box) {
	window.location=path + box.options[box.selectedIndex].value;
}