var s_int = 0;
var cur_obj = 0;
var cur_dir = 0;
var old_pos = 0;
var s_step = 0;

var autodir = 1;
var autostep = 1;
var autodelay=90;
var autopos;
var need_scroll = true;


var s_cleared = false;

function validateEmail(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }

	 if (str.indexOf(" ")!=-1){
	    return false
	 }

	 return true					
}

$(window).resize (function(){
	$('.scroller').each( function(){ 
        	fixResize(this.id);
    	});
    	fixCities();
});

$(document.body).ready(  function(){
    $('.jq-width').each( function(){
      if ( navigator.userAgent.toLowerCase().indexOf("msie") )
      {
        var jqw_max, jqw_min ;
        if ( ! parseFloat(jqw_max = $(this).css('max-width')) )
          jqw_max = '*';
        if ( ! parseFloat(jqw_min = $(this).css('min-width')) )
          jqw_min = '0';
 
        $(this).wrap('<table border="0" cellspacing="0" cellpadding="0"><tr border="0"><td border="0" width="'+jqw_max+'"><div width="'+jqw_min+'">'+'</div></td></tr></table>');
      }
    });
    
    $('.scroller').each( function(){ 
        	fixResize(this.id);
    });
    
    fixCities();
});



function checkForm() {
  var frm = document.forms['mail'];
  var error = "";
  if(frm.elements['Date'].value=="") error+="Пожалуйста введите Дату\n";
  if(frm.elements['Name'].value=="") error+="Пожалуйста введите Ваше имя\n";
  if(frm.elements['Phone'].value=="") error+="Пожалуйста введите Ваш контактный телефон\n";
  if(error==""){
  	frm.elements['action'].value = "doPostForm";
  	return true;
  }
  else {
    alert(error);
    return false;
  }
} 

function clearSearch(){
 if(s_cleared) return;
 else {
 document.forms['searchFrm'].elements['s'].value = "";
 s_cleared = true;
 }
}

function runActiveContent(what) {
	document.write(what);
}

function bookmark(){
	if (window.sidebar)
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print)
	{ 
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)
		window.external.AddFavorite(url, title);
	else
		alert("Ошибка добавления в избранное!");
	return false;
}

var name_cleared = false;
var date_cleared = false;
var phone_cleared = false;
var time_cleared = false;

function clearName(){
  if(name_cleared) return;
  else {
    document.forms['mail'].elements['Name'].value = "";
    name_cleared = true;
  }
}

function clearDate(){
  if(date_cleared) return;
  else {
    document.forms['mail'].elements['Date'].value = "";
    mail_cleared = true;
  }
}

function clearTime(){
  if(time_cleared) return;
  else {
    document.forms['mail'].elements['Time'].value = "";
    mail_cleared = true;
  }
}

function clearPhone(){
  if(phone_cleared) return;
  else {
    document.forms['mail'].elements['Phone'].value = "";
    phone_cleared = true;
  }
}

function autoScroll() {
	if(need_scroll) {
		cur_obj = document.getElementById("scroll1"); // events
		if(cur_obj.scrollLeft<autostep&&autodir<0) {
			autodir = 1;
		}
		if(cur_obj.scrollLeft+cur_obj.style.width>cur_obj.scrollWidth-autostep&&autodir>0) {
			autodir = -1;
		}
		autopos = cur_obj.scrollLeft+autodir*autostep;
		cur_obj.scrollLeft = autopos;
	}

}

function doStartScroll(what, dir, step) {
	if(cur_obj==0) cur_obj = document.getElementById("scroll"+what);
	if(cur_dir==0) cur_dir = dir;
	if(old_pos==0) old_pos = cur_obj.scrollLeft;
	if(step>0) s_step = step;
	else step = s_step;
	
	cur_obj.scrollLeft+=cur_dir*step;
	
	//if(cur_obj.scrollLeft+cur_dir*step > cur_obj.scrollWidth-step) {
		
	//}
	//alert("doScroll("+cur_obj.id+", "+cur_dir+"; sl="+cur_obj.scrollLeft+"; s_int="+s_int+"; op="+old_pos+")");
	//if(cur_obj.scrollLeft<=0||cur_obj.scrollLeft>=cur_obj.scrollWidth) {
		//cur_obj.scrollLeft = 0;
		//doStopScroll();
		//alert("finished!");
	//}
	//else {
		if(s_int==0) s_int=setInterval("doStartScroll()", 15);
		//alert("going on; s_int="+s_int);
	//}
}

