var checkAll = true;

var ie=ns=op=false;

if (navigator.userAgent.indexOf ("Opera") != -1) {op=true}
else if (navigator.userAgent.indexOf ("MSIE") != -1) {ie=true}
else if (navigator.userAgent.indexOf ("Netscape") != -1 ||
         navigator.userAgent.indexOf ("Gecko") != -1 ||
         navigator.userAgent.indexOf ("Mozilla") != -1 ||
         navigator.userAgent.indexOf ("Firefox") != -1) {ns=true}


/*function SetCookie(name, value, hours, path, domain, secure)
{
 var today = new Date();
 expires = new Date(today.getTime() + hours*60*60*1000);
 document.cookie = name + "=" + escape(value) +
                  ((hours) ? "; expires=" + expires.toGMTString() : "") +
                  ((path) ? "; path=" + path : "") +
                  ((domain) ? "; domain=" + domain : "") +
                  ((secure) ? "; secure" : "");
}*/


function btnOver(th, n)
{
 th.src = "_gfx/icons/"+n+"-ovr.gif";
}


function btnOut(th, n)
{
 th.src = "_gfx/icons/"+n+".gif";
}


function DefVal(th, val, act)
{
 switch (act)
  {
   case 1:
    if (th.value == val)
     {
      th.value = '';
     }
    break;
   case 0:
    if (th.value == '')
     {
      th.value = val;
     }
    break;
  }
}


function ShowMnuGroup(s, n)
{
 var obj = document.getElementById(s+'mnuGroup'+n);
 if (obj)
  {
   if (obj.className == "hid")
    {
     obj.className = "";
    }
   else
    {
     obj.className = "hid";
    }
  }
}


function ShowServiceText(n)
{
 var obj = document.getElementById('text'+n);
 if (obj)
  {
   if (obj.className == "hid")
    {
     obj.className = "";
    }
   else
    {
     obj.className = "hid";
    }
  }
}

function SwitchPanels(p, arrData)
{
 var obj = document.getElementById(p);
 if (obj)
  {
   obj.className = "vis";
  }
 for(var i=0; i<arrData.length; i++)
  {
   var obj = document.getElementById(arrData[i]);
   if (obj)
    {
     obj.className = "hid";
    }
  }
}


function ShowLoginWin()
{
 if (!ie)
  {
   var width = screen.availWidth;
   var height = screen.availHeight;
  }
 else
  {
   var width = screen.width;
   var height = screen.height;
  }
 var top = (height - 200 - (op ? 130 : 0))/2;
 var left = (width - 300)/2;

 var par="top="+top+",left="+left+",width=300,height=200,copyhistory=0,directories=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0";
 LoginW=window.open("login.php","LoginWindow",[par]);
 LoginW.focus();
}


function ShowMessageWin()
{
 if (!ie)
  {
   var width = screen.availWidth;
   var height = screen.availHeight;
  }
 else
  {
   var width = screen.width;
   var height = screen.height;
  }
 var top = (height - 200 - (op ? 130 : 0))/2;
 var left = (width - 300)/2;

 var par="top="+top+",left="+left+",width=300,height=200,copyhistory=0,directories=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0";
 MessageW=window.open("message.php","MessageWindow",[par]);
 MessageW.focus();
}

function ShowCabinetWin()
{
 if (!ie)
  {
   var width = screen.availWidth;
   var height = screen.availHeight;
  }
 else
  {
   var width = screen.width;
   var height = screen.height;
  }
 var top = (height - 480 - (op ? 130 : 0))/2;
 var left = (width - 640)/2;

 var par="top="+top+",left="+left+",width=640,height=480,copyhistory=0,directories=0,location=0,menubar=0,resizable=0,scrollbars=1,status=0,toolbar=0";
 CabinetW=window.open("cabinet.php","CabinetWindow",[par]);
 CabinetW.focus();
}


