var allSupport = document.all!=null
var getElementBy = document.getElementById!=null
var domImage = document.images!=null

if (ANKRES_MONTH_NAMES == null || typeof(ANKRES_MONTH_NAMES) == 'undefined' || typeof(ANKRES_MONTH_NAMES) != 'object')
{
	var ANKRES_MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
}
if (ANKRES_MONTH_NAMES_ABBR == null || typeof(ANKRES_MONTH_NAMES_ABBR) == 'undefined' || typeof(ANKRES_MONTH_NAMES_ABBR) != 'object')
{
	var ANKRES_MONTH_NAMES_ABBR=new Array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
}
if (ANKRES_MONTH_LENGTH == null || typeof(ANKRES_MONTH_LENGTH) == 'undefined' || typeof(ANKRES_MONTH_LENGTH) != 'object')
{
	var ANKRES_MONTH_LENGTH=new Array('31','28','31','30','31','30','31','31','30','31','30','31');
}
if (ANKRES_DAY_NAMES == null || typeof(ANKRES_DAY_NAMES) == 'undefined' || typeof(ANKRES_DAY_NAMES) != 'object')
{
	var ANKRES_DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
}


function getElement(elName) {
	// Get an element from its ID
	if (allSupport) {
		return document.all[elName]
	}else{
		if (getElementBy) {
			return document.getElementById(elName)
		}else{
			return document.layers[elName]
		}
	}
}
function initNumericSelect(sel, ss, s, e, def, nozero) {
	sel.length = 0;
	//alert(sel.name);
	var i = 0;
	if (def == "" || def == "undefined" || def == null)
	{
		def = 0;
	}
    if (isNaN(ss)) {
        if (ss == "?")
        {
            var option = new Option("-?-", "");
            sel.options[i] = option;
            i++;
        }else{
            var option = new Option(ss, "");
            sel.options[i] = option;
            i++;
        }
    }
	for (var j = s; j <= e; j++) {
		if (j == 0 && nozero != null) {
			var option = new Option("<1", 0);
		}else{
			var option = new Option(j, j);
		}
		sel.options[i] = option;
		i++;
	}
	if (def == null) {
		sel.options[0].selected = true;
	}else{
		for (var i = 0; i < sel.length; i++) {
			if (sel.options[i].value == def) {
				
				sel.options[i].selected = true;
				sel.options[i].defaultSelected = true;
				
			}
		}
	}
}

function initChildNumericSelect(sel, ss, s, e, def, nozero, max) {
	sel.length = 0;
	//alert(sel.name);
	var i = 0;
	if (def == "" || def == "undefined" || def == null)
	{
		def = 0;
	}
    if (isNaN(ss)) {
        if (ss == "?")
        {
            var option = new Option("-?-", "");
            sel.options[i] = option;
            i++;
        }else{
            var option = new Option(ss, "");
            sel.options[i] = option;
            i++;
        }
    }
    if (e > max) {e = max;}
	for (var j = s; j <= e; j++) {
		if (j == 0 && nozero != null) {
			var option = new Option("<1", 0);
		}else{
			var option = new Option(j, j);
		}
		sel.options[i] = option;
		i++;
	}
	if (def == null) {
		sel.options[0].selected = true;
	}else{
		for (var i = 0; i < sel.length; i++) {
			if (sel.options[i].value == def) {
				
				sel.options[i].selected = true;
				sel.options[i].defaultSelected = true;
				
			}
		}
	}
}

function checkRequestData(thisForm) {

	// verifico le date
	var data_arrivo = getElement('box_data_arrivo_date_hd');
	//alert(data_arrivo.value);
	if (!isDate(data_arrivo.value, jsDateFormat)) {
		// focus
		getElement('selectMonth_box_data_arrivo_date_hd').focus();
		return false;
	}
	var data_partenza = getElement('box_data_partenza_date_hd');
	//alert(data_partenza.value);
	if (!isDate(data_partenza.value, jsDateFormat)) {
		// focus
		getElement('selectMonth_box_data_partenza_date_hd').focus();
		return false;
	}

	// se ci sono bambini verifico l'inserimento della loro età
	var num_rooms = getProperties('camereAdv', 'value');

	if (parseInt(num_rooms)) {
		for (var r = 1; r <= num_rooms; r++) {
			var bambId = 'cameraBambini-'+r;
			if (getElement(bambId).options[getElement(bambId).selectedIndex].value > 0) {
				var num_child = getElement(bambId).options[getElement(bambId).selectedIndex].value;
				for (var e = 1; e <= num_child; e++) {
					var bambEtaId = 'cameraBambini-'+r+'-'+e;
					if (getElement(bambEtaId).options[getElement(bambEtaId).selectedIndex].value == '') {
						getElement(bambEtaId).focus();
						return false;
					}
				}
			}	
		}
	}

	return true;
}

