
function SetParam()
{
        return true;
}

function InitParam()
{
        document.afpForm.IMAGE.value=0;
        return true;
}

/* Functions go ret dates */
function QQUpdateDuration() {
        var field1 = document.getElementById("godate");
        var item1 = field1.value;
        var dt1 = item1.split("-");
        var field2 = document.getElementById("retdate");
        var item2 = field2.value;
        var dt2 = item2.split("-");
        var godate = new Date(dt1[0], dt1[1]-1, dt1[2]);
        var retdate = new Date(dt2[0], dt2[1]-1, dt2[2]);
        var duration = Math.round((retdate - godate) / 86400000);
        document.getElementById("duration").value = duration;

	changeDate();
//         return duration;
//          alert(godate);
//          alert(retdate);
//          alert(duration);
};

// 2011.07
function UpdateDuration() {
        var field1 = document.getElementById("godate");
        var item1 = field1.value;
        var dt1 = item1.split("-");
        var field2 = document.getElementById("desiredretdate") || document.getElementById("retdate");
        var item2 = field2.value;
	if(document.getElementById("desiredretdate")){
		document.getElementById("retdate").value = item2;
	}
// 	 alert(item2);

        var dt2 = item2.split("-");
        var godate = new Date(dt1[0], dt1[1]-1, dt1[2]);
        var retdate = new Date(dt2[0], dt2[1]-1, dt2[2]);
        var duration = Math.round((retdate - godate) / 86400000);
        document.getElementById("duration").value = duration;

	changeDate();

};

function AddDurationToReturnDate(o) {
        var field = document.getElementById("godate");
        var item = field.value;
        var dt = item.split("-");

        var retdate = new Date(dt[0], dt[1]-1, dt[2]);
        var duration = parseInt(o.options[o.selectedIndex].value);
        retdate.setDate(retdate.getDate()+duration);

        var y = retdate.getFullYear();
        var m = retdate.getMonth()+1;     // integer, 0..11
        var d = retdate.getDate();

        // alert(retdate);
        document.getElementById("retdate").value = printf("%04d-%02d-%02d", y, m, d);
	if (document.getElementById("retdate").value != printf("%04d-%02d-%02d", y, m, d)){
		document.getElementById('errdutation').style.display = 'block';
	}else {
		document.getElementById('errdutation').style.display = 'none';
		changeDate();
	}
};

function InitReturnDate() {
        var field = document.getElementById("godate");
        var item = field.value;
        var dt = item.split("-");

        var retdate = new Date(dt[0], dt[1]-1, dt[2]);
        var duration = 1;

        retdate.setDate(retdate.getDate()+duration);

        var y = retdate.getFullYear();
        var m = retdate.getMonth()+1;     // integer, 0..11
        var d = retdate.getDate();

        document.getElementById("retdate").value = printf("%04d-%02d-%02d", y, m, d);
};

function UpdateGoDate(calendar, date) {
        if (calendar.dateClicked) {
                var fieldname = this.params.inputField.name;
                var input_field = document.getElementById(fieldname);
                input_field.value = date;

                InitReturnDate();

                UpdateDuration();

                calendar.callCloseHandler();
        }
};

function UpdateReturnDate(calendar, date) {
        if (calendar.dateClicked) {
                var fieldname = this.params.inputField.name;
                var input_field = document.getElementById(fieldname);
                input_field.value = date;

                UpdateDuration();

                calendar.callCloseHandler();
        }
};

/* Function printf(format_string,arguments...)
 * Javascript emulation of the C printf function (modifiers and argument types
 * "p" and "n" are not supported due to language restrictions)
 *
 * Copyright 2003 K&L Productions. All rights reserved
 * http://www.klproductions.com
 *
 * Terms of use: This function can be used free of charge IF this header is not
 * modified and remains with the function code.
 *
 * Legal: Use this code at your own risk. K&L Productions assumes NO resposibility
 * for anything.
 ********************************************************************************/