function ShowNewsWin(id)
{
 var par="copyhistory=0,directories=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0,height=400,width=600,top=300,left=300";
 NewsW=window.open("news.php?id="+id,"NewsWindow",[par]);
 NewsW.focus();
}

function ShowPhotoWin(id)
{
 var par="copyhistory=0,directories=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0";
 PhotoW=window.open("photo.php?id="+id,"PhotoWindow",[par]);
 PhotoW.focus();
}

function ShowPhotoWin2(tid)
{
 var par="copyhistory=0,directories=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0";
 PhotoW=window.open("photo.php?tid="+tid,"PhotoWindow",[par]);
 PhotoW.focus();
}

function ShowRegistrWin()
{
 if (!ie)
  {
   var width = screen.availWidth;
   var height = screen.availHeight;
  }
 else
  {
   var width = screen.width;
   var height = screen.height;
  }
 var top = (height - 320 - (op ? 130 : 0))/2;
 var left = (width - 420)/2;

 var par="top="+top+",left="+left+",width=420,height=320,copyhistory=0,directories=0,location=0,menubar=0,resizable=0,scrollbars=1,status=0,toolbar=0";
 RegistrW=window.open("registr.php","RegistrWindow",[par]);
 RegistrW.focus();
}


function ShowReservWin(id)
{
 if (!ie)
  {
   var width = screen.availWidth;
   var height = screen.availHeight;
  }
 else
  {
   var width = screen.width;
   var height = screen.height;
  }
 var top = (height - 480 - (op ? 130 : 0))/2;
 var left = (width - 640)/2;

 var par="top="+top+",left="+left+",width=640,height=480,copyhistory=0,directories=0,location=0,menubar=0,resizable=0,scrollbars=1,status=0,toolbar=0";
 ReservW=window.open("reserv.php?tid="+id,"ReservWindow",[par]);
 ReservW.focus();
}


function SetDaysSelect(fName, vName)
{
 var num_days = 0;
 var year = document.forms[fName][vName+'_YEAR'].value;
 var month = document.forms[fName][vName+'_MONTH'].value;
 var day = document.forms[fName][vName+'_DAY'].value;
 var day_val = '';
 document.forms[fName][vName+'_DAY'].options.length = 0;
 switch(month)
  {
   case '01':
   case '03':
   case '05':
   case '07':
   case '08':
   case '10':
   case '12':
    num_days = 31;
    break;
   case '04':
   case '06':
   case '05':
   case '09':
   case '11':
    num_days = 30;
    break;
   case '02':
    if ((year % 4) == 0)
     {
      num_days = 29;
     }
    else
     {
      num_days = 28;
     }
    break;
  }
 for (var i = 0; i < num_days; i++)
  {
   if (i < 9)
    {
     day_val = '0'+(i+1);
    }
   else
    {
     day_val = i+1;
    }
   document.forms[fName][vName+'_DAY'].options[i] = new Option(i+1, day_val);
  }
 if ((day - 1) < document.forms[fName][vName+'_DAY'].options.length)
  {
   document.forms[fName][vName+'_DAY'].options[day-1].selected = true;
  }
 else
  {
   document.forms[fName][vName+'_DAY'].options[0].selected = true;
  }
}


function SetDate(fName, vName)
{
 var year = document.forms[fName][vName+'_YEAR'].value;
 var month = document.forms[fName][vName+'_MONTH'].value;
 var day = document.forms[fName][vName+'_DAY'].value;
 document.forms[fName][vName].value = year+'-'+month+'-'+day;
}