function checkMoreRoomsFormJquery(elm, lng, idt, labelId, contentId, Value) {
	if (Value != "" && Value != null)
	{
		var arrValue = eval(Value);
	}else{
        var arrValue = new Array();
    }
	var numRooms = $(elm).val();
	if (numRooms == "+") {
		/*setChange("pr", "SFND");
		FormSubmit("", "", "/rates.php?lng=" + lng + "&idt_rates=" + idt);*/
	}else{
		var br = "<br \/>";
		var contentElm = getElement(contentId);
		maxChildPerRoom = (maxBambini-1);
		for (var r = 1; r <= maxRooms; r++)
		{	
			var elm = getElement("cameraLabel-"+r);
			if (!elm) {
				if (defPax != "" && defPax != null) {
					var defaultPax = ((parseInt(arrValue["cameraPersone"+r]) > 0) ? parseInt(arrValue["cameraPersone"+r]) : defPax);
				}else{
					var defaultPax = ((parseInt(arrValue["cameraPersone"+r]) > 0) ? parseInt(arrValue["cameraPersone"+r]) : 2);
				}
				var grpLabel= "<td id=\"cameraLabel-"+r+"\" align=\"left\" colspan=\"3\" class=\"ankres-camera-label b02-ankres-camera-label\""+">" + occCameraLabel + " "+r+"<\/td>";
				var selPersone = "<td align=\"left\" valign=\"top\" style=\"padding-left: 5px;\">" + adultiLabel + ":<br \/><select name=\"cameraPersone-"+r+"\" id=\"cameraPersone-"+r+"\" onChange=\"initNumericSelect(getElement(\'cameraBambini-"+r+"\'), 0, 0, (((maxPax-this.options[this.selectedIndex].value >= maxChildPerRoom) ? maxChildPerRoom : maxPax-this.options[this.selectedIndex].value)), 0); displayEtaBambiniRow(getElement(\'cameraBambini-"+r+"\'), "+r+");\" class=\"ankres-select b02-ankres-select\"><\/select><\/td>";
				var selBambini = "<td align=\"left\" valign=\"top\" style=\"padding-left: 5px;\">" + bambiniLabel + ":<br \/><select  name=\"cameraBambini-"+r+"\" id=\"cameraBambini-"+r+"\" onChange=\"displayEtaBambiniRow(this, "+r+");\" class=\"ankres-select b02-ankres-select\"><\/select><\/td>";
				var etaBambini = "";
				var cullaBambini = "";
				
				for (var e = 1; e < maxBambini; e++)
				{
					etaBambini += "<td align=\"center\"><span id=\"cameraEtaBambini-"+r+"-"+e+"\"><select  name=\"cameraBambini-"+r+"-"+e+"\" id=\"cameraBambini-"+r+"-"+e+"\" onChange=\"getFasceEtaWithCradle(\'bambiniCullaLabel-"+r+"\', \'cameraEtaBambiniCulla-"+r+"-"+e+"\', this.options[this.selectedIndex].value, " + r + ");\" class=\"ankres-select b02-ankres-select\"><\/select><\/span><\/td>";
					cullaBambini += "<td align=\"center\"><span id=\"cameraEtaBambiniCulla-"+r+"-"+e+"\" "+"><"+"input type=\"checkbox\" value=\"Y\" name=\"cameraBambiniCulla-"+r+"-"+e+"\" id=\"cameraBambiniCulla-"+r+"-"+e+"\" style=\"width: auto;\" \/>"+"<\/span"+"><\/td>";
				}
				contentElm.innerHTML += "<div id=\"cameraContent-"+r+"\" class=\"ankres-camera-block b02-ankres-camera-block\" align=\"left\"><div class=\"ankres-camera-block-int b02-ankres-camera-block-int\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"ankres-cameracontent b02-ankres-cameracontent\" ><tr>"+grpLabel+"<\/tr><tr>"+selPersone+selBambini+"<td align=\"left\" style=\"padding-left: 5px;\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"cameraEtaLabel-"+r+"\" width=\"99%\" class=\"ankres-cameracontent b02-ankres-cameracontent\" ><tr><td colspan=\""+(maxBambini-1)+"\" align=\"left\"><span id=\"qcameraEtaLabel-"+r+"\">"+etaBambiniLabel+":<\/span><\/td><\/tr><tr>"+etaBambini+"<\/tr><\/table><\/td><\/tr><tr id=\"bambiniCullaLabel-"+r+"\" class=\"ankres-culle-block b02-ankres-culle-block\"><td colspan=\"2\" align=\"right\">"+ cullaLabel +":<\/td><td><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"cameraContentBambiniEta-"+r+"\" class=\"ankres-cameracontent b02-ankres-cameracontent\" width=\"99%\" ><tr>"+ cullaBambini +"<\/tr><\/table><\/td><\/tr><\/table><\/div><\/div>";
				initNumericSelect(getElement("cameraPersone-"+r), 0, minPax, maxPax, defaultPax);
				
				initNumericSelect(getElement("cameraBambini-"+r), 0, 0, maxChildPerRoom, parseInt(arrValue["cameraBambini"+r]));
				for (var e = 1; e < maxPax; e++)
				{
					if (getElement("cameraBambini-"+r+"-"+e))
					{
						var newString = new String(arrValue["cameraBambini"+r+""+e]);
						arrayOfEta = newString.split(",");
						initNumericSelect(getElement("cameraBambini-"+r+"-"+e), "?", 0, 17, parseInt(arrayOfEta[0]), true);
						setVisibile("cameraEtaBambini-"+r+"-"+e, "hidden");
					}
				}
			}
			if (r <= numRooms)
			{
				setVisibility("cameraContent-"+r, "");
				displayEtaBambiniRow(getElement("cameraBambini-"+r), r);
			}else{
				setVisibility("cameraContent-"+r, "none");
			}
		}
        var zIndex = $("#ankres-form-overlay").css("z-index");
        if (numRooms > 0) {
            //$("#ankres-form-overlay *").css("z-index", zIndex);
            //$("#ankres-form-overlay").css("z-index", "2000");
            $("#num-room-content").show();
            if (!$("#ankres-form-overlay").hasClass("bj02-ankres-overlay-bg")) {
                $("#ankres-form-overlay").addClass("bj02-ankres-overlay-bg");
            }
            //alert($("#ankres-form-overlay").height()+" - "+$("#num-room-content").height());
            /*if (!$("#ankres-HideSelect").hasClass("ankres-HideSelect")) {
                $("#ankres-HideSelect").addClass("ankres-HideSelect");
            }*/
            //$("#ankres-HideSelect").height(400).show();
            //$("#ankres-superoverlay").height(400).show();
            //alert($("#ankres-HideSelect").offset().left+" "+ $("#ankres-HideSelect").position().left)
            if (document.getElementById("ankres-HideSelect") !== null) {
                /*$("#ankres-form-overlay").css({'position' : 'absolute'});*/
                $("#ankres-HideSelect").height($("#ankres-form-overlay").outerHeight()).show();
                /*$("#ankres-superoverlay").height($("#ankres-form-overlay").outerHeight()).show();
                $("#ankres-superoverlay").css({
                    'top' : $("#ankres-HideSelect").offset().top+"px",
                    'left' : $("#ankres-HideSelect").offset().left+"px"
                });*/
            }
        }else{
            //$("#ankres-form-overlay").css("z-index", zIndex);
            $("#num-room-content").hide();
            $("#ankres-form-overlay").removeClass("bj02-ankres-overlay-bg");
            $("#ankres-HideSelect").height(0).hide();
            //$("#ankres-superoverlay").height(0).hide();
        }
		if (arrValue) {
			for (var r = 1; r <= maxRooms; r++)
			{	
				for (var e = 1; e < maxPax; e++)
				{
					var newString = new String(arrValue["cameraBambini"+r+""+e]);
					arrayOfEta = newString.split(",");
					if (arrayOfEta[1] == "Y")
					{	
						var checkElm = getElement("cameraBambiniCulla-"+r+"-"+e);
						if (checkElm) {
							checkElm.checked=true;
						}
					}
				}
			}
		}
	}
}
function initComboMontYearSelect(sel, ss, sm, sy, def, nozero) {
    sel.length = 0;
	//alert(sel.name);
	var i = 0;
	if (def == "" || def == "undefined" || def == null)
	{
		def = 0;
	}
    if (isNaN(ss)) {
        if (ss == "?")
        {
            var option = new Option("-?-", "");
            sel.options[i] = option;
            i++;
        }else{
            var option = new Option(ss, "");
            sel.options[i] = option;
            i++;
        }
    }
    var s = sm;
    var e = 24;
    
    var Sy = sy;
    var Sm = sm+11;
	for (var j = s; j <= e; j++) {
		if (j == 0 && nozero != null) {
			var option = new Option("<1", 0);
		}else{
            if (Sm > 23) {
                Sm = 12;
                Sy++;
            }
            //alert(j+" "+ANKRES_MONTH_NAMES[Sm]+" "+Sy);
            Ssy = new String(Sy);
			var option = new Option(ANKRES_MONTH_NAMES[Sm]+" "+Ssy.substr(2,2), Sy+"-"+(((Sm-11) < 10) ? "0" : "")+(Sm-11));
            Sm++;
		}
		sel.options[i] = option;
		i++;
	}
    
	if (def == null) {
		sel.options[0].selected = true;
	}else{
		for (var i = 0; i < sel.length; i++) {
			if (sel.options[i].value == def) {
				
				sel.options[i].selected = true;
				sel.options[i].defaultSelected = true;
				
			}
		}
	}
}