function printf(fstring)
 { var pad = function(str,ch,len)
 { var ps='';
 for(var i=0; i<Math.abs(len); i++) ps+=ch;
 return len>0?str+ps:ps+str;
 }
 var processFlags = function(flags,width,rs,arg)
 { var pn = function(flags,arg,rs)
 { if(arg>=0)
 { if(flags.indexOf(' ')>=0) rs = ' ' + rs;
 else if(flags.indexOf('+')>=0) rs = '+' + rs;
 }
 else
 rs = '-' + rs;
 return rs;
 }
 var iWidth = parseInt(width,10);
 if(width.charAt(0) == '0')
 { var ec=0;
 if(flags.indexOf(' ')>=0 || flags.indexOf('+')>=0) ec++;
 if(rs.length<(iWidth-ec)) rs = pad(rs,'0',rs.length-(iWidth-ec));
 return pn(flags,arg,rs);
 }
 rs = pn(flags,arg,rs);
 if(rs.length<iWidth)
 { if(flags.indexOf('-')<0) rs = pad(rs,' ',rs.length-iWidth);
 else rs = pad(rs,' ',iWidth - rs.length);
 }
 return rs;
 }
 var converters = new Array();
 converters['c'] = function(flags,width,precision,arg)
 { if(typeof(arg) == 'number') return String.fromCharCode(arg);
 if(typeof(arg) == 'string') return arg.charAt(0);
 return '';
 }
 converters['d'] = function(flags,width,precision,arg)
 { return converters['i'](flags,width,precision,arg);
 }
 converters['u'] = function(flags,width,precision,arg)
 { return converters['i'](flags,width,precision,Math.abs(arg));
 }
 converters['i'] = function(flags,width,precision,arg)
 { var iPrecision=parseInt(precision);
 var rs = ((Math.abs(arg)).toString().split('.'))[0];
 if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
 return processFlags(flags,width,rs,arg);
 }
 converters['E'] = function(flags,width,precision,arg)
 { return (converters['e'](flags,width,precision,arg)).toUpperCase();
 }
 converters['e'] = function(flags,width,precision,arg)
 { iPrecision = parseInt(precision);
 if(isNaN(iPrecision)) iPrecision = 6;
 rs = (Math.abs(arg)).toExponential(iPrecision);
 if(rs.indexOf('.')<0 && flags.indexOf('#')>=0) rs = rs.replace(/^(.*)(e.*)$/,'$1.$2');
 return processFlags(flags,width,rs,arg);
 }
 converters['f'] = function(flags,width,precision,arg)
 { iPrecision = parseInt(precision);
 if(isNaN(iPrecision)) iPrecision = 6;
 rs = (Math.abs(arg)).toFixed(iPrecision);
 if(rs.indexOf('.')<0 && flags.indexOf('#')>=0) rs = rs + '.';
 return processFlags(flags,width,rs,arg);
 }
 converters['G'] = function(flags,width,precision,arg)
 { return (converters['g'](flags,width,precision,arg)).toUpperCase();
 }
 converters['g'] = function(flags,width,precision,arg)
 { iPrecision = parseInt(precision);
 absArg = Math.abs(arg);
 rse = absArg.toExponential();
 rsf = absArg.toFixed(6);
 if(!isNaN(iPrecision))
 { rsep = absArg.toExponential(iPrecision);
 rse = rsep.length < rse.length ? rsep : rse;
 rsfp = absArg.toFixed(iPrecision);
 rsf = rsfp.length < rsf.length ? rsfp : rsf;
 }
 if(rse.indexOf('.')<0 && flags.indexOf('#')>=0) rse = rse.replace(/^(.*)(e.*)$/,'$1.$2');
 if(rsf.indexOf('.')<0 && flags.indexOf('#')>=0) rsf = rsf + '.';
 rs = rse.length<rsf.length ? rse : rsf;
 return processFlags(flags,width,rs,arg);
 }
 converters['o'] = function(flags,width,precision,arg)
 { var iPrecision=parseInt(precision);
 var rs = Math.round(Math.abs(arg)).toString(8);
 if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
 if(flags.indexOf('#')>=0) rs='0'+rs;
 return processFlags(flags,width,rs,arg);
 }
 converters['X'] = function(flags,width,precision,arg)
 { return (converters['x'](flags,width,precision,arg)).toUpperCase();
 }
 converters['x'] = function(flags,width,precision,arg)
 { var iPrecision=parseInt(precision);
 arg = Math.abs(arg);
 var rs = Math.round(arg).toString(16);
 if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
 if(flags.indexOf('#')>=0) rs='0x'+rs;
 return processFlags(flags,width,rs,arg);
 }
 converters['s'] = function(flags,width,precision,arg)
 { var iPrecision=parseInt(precision);
 var rs = arg;
 if(rs.length > iPrecision) rs = rs.substring(0,iPrecision);
 return processFlags(flags,width,rs,0);
 }
 farr = fstring.split('%');
 retstr = farr[0];
 fpRE = /^([-+ #]*)(\d*)\.?(\d*)([cdieEfFgGosuxX])(.*)$/;
 for(var i=1; i<farr.length; i++)
 { fps=fpRE.exec(farr[i]);
 if(!fps) continue;
 if(arguments[i]!=null) retstr+=converters[fps[4]](fps[1],fps[2],fps[3],arguments[i]);
 retstr += fps[5];
 }
 return retstr;
 }
/* Function printf() END */

/* END Functions go ret dates */

function addRoomSelector(package){
        document.getElementById('numrooms_'+package).value = document.getElementById('numrooms_'+package).value * 1 + 1;
        var rooms = document.getElementById('numrooms_'+package).value;
        var tmp = document.getElementById('room_selector_'+package).innerHTML;
        tmp = tmp.replace(/\{rrr\}/gi,rooms);
        var output = tmp.replace(/\{ppp\}/gi,package);
        document.getElementById('add_room_'+package).innerHTML += output;
}

function clearRoomSelector(package){
        document.getElementById('numrooms_'+package).value = 1;
        document.getElementById('add_room_'+package).innerHTML = ' ';
}

function showBirthMask(room){
         var children = document.getElementById('children_'+room).value * 1;
         var output = ' ';
         for (var i=1; i <= children; i++){
                var tmp = document.getElementById('birth_mask').innerHTML;
                var tmp2 = tmp.replace(/\{rrr\}/gi,room);
                output += tmp2.replace(/\{ccc\}/gi,i);
         }
         document.getElementById('birth_mask_sel_children_'+room).innerHTML = '';
         document.getElementById('birth_children_'+room).innerHTML = output;
}

function showAdultsChildrenMask(){
         var numrooms = document.getElementById('numrooms').value * 1;
         var output = ' ';
         for (var i=2; i <= numrooms; i++){
                var tmp = document.getElementById('adults_children_mask').innerHTML;
                output += tmp.replace(/\{rrr\}/gi,i);
         }

         document.getElementById('adults_children_mask_sel').innerHTML = '';
         document.getElementById('adults_children').innerHTML = output;
}

function changeChildrenOptions(adultsselector){
	
	if (document.getElementById('adults_'+adultsselector).value == 3) {
		document.getElementById('ch_'+adultsselector+'_2').disabled = true;
		document.getElementById('ch_'+adultsselector+'_1').disabled = false;
		document.getElementById('children_'+adultsselector).selectedIndex = 0;
		document.getElementById('birth_children_'+adultsselector).innerHTML = '';
		//if (document.getElementById('children_'+adultsselector).value > 1 ){
		//	document.getElementById('children_'+adultsselector).selectedIndex = 1;
		//}
	}else if (document.getElementById('adults_'+adultsselector).value == 4) {
		document.getElementById('ch_'+adultsselector+'_1').disabled = true;
		document.getElementById('ch_'+adultsselector+'_2').disabled = true;
		document.getElementById('children_'+adultsselector).selectedIndex = 0;
		document.getElementById('birth_children_'+adultsselector).innerHTML = '';
	}else if (document.getElementById('adults_'+adultsselector).value == 1) {
		document.getElementById('ch_'+adultsselector+'_1').disabled = false;
		document.getElementById('ch_'+adultsselector+'_2').disabled = false;
	}else if (document.getElementById('adults_'+adultsselector).value == 2) {
		document.getElementById('ch_'+adultsselector+'_1').disabled = false;
		document.getElementById('ch_'+adultsselector+'_2').disabled = false;
	}

}

function printWindow() {
        bV = parseInt(navigator.appVersion);
        if (bV >= 4) window.print();
}

function QQ_Show_Stuff(Click_Menu){
// Function that will swap the display/no display for
// all content within span tags
	if (Click_Menu.style.display == "none"){
		Click_Menu.style.display = "";
	}else{
		Click_Menu.style.display = "none";
	}
}

function Show_Stuff(element){
// Function that will swap the display/no display for
// all content within span tags
	if (document.getElementById(element).style.display == 'none'){
		showdiv(element);
	//	document.getElementById(element).style.display = 'block';
	}else{
		hidediv(element);
	//	document.getElementById(element).style.display = 'none';
	}
}

function hidediv(id) {
        //safe function to hide an element with a specified id
        if (document.getElementById) { // DOM3 = IE5, NS6
                document.getElementById(id).style.display = 'none';
        }
        else {
                if (document.layers) { // Netscape 4
                        document.id.display = 'none';
                }
                else { // IE 4
                        document.all.id.style.display = 'none';
                }
        }
}

function showdiv(id) {
        //safe function to show an element with a specified id
        if (document.getElementById) { // DOM3 = IE5, NS6
                document.getElementById(id).style.display = 'block';
        }
        else {
                if (document.layers) { // Netscape 4
                        document.id.display = 'block';
                }
                else { // IE 4
                        document.all.id.style.display = 'block';
                }
        }
}

// old
function checkPaxAndRooms()
{
   //try {
	var package = document.getElementById('package').value;

	var numrooms = document.getElementById('numrooms_'+package).value * 1;

	var maxpax = 0; 	
	var extrabed = 0;
        for (var i=1; i <= numrooms; i++){
                var roomtype = document.getElementById('roomtype_'+package+'_'+i).value;
		if (roomtype == 'DBL'){
		    maxpax += 2;
		    extrabed += 1;
		}
		if (roomtype == 'SGL') maxpax += 1;
		if (roomtype == 'TPL') maxpax += 3;
		if (roomtype == 'QPL') maxpax += 4;
		if (roomtype == 'MPL') maxpax += 5;

        }

	var adults = document.getElementById('adults').value * 1;
	var children = document.getElementById('children').value * 1;
	var infants = 0;
        var godate = document.getElementById('godate_'+package).value;

	var godate_array=godate.split("-");
	var gd = new Date(godate_array[0],godate_array[1],godate_array[2],00,00,00);
	var one_day=1000*60*60*24;
        for (var c=1; c <= children; c++){
                var birthmm = document.getElementById('birth_mm_child_'+c).value;
                var birthyy = document.getElementById('birth_yy_child_'+c).value;
		//alert(birthyy);
		//alert(birthmm);
		var birthd = new Date(birthyy,birthmm,14,00,00,00);
		var deltadd = Math.ceil((gd.getTime()-birthd.getTime())/(one_day));
		//alert(deltadd);
		if (deltadd < 730) infants++;

        }

	if (children >= infants){
		children -= infants; // if some children are infants
	}

	var totalpax = adults + children;
	if ((children > 0) && (adults > 1) && (extrabed > 0)){
		if (maxpax < totalpax){
			children -= extrabed; // if extra beds in dbl rooms
		}
		if (children < 0){
			children = 0;
		}
	}

	totalpax = adults + children;
	if (maxpax != totalpax){
		//alert('Rooms do not correspond to the number of passengers');
		document.getElementById('paxroomserrormsg').style.display = 'block';
		return false;
	}
        //return false;
   //} catch(err) {alert('error'); return true;}
       return true;

}

// poll submit vote
function submitPollVote(){

        var url = encodeURIComponent('poll.pl');

	var params = $('poll_form').serialize() ;
	document.getElementById('poll').innerHTML = "";
	document.getElementById('poll').innerHTML = '<img align='+'"center"'+' src=/images/calculating.gif>';
	new Ajax.Request(url,
		{
			method: 'post',
			encoding: 'UTF-8',
			parameters: params,
			onSuccess: function(transport) {
				var div = $('poll');
				div.innerHTML = transport.responseText;	
				jQuery(document).ready(function($) {
				$('a[rel*=facebox]').facebox()
				});
				}
			}
		);

	setcookie('poll', 1,1);
}

function setcookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0){ nDays=1;}
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString();
} 