function CheckForm(fName, arrData, chkPass)
{
 for(var i=0; i<arrData.length; i++)
  {
   if ((document.forms[fName][arrData[i]].type == 'text' || document.forms[fName][arrData[i]].type == 'password' || document.forms[fName][arrData[i]].type == 'hidden' || document.forms[fName][arrData[i]].type == 'file' || document.forms[fName][arrData[i]].type == 'select-one' || document.forms[fName][arrData[i]].type == 'textarea') && !document.forms[fName][arrData[i]].value)
    {
     alert('Необходимо заполнить все поля, помеченные звездачкой!');
     return false;
    }
  }
 if (chkPass && (document.forms[fName][arrData[0]].value != document.forms[fName][arrData[1]].value))
  {
   alert('Пароль и подтверждение пароля не совпадают!');
   return false;
  }
 document.forms[fName].submit();
}

function CheckKeyCode(evt, str, type)
{
 evt = (evt) ? evt : event;
 var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : null));
 switch (type)
  {
   case 'en_str' :
    //Латинские буквы, цифры, _ и -
    //if (charCode > 31 && !(charCode == 37 || charCode == 39) && !(charCode > 47 && charCode < 58) && !(charCode > 64 && charCode < 91) && !(charCode > 96 && charCode < 123) && charCode != 95)
    if (charCode > 31 && !(charCode > 47 && charCode < 58) && !(charCode > 64 && charCode < 91) && !(charCode > 96 && charCode < 123) && charCode != 45 && charCode != 95)
     {
      return false;
     }
    break;

/*   case 'en_str_sp' :
    //Латинские буквы, цифры и все спецсимволы
    if (!(charCode > 31 && charCode < 127))
     {
      return false;
     }
    break;*/

   case 'int' :
    //Цифры
    if (charCode > 31 && !(charCode > 47 && charCode < 58))
     {
      return false;
     }
    break;

/*   case 'float' :
    //Цифры и .
    var d = (str.indexOf('.') == -1) ? false : true;
    //if (charCode > 31 && !(charCode == 37 || charCode == 39) && !((charCode == 46 || charCode == 44) && !dot) && !(charCode > 47 && charCode < 58))
    if (charCode > 31 && !(charCode == 46 && !d) && !(charCode > 47 && charCode < 58))
     {
      return false;
     }
    break;*/

   case 'email' :
    //Формат e-mail
    var d = (str.indexOf('@') == -1) ? false : true;
    //if (charCode > 31 && !(charCode == 37 || charCode == 39) && !((charCode == 46 || charCode == 44) && !dot) && !(charCode > 47 && charCode < 58))
    if (charCode > 31 && !(charCode == 64 && !d) && !(charCode > 44 && charCode < 58) && !(charCode > 64 && charCode < 91) && !(charCode > 96 && charCode < 123) && charCode != 95 || charCode == 47)
     {
      return false;
     }
    break;

/*   case 'spec' :
    //Для спецификаций
    if (charCode > 31 && !(charCode > 38 && charCode < 58) && !(charCode > 64 && charCode < 91) && !(charCode > 96 && charCode < 123) && !(charCode > 1039 && charCode < 1104) && charCode != 32 && charCode != 34 && charCode != 92 && charCode != 95 && charCode != 8470)
     {
      return false;
     }
    break*/;
  }
 return true;
}


function CheckInput(fName, vName, type)
{
 var str = document.forms[fName][vName].value;
 if (!str)
  {
   return false;
  }
 switch (type)
  {
   case 'en_str' :
    var re = /^[\w-]{1,255}$/i;
    break;

/*   case 'en_str_sp' :
    var re = /^[\w ~`!@#$%^&*()_+=\[\]\{\}\\\/:;<>,.\?|"'-]{1,255}$/i;
    break;*/

   case 'int' :
    var re = /^[+]?\d*$/;
    break;

/*   case 'float' :
    //var re = /^[+]?[0-9]*\.?[0-9]*$/;
    var re = /^[+]?\d*\.?\d*$/;
    break;*/

   case 'email' :
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-z]{2,4}$/i;
    break;

/*   case 'spec' :
    var re = /^[a-z0-9а-я_.,"'-\\\/ ()*№]{1,255}$/i;
    break;*/
  }
 if (!str.match(re))
  {
   document.forms[fName][vName].value = '';
  }
}