function setStyleProperties(elm_id, stylePropr, styleValue) {
	var elm = getElement(elm_id);	
	//alert(elm.name);
	if (elm) {
		if (allSupport || getElementBy && domImage) {		
			eval('elm.style.' + stylePropr + ' = \'' + styleValue + '\';');
		}else if(domImage) {
			eval('elm.' + stylePropr + ' = \'' + styleValue + '\';');
		}
	}
}

function getStyleProperties(elm_id, stylePropr) {
	var elm = getElement(elm_id);	
	//alert(elm.name);
	var propertyValue = '';
	if (elm) {
		if (allSupport || getElementBy && domImage) {		
			propertyValue =  eval('elm.style.' + stylePropr + ';');
		}else if(domImage) {
			propertyValue =  eval('elm.' + stylePropr + ';');
		}
	}
	return propertyValue;
}

function getProperties(elm_id, Propr) {
	var elm = getElement(elm_id);	
	//alert(elm.name);
	var propertyValue = '';
	if (elm) {
		if (allSupport || getElementBy && domImage) {		
			propertyValue =  eval('elm.' + Propr + ';');
		}else if(domImage) {
			propertyValue =  eval('elm.' + Propr + ';');
		}
	}
	return propertyValue;
}

var cullaLabelVisibility = {};

