var regemail = /^[+]?[_a-z0-9-]+(\.[_a-z0-9-]+)*@([0-9a-z][0-9a-z-]*\.)+[a-z]{2,4}$/i;
var regphone = /^[+ 0-9\-]+$/;
var regzip = /^[A-Za-z 0-9-]+$/;
var regpernr = /^[0-9]+$/;

function Is() {
  var agt=navigator.userAgent.toLowerCase();
  this.ie=((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1))?1:0;
  this.dom=document.getElementById?1:0;
  this.ie4=(document.all && !this.dom)?1:0;
  this.ns4=(document.layers && !this.dom)?1:0;
  return this;
}
var is=new Is();
if(is.ns4) {
	var regSurnames = /^[ ]*[a-z]+[ ]*[a-z]+[ a-z]*$/i;
	var regNames = /^[ ]*[a-z]+[ a-z]*$/i;                             
	var regInfSurnames = /^[ ]*[a-z]{2,}[ ]*$/i;
	var regInfNames = /^[ ]*[a-z]{1,}[ ]*$/i;
	var regContact = /^[ ]*[a-z0-9,.:_"\'()&#*+\/-]+[ a-z0-9,.:_"\'()&#*+\/-]*$/i;
}
else {
	var charAllowed = ' ';
	var regSurnames = new RegExp("^[ ]*[a-z" + charAllowed + "]+[ ]*[a-z" + charAllowed + "]+[ a-z" + charAllowed + "]*$", "i");
	var regNames = new RegExp("^[ ]*[a-z" + charAllowed + "]+[ a-z" + charAllowed + "]*$", "i");
	var regInfSurnames = new RegExp("^[ ]*[a-z" + charAllowed + "]{2,}[ ]*$", "i");
	var regInfNames = new RegExp("^[ ]*[a-z" + charAllowed + "]{1,}[ ]*", "i");
	var regContact = new RegExp("^[ ]*[a-z" + charAllowed + "0-9,.:_\"'()&#\/*+-]+[ a-z" + charAllowed + "0-9,.:_\"'()&#\/*+-]*$", "i");
}
////////////////////////////////////////////////////////////////
function LAT(s) {
    for (i=0; i<s.length; i++) {
        if (((s.charAt(i)<'a') || (s.charAt(i)>'z')) && ((s.charAt(i)<'A') || (s.charAt(i)>'Z')) && (s.charAt(i) != '-'))
            return false
    }
    return true;
}
function ReplaceChar(s1,tt){
    s2 ="";
    for (i=0; i<s1.length; i++) {
        if (((s1.charAt(i)<'a') || (s1.charAt(i)>'z')) && ((s1.charAt(i)<'A') || (s1.charAt(i)>'Z')) && ((s1.charAt(i)<'0') || (s1.charAt(i)>'9')))
            s2=s2+"_";
        else
            s2=s2+s1.charAt(i);
    }
    eval(tt+" = '" + s2.toUpperCase() + "'");
}
function Translate(s1,tt) {
	var charcode;
    EnglishEquivalent1 = new Array('A','A','','','S','S','','','C','C','','','','','U','U','','','E','E','E','E','U','U','','','','','','','','','','Z','Z','','','','I','I','','','I','I','','','','','','','','','','','','','I','','','', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
    EnglishEquivalent2 = new Array('A','A','','','S','S','','','C','C','','','','','E','E','','','E','E','E','E','U','U','','','','','','','G','G','','Z','Z','35','36','37','I','I','','','I','I','','','','','','','K','K','','','','I','I','','','', '', '', '', '', '', 'N', 'N', '', '', '', '', '');
    EnglishEquivalent_ru = new Array('A','B','V','G','D','E','ZH','Z','I','J','K','L','M','N','O','P','R','S','T','U','F','H','C','SH','SHT','CH','','','J','Z','JU','JA','','','');
    
    s2 ='';
    for (j=0; j<s1.length; j++) {
    	//s1 = s1.toUpperCase();
    	charcode = s1.charCodeAt(j);
    	//alert(s1[j] + ' ' + s1.charCodeAt(j) + '[' + ((s1.charCodeAt(j)>350)?(s1.charCodeAt(j) - 348):(s1.charCodeAt(j) - 260)) + ']');
        if ((s1.charCodeAt(j)>259) && (s1.charCodeAt(j)<383))
            if (s1.charCodeAt(j)>350) {
            	s2 = s2 + EnglishEquivalent1[(s1.charCodeAt(j) - 348)];
            }
            else {
            	s2 = s2 + EnglishEquivalent2[(s1.charCodeAt(j) - 260)];
            }
        else
			if ((s1.charCodeAt(j)>1039) && (s1.charCodeAt(j)<1104)) {
           		 s2 = s2 + EnglishEquivalent_ru[(s1.charCodeAt(j)>1071)?(s1.charCodeAt(j) - 1072):(s1.charCodeAt(j) - 1040)];
            }
        		else
        			if ( (charcode == 256) || (charcode == 257) ) {
        				s2 = s2 + 'A';
        			}
        			else
            			s2 = s2 + s1.charAt(j);
        				eval(tt+" = '" + trim(s2.toUpperCase()) + "'");
    }
    return false;
}
function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}
function TranslateName(s1,tt){
    if (LAT(s1)) return true;
    Translate(s1,tt);
    return false;
}
function TranslateAll(s1,tt){
    Translate(s1,tt);
    s3="";
    eval("s3="+tt);
    ReplaceChar(s3,tt);
}
function fullnameLength(surname, firstname, title) {
    var fullname = "" + surname + "/" + firstname + "" + title;
    return (fullname.length <= 27);
}
function IsLeapYear(y) {
   return (0 == y%4 && ((y%100 != 0) || (y%400 == 0)));
}
function DayEnd(m, y) {
   m -= 1;
   d = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
   return ((IsLeapYear(y)) && (m == 1)) ? 29 : d[m];
}
function checkBirthDates(day, month, year, min, max) {
  var depDay = departureDate.substr(0,2);
  var depMonth = departureDate.substr(2,2);
  var depYear = departureDate.substr(4,2);

  year1 = parseInt(year,10)+min;
  year3 = parseInt(year,10)+max;
  var stamp1 = new Date(year3, parseInt(month,10)-1, day);
  var stamp2 = new Date(2000+parseInt(depYear,10), parseInt(depMonth,10)-1, depDay);
  var stamp3 = new Date(year1, parseInt(month,10)-1, day);
  return ((stamp3 <= stamp2) && (stamp2 < stamp1));
}
////////////////////////////////////////////////////////////////


function checkInfantsNr() {
	var option_array = new Array(10);
	var url_array = new Array(10);
	option_array[0] = new Array("0");
	url_array[0] = new Array("0");
	option_array[1] = new Array("0", "1");
	url_array[1] = new Array("0", "1");
	option_array[2] = new Array("0", "1", "2");
	url_array[2] = new Array("0", "1", "2");
	option_array[3] = new Array("0", "1", "2", "3");
	url_array[3] = new Array("0", "1", "2", "3");
	option_array[4] = new Array("0", "1", "2", "3", "4");
	url_array[4] = new Array("0", "1", "2", "3", "4");
	option_array[5] = new Array("0", "1", "2", "3", "4", "5");
	url_array[5] = new Array("0", "1", "2", "3", "4", "5");
	option_array[6] = new Array("0", "1", "2", "3", "4", "5", "6");
	url_array[6] = new Array("0", "1", "2", "3", "4", "5", "6");
	option_array[7] = new Array("0", "1", "2", "3", "4", "5", "6", "7");
	url_array[7] = new Array("0", "1", "2", "3", "4", "5", "6", "7");
	option_array[8] = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8");
	url_array[8] = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8");
	option_array[9] = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
	url_array[9] = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
	sel = 0;
	selIndex = 0;
	
	selIndex= window.document.forms['reservform'].infant.selectedIndex;
	for (loop= window.document.forms['reservform'].infant.options.length- 1; loop> 0; loop--) {
		window.document.forms['reservform'].infant.options[loop] = null;
	}
	max_loop= window.document.forms['reservform'].adult.selectedIndex+ window.document.forms['reservform'].youth.selectedIndex;
	if (max_loop>9) max_loop=9;
	for (loop= 0; loop< option_array[max_loop].length; loop++) {
		window.document.forms['reservform'].infant.options[loop] = new Option(option_array[max_loop][loop]);
		window.document.forms['reservform'].infant.options[loop].value=url_array[max_loop][loop];
	}
	if (window.document.forms['reservform'].infant.options.length> selIndex) {
		window.document.forms['reservform'].infant.selectedIndex= selIndex;
	}
	else {
		window.document.forms['reservform'].infant.selectedIndex= 0;
	}
}
//
function set_orig() {
	selIndex= window.document.forms['reservform'].infant.selectedIndex;
	if (window.document.forms['reservform'].adult.selectedIndex!= 1) {
		checkInfantsNr();
	}
	else {
		window.document.forms['reservform'].adult.selectedIndex = 1;
	}
	if (window.document.forms['reservform'].infant.options.length>= selIndex) {
		window.document.forms['reservform'].infant.selectedIndex= selIndex;
	}
	else {
		window.document.forms['reservform'].infant.selectedIndex= 0;
	}
}

function changeImages(el, status){
    if(el)
	    el.src = (status) ? "img/ico_issamiau.gif" : "img/ico_issamiau.gif";
}
function switchVisibility(elem, status) {
  var elm = document.getElementById(elem);
  if(elm)
	  elm.style.display = (status) ? "" : "none";
}
function switchVisibility2(elem) {
  var elm = document.getElementById(elem);
  if(elm)
	  elm.style.display = (elm.style.display == "none") ? "" : "none";
}
function iterateSwitch(id, status, start, end){
	for(var k = start; k <= end; k++)
		eval("switchVisibility('" + id + k + "', " + status + ");");	
}

function getRadio(formName, elName){
	var Ctrl = document.forms[formName];
	var el;
	if(!Ctrl)
		return false;

	eval("el = Ctrl." + elName);
	if(!el)
		return false;

  	if(!el.length && el.checked == 1) {	
    	return el.value
	}
	else {
    	for(var z=0; z<el.length; z++)
     		if(el[z].checked)
	     	   	return el[z].value;
  	}
	return false;
}
function setRadio(name, wanted, unwanted){
	elm = document.getElementsByName(name);
	if(!elm || !elm.length)
		return;

	if(unwanted && unwanted.length) {
		for(var k = 0; k < elm.length; k++) {
			if(elm[k].value == unwanted && elm[k].checked) {
				return setRadio(name, wanted);
			}
		}
	}
	else {
		for(var k = 0; k < elm.length; k++) {
			if(elm[k].value == wanted) {
				elm[k].checked = true;
				return true;
		    }
		}
	}
	return false;
}

function num_format(num) {
  num = num+'';
  num = num.replace(",", ".");
  num= parseFloat(num);
  num= num.toFixed(2); 
  //num= num.toFixed(0); 
  //return num.replace(".", ",");
  return num;
}

function psg_disabled_country(form_name, select_name, adr_row_name, adr_row_from, adr_row_to, adr_row_2) {
	var sel_elm= document.getElementById(select_name);

   if (sel_elm) {
   		var add_arr= sel_elm.value.split("|");
		if (add_arr[2] == 1) iterateSwitch(adr_row_name, 0, adr_row_from, adr_row_2);
		else if (add_arr[2] == 2) {
			iterateSwitch(adr_row_name, 0, adr_row_from, adr_row_to);
			iterateSwitch(adr_row_name, 1, adr_row_2, adr_row_2);
		} else {
			iterateSwitch(adr_row_name, 1, adr_row_from, adr_row_to);
			iterateSwitch(adr_row_name, 0, adr_row_2, adr_row_2);
		}
	}
}

function psg_addelprice(formName,pricetoad,priceflight_name,priceinsurance_name,sum_topay_name,sum_delivery) {
   var Ctrl= document.forms[formName];
	var sel_elm= document.getElementById(pricetoad);
	var pricetotal= document.getElementById(priceflight_name);
	var priceinsurance= document.getElementById(priceinsurance_name);
	if (sel_elm) {
		var add_arr= sel_elm.value.split("|");
		add_arr[0] = add_arr[0]+"";
		add_arr[0] = add_arr[0].replace(",", ".");
		
	   num_done= num_format(parseFloat(pricetotal.value)+parseFloat(priceinsurance.value)+parseFloat(add_arr[0]));
	   num_done = num_done+'';
  	   num_done = num_done.replace(".", ",");
  	   var del = num_format(add_arr[0]);
  	   var del = del+'';
  	   del = del.replace(".", ",");
	   switch_inner_html(sum_topay_name, "0", num_done, "0");
	   switch_inner_html(sum_delivery, "0", del, "0");
	}
}

function switch_inner_html(elem, status, inner_yes, inner_no) {
  var elm;
  elm = document.getElementById(elem);
  if(elm)
	  elm.innerHTML = (status=='0') ? inner_yes : inner_no;
}

function goLocation(href) {
  w = window.open(href, "info", "resizable=yes,scrollbars=yes,width=640,height=480,left="+Math.floor((screen.width-640)/2)+",top="+Math.floor((screen.height-480)/2));
  if(w) w.focus();
}