function CheckTCountInput(fName, vName)
{
 if (parseInt(document.forms[fName][vName].value) < 1)
  {
   document.forms[fName][vName].value = 1;
  }
}


function CalculateTourPrice()
{
 var price = 0;
 var chk_str = '';
 var chks = document.getElementsByName('SERVICE[]');
 for (var i = 0; i < chks.length; i++)
  {
   if (chks[i].type == 'hidden' || (chks[i].type == 'checkbox' && chks[i].checked))
    {
     chk_str = chk_str+'|'+chks[i].value;
     price += Prices[i];
    }
  }
 document.getElementById('PRICE').innerHTML = "<font style='font-size: 17px; color: #4C4C4C'>Цена: </font>"+price+" "+Currency;
 document.ReservData.CHECKS.value = chk_str.substring(1);
}

function ChangeService(sr, n, d)
{
 var price = 0;
 var sr_data = Services[sr+'_'+n][d];
 for (var i = 1; i < sr_data.length; i++)
  {
   price += parseFloat(sr_data[i]);
   document.getElementById('SPEC'+sr+'_'+n+'_'+i).value = d;
   if (parseFloat(sr_data[i]))
    {
     document.getElementById('SPRICE'+sr+'_'+n+'_'+i).innerHTML = sr_data[i]+" "+Currency;
    }
   else
    {
     document.getElementById('SPRICE'+sr+'_'+n+'_'+i).innerHTML = '';
    }
  }
 Prices[n-1] = price;
 CalculateTourPrice();
 document.ReservData["DATES["+sr+"_"+n+"]"].value = parseInt(sr_data[0]);
}

function bigPhotoOpen(imgID)
{
	window.open("/_new/foto/picofday/foto.html", "big_photo", "toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=0,scrollbars=0,width="+(document.all[imgID].width)*4+",height="+(document.all[imgID].height)*4+",top=300,left=300");
}

function resetBaseFormParameters()
{
	allEl=document.all;
	allEl.fTypeDefVal.selected=true;
	allEl.countryDefVal.selected=true;
	allEl.regionsDefVal.selected=true;
	allEl.search_region.disabled=true;
	allEl.recomendDefVal.selected=true;
	allEl.typesDefVal.selected=true;
	allEl.foodDefVal.selected=true;
	allEl.catDefVal.selected=true;
}

function closeInfoDiv()
{
	var i=1;
	while(document.all["cot_info_div"+i]!=null)
		document.all["cot_info_div"+i++].className='hide';

	i=1;
	while(document.all["cot_info_div_c"+i]!=null)
		document.all["cot_info_div_c"+i++].className='hide';

	i=1;
	while(document.all["cot_info_div_r"+i]!=null)
		document.all["cot_info_div_r"+i++].className='hide';
}

function showInfoDiv(event, i)
{
	closeInfoDiv();
	obj=document.getElementById("cot_info_div"+i);
	if(ns)
	{
		obj.style.top=event.pageY;
		obj.style.left=event.pageX-200;
	}
	else if(ie || op)
	{
		obj.style.top=event.y+document.body.scrollTop;
		obj.style.left=(event.x+document.body.scrollLeft)-200;
	}
	obj.className='';
}

function submitOrderSearch(formName, formAction)
{
	if(document.all['elCount'].options[document.all['elCount'].selectedIndex])
		formAction+="&page_el_count="+document.all['elCount'].options[document.all['elCount'].selectedIndex].value+'#form';

	document.all[formName].action=formAction;
	if(document.all['regionDefVal']!=null)
		document.all['regionDefVal'].selected=true;
	document.all[formName].submit();
}

function submitSearchForm(formName, formAction)
{
	document.all[formName].action=formAction;
	document.all[formName].submit();
}

function submitTourSearch(formName, formAction)
{
	document.all[formName].action=formAction;
	document.all[formName].submit();
}