function displayEtaBambini(elm, r) {
	var childs = elm.options[elm.selectedIndex].value;
	if (childs > 0)
	{
		setVisibile('cameraEtaLabel-'+r, 'visible');
        //setVisibility('cameraEtaLabel-'+r, '');
	}
	else
	{
		setVisibile('cameraEtaLabel-'+r, 'hidden');
        //setVisibility('cameraEtaLabel-'+r, 'none');
		setVisibility('bambiniCullaLabel-'+r, 'none');
	}
	for (var i = 1; i <= maxBambini ; i++)
	{
		if (i <= childs)
		{
			setVisibile('cameraEtaBambini-'+r+'-'+i, 'visible');
			getFasceEtaWithCradle('bambiniCullaLabel-'+r, 'cameraEtaBambiniCulla-'+r+'-'+i, getElement('cameraBambini-'+r+'-'+i).options[getElement('cameraBambini-'+r+'-'+i).selectedIndex].value, r)
			//setVisibility('cameraEtaBambini-'+r+'-'+i, 'table-cell');
			//setVisibility('cameraEtaBambiniCulla-'+r+'-'+i, '');
		}else{
			setVisibile('cameraEtaBambini-'+r+'-'+i, 'hidden');
			setVisibile('cameraEtaBambiniCulla-'+r+'-'+i, 'hidden');
			//setVisibility('cameraEtaBambini-'+r+'-'+i, 'none');
			//setVisibility('cameraEtaBambiniCulla-'+r+'-'+i, 'none');
		}
	}
}
function displayEtaBambiniRow(elm, r) {
	var childs = elm.options[elm.selectedIndex].value;
	if (childs > 0)
	{
		
        setVisibility(elm.id.replace('cameraBambini-'+r,'cameraEtaLabel-'+r), '');
	}
	else
	{
		
        setVisibility(elm.id.replace('cameraBambini-'+r,'cameraEtaLabel-'+r), 'none');
		setVisibility(elm.id.replace('cameraBambini-'+r,'bambiniCullaLabel-'+r), 'none');
	}
	for (var i = 1; i <= maxBambini ; i++)
	{
		if (i <= childs)
		{
			setVisibile(elm.id.replace('cameraBambini-'+r,'cameraEtaBambini-'+r)+'-'+i, 'visible');
			getFasceEtaWithCradle(elm.id.replace('cameraBambini-'+r,'bambiniCullaLabel-'+r), elm.id.replace('cameraBambini-'+r,'cameraEtaBambiniCulla-'+r)+'-'+i, getElement(elm.id+'-'+i).options[getElement(elm.id+'-'+i).selectedIndex].value, r)
		}else{
			setVisibile(elm.id.replace('cameraBambini-'+r,'cameraEtaBambini-'+r)+'-'+i, 'hidden');
			setVisibile(elm.id.replace('cameraBambini-'+r,'cameraEtaBambiniCulla-'+r)+'-'+i, 'hidden');
		}
	}
}

function setCullaRowVisibility(contId, r) {
	setVisibility(contId, 'none');
	for (var i = 1; i <= maxBambini ; i++)
	{
		if (getStyleProperties(contId.replace('bambiniCullaLabel-'+r, 'cameraEtaBambiniCulla-'+r)+'-'+i, 'visibility') == 'visible')
		{
			setVisibility(contId, '');
			return;
		}
	}
}

function getFasceEtaWithCradle(contId, elmId, eta, r) {
	if (parseInt(eta) < maxCradleAge)
	{
		setVisibile(elmId, 'visible');
	}else{
		setVisibile(elmId, 'hidden');
		getElement(elmId.replace('Eta', '')).checked = false;
	}
	setCullaRowVisibility(contId, r);
}

function FormSubmit(form_name, form_target, form_action) {
	//var thisForm = eval('document.'+form_name);
	if (form_name != '' && form_name != null && form_name != 'undefined') {
		var thisForm = getElement(form_name);
	}else{
		var thisForm = document.forms[0];
	}
	/*if (window.opener)
		var thisForm = eval('window.opener.document.'+form_name);*/
	if (form_target != '')
		thisForm.target = form_target;
	if (form_action != '')
		thisForm.action = form_action;
	//alert(thisForm.action);
	if (thisForm.onsubmit) {
		if (thisForm.onsubmit()) {
			//alert('pippo');
			thisForm.submit();
		}
	}else{
		thisForm.submit();
	}
}