function doStopScroll() {
	clearInterval(s_int);
	s_int = 0;
	cur_obj = 0;
	cur_dir = 0;
	old_pos = 0;
}

function show(){
	//alert(document.getElementById('table').scrollWidth);
	//alert(document.body.clientWidth);
}

function fixResize(obj) {
	//return false;
	//if(obj==1) setInterval("autoScroll()", autodelay);
	if(obj!="scroll1"&&obj!="scroll2") width = document.body.clientWidth - 115;
	else width = document.body.clientWidth - 44;
	//min = document.getElementById('table').clientWidth - 44;
	min = 954;
	if(min > width)
		width = min;
	//alert(obj+"; "+width);
	//alert(obj);
	document.getElementById(obj).style.cssText="position: relative; overflow: hidden; width: "+width+"px";
	if(obj=="scroll1") document.getElementById(obj).scrollLeft = scrl;
}

function fixCities() {
	var wd = document.body.clientWidth-2;
	$('.scroller_container').each( function(){
		this.style.cssText="position: relative; width: "+wd.toString()+"px";
	});
}

var id = '1';
var old_id = '1';
function showLine(id) {
	document.getElementById('com'+old_id).style.cssText = "background: none";
	document.getElementById('com'+id).style.cssText = "background: url("+dp+"images/line.gif) left 8px no-repeat";
	document.getElementById('field').innerHTML = document.getElementById('text'+id).value;
	old_id = id;
}

function Check(id) {
	if (document.getElementById('complex'+id).className == "checkboxOn") {
		document.getElementById('complex'+id).className = "checkboxOff";
		document.getElementById('value'+id).value = "";
	}else {
		document.getElementById('complex'+id).className = "checkboxOn";
		document.getElementById('value'+id).value = document.getElementById('complex'+id).innerHTML;
	}
}

function ValetCheck() {
	if (document.getElementById('valet').className == "checkboxOn") {
		document.getElementById('valet').className = "checkboxOff";
		document.getElementById('valetValue').value = "Нет";
	}else {
		document.getElementById('valet').className = "checkboxOn";
		document.getElementById('valetValue').value = "Да";
	}
}

function showBg(id, dp){
	document.getElementById('left' + id).src = dp + "/menu_l_a.gif";
	document.getElementById('right' + id).src = dp + "/menu_r_a.gif";
}

function hideBg(id, dp){
	document.getElementById('left' + id).src = dp + "/menu_l.gif";
	document.getElementById('right' + id).src = dp + "/menu_r.gif";
}

function change(){
	/*var doc;
	iframe = window.frames['ifr'];
	//iframe.src = 'javascript:;';

	if(iframe.contentDocument) doc = iframe.contentDocument;
	else if(iframe.contentWindow) doc = iframe.contentWindow.document;
	else if(iframe.document) doc = iframe.document;

	//alert(document.getElementById('m').value);

	//doc.getElementById('m').value = document.getElementById('m').value;
	//doc.getElementById('y').value = document.getElementById('y').value;

	alert(document.getElementById('m').value);*/

	document.forms['cal'].submit();
}

function showCalendar(){
	var doc;
	iframe = window.frames['ifr'];
	//iframe.src = 'javascript:;';

	if(iframe.contentDocument) doc = iframe.contentDocument;
	else if(iframe.contentWindow) doc = iframe.contentWindow.document;
	else if(iframe.document) doc = iframe.document;

	if(doc == null) return null;
	else alert(doc.getElementById('calendar').innerHTML);
	
	document.getElementById('calendar').innerHTML = doc.getElementById('calendar').innerHTML;
}