// submit bookingpaxdata form
function submitBookingPaxData(paxid){

	if (document.getElementById('Gender_' + paxid).value=='NA'){
		document.getElementById('Gender_' + paxid).focus();
        	document.getElementById('Gender_' + paxid).style.background = 'Yellow';
		return false;
	}

	if (emailCheck(document.getElementById('Email_' + paxid).value)==false){
		document.getElementById('Email_' + paxid).value="";
		document.getElementById('Email_' + paxid).focus();
        	document.getElementById('Email_' + paxid).style.background = 'Yellow';
		return false;
	}

	var params = $('#form_pax_' + paxid).serialize() ;
	document.getElementById('div_pax_' + paxid).innerHTML = "";
	document.getElementById('div_pax_' + paxid).innerHTML = '<img align='+'"center"'+' src=/images/calculating.gif>';
	$.post("bookingpaxdata.pl", params,
	function(data){
		// alert(data);
		document.getElementById('div_pax_' + paxid).innerHTML = data;
	}, "html");

}

function emailCheck(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;	
}

// sidebar 
function clickSideBar(){
if (document.getElementById('mainproduct').className=='sidebar-at-left'){
document.getElementById('mainproduct').className='use-sidebar sidebar-at-left';
document.getElementById('imagemainproduct').src ='/images/destinos1.gif';
}else{
document.getElementById('mainproduct').className='sidebar-at-left';
document.getElementById('imagemainproduct').src ='/images/destinos2.gif';
}
}
	