function setChange(id, val) {
	if (getElement(id))
		getElement(id).value = val;
}

function setVisibile(el, style) {
	var elm = getElement(el);	
	if (elm) {			
		if (allSupport || getElementBy && domImage) {		
			elm.style.visibility = style;
		}else if(domImage) {
			elm.visibility = style;
		}	
	}
}

function setVisibility(el, style) {
	// Hide or show to tip	
	var elm = getElement(el);	
	// alert(elm.name);	
	if (elm) {			
		if (allSupport || getElementBy && domImage) {		
			elm.style.display = style;
		}else if(domImage) {
			elm.display = style;
		}	
	}
}

function checkVisibility(elm_id) {
	var elm = getElement(elm_id);
	if (allSupport || getElementBy && domImage) {
		if (elm.style.display == 'none') {
			return false;
		}else{
			return true;
		}
	}else if(domImage) {
		if (elm.display == 'none') {
			return false;
		}else{
			return true;
		}
	}
}

function optionsReset(elm) {
    elm.options = new Array();
}

function LZ(x) {return(x<0||x>9?"":"0")+x}
function isDate(val,format) {
	var date=getDateFromFormat(val,format);
	if (date==0) { return false; }
	return true;
	}
function compareDates(date1,dateformat1,date2,dateformat2) {
	var d1=getDateFromFormat(date1,dateformat1);
	var d2=getDateFromFormat(date2,dateformat2);
	if (d1==0 || d2==0) {
		return -1;
		}
	else if (d1 >= d2) {
		return 1;
		}
	return 0;
	}
function compareDates3(date1,dateformat1,date2,dateformat2) {
	var d1=getDateFromFormat(date1,dateformat1);
	var d2=getDateFromFormat(date2,dateformat2);
	if (d1==0 || d2==0) {
		return -1;
		}
	else if (d1 > d2) {
		return 1;
		}
	return 0;
	}
function formatDate(date,format) {
	format=format+"";
	var result="";
	var i_format=0;
	var c="";
	var token="";
	var y=date.getYear()+"";
	var M=date.getMonth()+1;
	var d=date.getDate();
	var E=date.getDay();
	var H=date.getHours();
	var m=date.getMinutes();
	var s=date.getSeconds();
	var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
	var value=new Object();
	if (y.length < 4) {y=""+(y-0+1900);}
	value["y"]=""+y;
	value["yyyy"]=y;
	value["yy"]=y.substring(2,4);
	value["M"]=M;
	value["MM"]=LZ(M);
	value["MMM"]=ANKRES_MONTH_NAMES[M-1];
	value["NNN"]=ANKRES_MONTH_NAMES[M+11];
	value["d"]=d;
	value["dd"]=LZ(d);
	value["E"]=ANKRES_DAY_NAMES[E+7];
	value["EE"]=ANKRES_DAY_NAMES[E];
	value["H"]=H;
	value["HH"]=LZ(H);
	if (H==0){value["h"]=12;}
	else if (H>12){value["h"]=H-12;}
	else {value["h"]=H;}
	value["hh"]=LZ(value["h"]);
	if (H>11){value["K"]=H-12;} else {value["K"]=H;}
	value["k"]=H+1;
	value["KK"]=LZ(value["K"]);
	value["kk"]=LZ(value["k"]);
	if (H > 11) { value["a"]="PM"; }
	else { value["a"]="AM"; }
	value["m"]=m;
	value["mm"]=LZ(m);
	value["s"]=s;
	value["ss"]=LZ(s);
	while (i_format < format.length) {
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		if (value[token] != null) { result=result + value[token]; }
		else { result=result + token; }
		}
	return result;
	}
function _isInteger(val) {
	var digits="1234567890";
	for (var i=0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i))==-1) { return false; }
		}
	return true;
	}
function _getInt(str,i,minlength,maxlength) {
	for (var x=maxlength; x>=minlength; x--) {
		var token=str.substring(i,i+x);
		if (token.length < minlength) { return null; }
		if (_isInteger(token)) { return token; }
		}
	return null;
	}
function getDateFromFormat(val,format) {
	val=val+"";
	format=format+"";
	var i_val=0;
	var i_format=0;
	var c="";
	var token="";
	var token2="";
	var x,y;
	var now=new Date();
	var year=now.getYear();
	var month=now.getMonth()+1;
	var date=1;
	var hh=now.getHours();
	var mm=now.getMinutes();
	var ss=now.getSeconds();
	var ampm="";
	while (i_format < format.length) {
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		if (token=="yyyy" || token=="yy" || token=="y") {
			if (token=="yyyy") { x=4;y=4; }
			if (token=="yy")   { x=2;y=2; }
			if (token=="y")    { x=2;y=4; }
			year=_getInt(val,i_val,x,y);
			if (year==null) { return 0; }
			i_val += year.length;
			if (year.length==2) {
				if (year > 70) { year=1900+(year-0); }
				else { year=2000+(year-0); }
				}
			}
		else if (token=="MMM"||token=="NNN"){
			month=0;
			for (var i=0; i<ANKRES_MONTH_NAMES.length; i++) {
				var month_name=ANKRES_MONTH_NAMES[i];
				if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
					if (token=="MMM"||(token=="NNN"&&i>11)) {
						month=i+1;
						if (month>12) { month -= 12; }
						i_val += month_name.length;
						break;
						}
					}
				}
			if ((month < 1)||(month>12)){return 0;}
			}
		else if (token=="EE"||token=="E"){
			for (var i=0; i<ANKRES_DAY_NAMES.length; i++) {
				var day_name=ANKRES_DAY_NAMES[i];
				if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) {
					i_val += day_name.length;
					break;
					}
				}
			}
		else if (token=="MM"||token=="M") {
			month=_getInt(val,i_val,token.length,2);
			if(month==null||(month<1)||(month>12)){return 0;}
			i_val+=month.length;}
		else if (token=="dd"||token=="d") {
			date=_getInt(val,i_val,token.length,2);
			if(date==null||(date<1)||(date>31)){return 0;}
			i_val+=date.length;}
		else if (token=="hh"||token=="h") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>12)){return 0;}
			i_val+=hh.length;}
		else if (token=="HH"||token=="H") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>23)){return 0;}
			i_val+=hh.length;}
		else if (token=="KK"||token=="K") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>11)){return 0;}
			i_val+=hh.length;}
		else if (token=="kk"||token=="k") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>24)){return 0;}
			i_val+=hh.length;hh--;}
		else if (token=="mm"||token=="m") {
			mm=_getInt(val,i_val,token.length,2);
			if(mm==null||(mm<0)||(mm>59)){return 0;}
			i_val+=mm.length;}
		else if (token=="ss"||token=="s") {
			ss=_getInt(val,i_val,token.length,2);
			if(ss==null||(ss<0)||(ss>59)){return 0;}
			i_val+=ss.length;}
		else if (token=="a") {
			if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";}
			else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";}
			else {return 0;}
			i_val+=2;}
		else {
			if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
			else {i_val+=token.length;}
			}
		}
	if (i_val != val.length) { return 0; }
	if (month==2) {
		if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) {
			if (date > 29){ return 0; }
			}
		else { if (date > 28) { return 0; } }
		}
	if ((month==4)||(month==6)||(month==9)||(month==11)) {
		if (date > 30) { return 0; }
		}
	if (hh<12 && ampm=="PM") { hh=hh-0+12; }
	else if (hh>11 && ampm=="AM") { hh-=12; }
	var newdate=new Date(year,month-1,date,hh,mm,ss);
	return newdate.getTime();
	}
function parseDate(val) {
	var preferEuro=(arguments.length==2)?arguments[1]:false;
	generalFormats=new Array('y-M-d','MMM d, y','d MMM y','MMM d,y','y-MMM-d','d-MMM-y','MMM d','y, MMM d');
	monthFirst=new Array('M/d/y','M-d-y','M.d.y','MMM-d','M/d','M-d');
	dateFirst =new Array('d/M/y','d-M-y','d.M.y','d-MMM','d/M','d-M');
	var checkList=new Array('generalFormats',preferEuro?'dateFirst':'monthFirst',preferEuro?'monthFirst':'dateFirst');
	var d=null;
	for (var i=0; i<checkList.length; i++) {
		var l=window[checkList[i]];
		for (var j=0; j<l.length; j++) {
			d=getDateFromFormat(val,l[j]);
			if (d!=0) { return new Date(d); }
			}
		}
	return null;
	}

function checkThisDate(prefix, hidden, linkedhidden, format, day) {
    var selectMonth = getElement(prefix+"month_"+hidden);
    var selectDay = getElement(prefix+"day_"+hidden);
    var selectYear = getElement(prefix+"year_"+hidden);
    month = (selectMonth.options[selectMonth.selectedIndex].value*1);
	//alert(month);
    var date_value = selectYear.options[selectYear.selectedIndex].value + '-' + month + '-' + selectDay.options[selectDay.selectedIndex].value;
    var date_text =  selectDay.options[selectDay.selectedIndex].text + '-' + selectMonth.options[selectMonth.selectedIndex].text + '-' + selectYear.options[selectYear.selectedIndex].text;
	if (isDate(date_value, format)) {
        // inserire controllo su data anteriore
        var oggi = new Date();
        var date2 = new Date(getDateFromFormat(date_value, format));
        
        if (date2.getTime() > (oggi.getTime() - oneDayTime*startTime)) {
            var date3 = new Date(date2.getTime()+(day*oneDayTime));
            
            //if (hidden != '' && hidden != null) setThisDate(date2, hidden, '%Y-%m-%d');
            if (linkedhidden != '' && linkedhidden != null) {
                //setThisDate(date3, linkedhidden, '%Y-%m-%d');
                updateSelectFromDate(date3, prefix, linkedhidden);
            }
            //hi.value = date3.print(hiformat);
        }else{
            alert(mess_date_ant.replace("#date_text#", date_text));
            selectMonth.focus();
        }
	}else if (
				selectYear.options[selectYear.selectedIndex].value != "" &&
				selectDay.options[selectDay.selectedIndex].value != "" &&
				selectMonth.options[selectMonth.selectedIndex].value != ""
		) {
		alert(mess_date_invalid.replace("#date_text#", date_text));
		selectMonth.focus();
	}

}

function updateSelectFromDate(date, prefix, hidden) {
		//var date = cal.date;
		var selectMonth = getElement(prefix+"month_"+hidden);
		selectMonth.selectedIndex = date.getMonth()+1;
		var selectYear = getElement(prefix+"year_"+hidden);
		
        for (var i = 0; i < selectYear.length; i++) {
            if (selectYear.options[i].text == date.getFullYear()) {
                selectYear.options[i].selected = true;
            }
        }
        
        setDaysOptions(prefix+"month_"+hidden, prefix+"day_"+hidden, prefix+"year_"+hidden);
        
        var selectDay = getElement(prefix+"day_"+hidden);
		selectDay.selectedIndex = (date.getDate());
}

function setDaysOptions(monthId, dayId, yearId) {
    var dayElm = getElement(dayId);
    var monthElm = getElement(monthId);
    var yearElm = getElement(yearId);
    var len = ANKRES_MONTH_LENGTH[monthElm.selectedIndex-1];
    // controllo se l'anno è bisestile [opzionale]
    if ((yearElm.options[yearElm.selectedIndex].value % 4) == 0 && len == 28) {
        len = 29;
    }
    //alert(len);
    //setNumericOptions(dayElm, 1, len);
	initNumericSelect(dayElm, day_opt_label, 1, len, "");
}
function setNumericOptions(elm, start, end) {
    elm.length = end;
    
    for (var i=0; i < elm.length; i++) {
        val = (start+i);
        eval("var option"+i+" = new Option(\""+val+"\", \""+val+"\")");
        eval("elm.options[i]=option"+i);
        if (i==0) {
            elm.options[i].selected=true
        }
    }
}


function updateDaySelect( me ) {
	// 1-2 testing
	if(!ANKRES_DAY_NAMES_ABBR) {return;}
	var ci_d = getElement('be_select_day_start');
	var co_d = getElement('be_select_day_end');
	var ci_my = getElement('be_select_month_year_start').value.split("-");
	var co_my = getElement('be_select_month_year_end').value.split("-");
	var ci_sel = ci_d.selectedIndex;
	var co_sel = co_d.selectedIndex;
	var monthDays = [], opt;
	// Checkin month
    //alert(ci_my[1]+" "+parseInt(ci_my[1]*1));
	monthDays = buildDaysForMonth(ci_my[0], parseInt(ci_my[1]*1));
	ci_d.innerHTML = '';
	for(var i = 0; i < monthDays.length; i++) {
		opt = document.createElement('option');
		opt.innerHTML = (monthDays[i] + ' ' + (i+1));
		opt.value = (i+1);
		ci_d.appendChild(opt);
	}
	ci_d.options[ci_sel].defaultSelected = ci_d.options[ci_sel].selected = true;
	// Checkout month
	monthDays = buildDaysForMonth(co_my[0], parseInt(co_my[1]*1));
	co_d.innerHTML = '';
	for(var i = 0; i < monthDays.length; i++) {
		opt = document.createElement('option');
		opt.innerHTML = (monthDays[i] + ' ' + (i+1));
		opt.value = (i+1);
		co_d.appendChild(opt);
	}
	co_d.options[co_sel].defaultSelected = co_d.options[co_sel].selected = true;
}
function updateDayThreeSelect( me ) {
	// 1-2 testing
	if(!ANKRES_DAY_NAMES_ABBR) {return;}
	var ci_d = getElement('be_select_day_start');
	var co_d = getElement('be_select_day_end');
	var ci_m = getElement('be_select_month_start').value;
	var ci_y = getElement('be_select_year_start').value;
	var co_m = getElement('be_select_month_end').value;
	var co_y = getElement('be_select_year_end').value;
	var ci_sel = ci_d.selectedIndex;
	var co_sel = co_d.selectedIndex;
	var monthDays = [], opt;
	// Checkin month
    //alert(ci_my[1]+" "+parseInt(ci_my[1]*1));
	monthDays = buildDaysForMonth(parseInt(ci_y), parseInt(ci_m*1));
	ci_d.innerHTML = '';
	for(var i = 0; i < monthDays.length; i++) {
		opt = document.createElement('option');
		opt.innerHTML = (monthDays[i] + ' ' + (i+1));
		opt.value = ((i < 9) ? "0" : "")+(i+1);
		ci_d.appendChild(opt);
	}
    if (ci_d.options[ci_sel] != undefined) { //if (ci_d.options.length > ci_sel) {
        ci_d.options[ci_sel].defaultSelected = ci_d.options[ci_sel].selected = true;
    }
    //alert("pippo");
	// Checkout month
	monthDays = buildDaysForMonth(parseInt(co_y), parseInt(co_m*1));
	co_d.innerHTML = '';
	for(var i = 0; i < monthDays.length; i++) {
		opt = document.createElement('option');
		opt.innerHTML = (monthDays[i] + ' ' + (i+1));
		opt.value = ((i < 9) ? "0" : "")+(i+1);
		co_d.appendChild(opt);
	}
    if (co_d.options[co_sel] != undefined) { //if (co_d.options.length > co_sel) {
        co_d.options[co_sel].defaultSelected = true;
        co_d.options[co_sel].selected = true;
    }
    
    checkThreeDateOrder(me);
}
function checkThreeDateOrder(me) {
	if (document.getElementById) {
        var ci_d = getElement('be_select_day_start');
        var co_d = getElement('be_select_day_end');
        var ci_m = getElement('be_select_month_start');
        var ci_y = getElement('be_select_year_start');
        var co_m = getElement('be_select_month_end');
        var co_y = getElement('be_select_year_end');
        //alert(parseInt(ci_m.options[ci_m.selectedIndex].value*1)-1);
	    var ci = new Date (parseInt(ci_y.options[ci_y.selectedIndex].value), parseInt(ci_m.options[ci_m.selectedIndex].value*1)-1, parseInt(ci_d.options[ci_d.selectedIndex].value), 12, 0, 0, 0);
	    var co = new Date (parseInt(co_y.options[co_y.selectedIndex].value), parseInt(co_m.options[co_m.selectedIndex].value*1)-1, parseInt(co_d.options[co_d.selectedIndex].value), 12, 0, 0, 0);
	    if (ci >= co){
    	    co.setTime(ci.getTime() + 1000 * 60 * 60 * 24);
	        //co_d.value =  co.getDate();
            for (var i=0; i < co_d.options.length; i++) {
				if (co_d.options[i].value == co.getDate()) {
					co_d.options[i].selected = true;
				}
			}
    	    var com =((co.getMonth()<9) ? "0" : "")+(co.getMonth()+1);
            co_m.value =  com;
            for (var i=0; i < co_m.options.length; i++) {
				if (co_m.options[i].value == com) {
					co_m.options[i].selected = true;
				}
			}
			for (var i=0; i < co_y.options.length; i++) {
				if (co_y.options[i].value == co.getFullYear()) {
					co_y.options[i].selected = true;
				}
			}
		}
	}
}
function updateDaySelectSingle( me, mep ) {
	// 1-2 testing
	if(!ANKRES_DAY_NAMES_ABBR) {return;}
	var ci_d = me;
	var ci_my = mep.value.split("-");
	var ci_sel = ci_d.selectedIndex;
	var monthDays = [], opt;
	// Checkin month
    //alert(ci_my[1]+" "+parseInt(ci_my[1]*1));
	monthDays = buildDaysForMonth(ci_my[0], parseInt(ci_my[1]*1));
	ci_d.innerHTML = '';
	for(var i = 0; i < monthDays.length; i++) {
		opt = document.createElement('option');
		opt.innerHTML = (monthDays[i] + ' ' + (i+1));
		opt.value = (i+1);
		ci_d.appendChild(opt);
	}
	ci_d.options[ci_sel].defaultSelected = ci_d.options[ci_sel].selected = true;
}
function buildDaysForMonth( year, month ) {
	// Month index starts on 0(-11) in Date()-object
	var monthDate = new Date(year, month-1, 1);
	var orgMonth = monthDate.getMonth();
	var dayArray = [], weekDay;
	while(monthDate.getMonth() == orgMonth) {
		// Week starts on Sunday in Date()-object
        //alert( month +" : "+monthDate.getDay()+" : "+monthDate);
		weekDay = (monthDate.getDay() == 0) ? 6 : (monthDate.getDay()-1);
		dayArray.push(ANKRES_DAY_NAMES_ABBR[weekDay]);
		monthDate.setDate(monthDate.getDate()+1);
	}
    //alert(monthDate.getMonth()+" - "+orgMonth);
	return dayArray;
}
function checkDateOrder(me, ci_day, ci_month_year, co_day, co_month_year) {
	if (document.getElementById) {
		var my = getElement(ci_month_year).value.split("-"); //frm[ci_month_year].value.split("-");
	    var ci = new Date (my[0], my[1]-1, getElement(ci_day).value, 12, 0, 0, 0);
	    my = getElement(co_month_year).value.split("-"); //frm[co_month_year].value.split("-");
	    var co = new Date (my[0], my[1]-1, getElement(co_day).value, 12, 0, 0, 0);
	    if (ci >= co){
    	    co.setTime(ci.getTime() + 1000 * 60 * 60 * 24);
	        getElement(co_day).value =  co.getDate();
    	    var com =((co.getMonth()<9) ? "0" : "")+(co.getMonth()+1);
			var so = getElement(co_month_year); //frm[co_month_year];
			for (var i=0; i < so.options.length; i++) {
				if (so.options[i].value == co.getFullYear() + "-" + com) {
					so.options[i].selected = true;
				}
			}
		}
	}
}

