/**********************************************************************/
/* STEP 1 functions
/**********************************************************************/

function SLayer(){
	document.getElementById("SLayer").style.display = "block";
}


function _ChangeDestination(d, i){
	if(d==1){
		document.frm["startpt"+i].value = "";
	}else if(d==2){
		document.frm["endpt"+i].value = "";
	}
}


function _ChangeDestination2(d, i){
	if(d==1){
		document.frm2["startpt"+i].value = "";
	}else if(d==2){
		document.frm2["endpt"+i].value = "";
	}
}


function SetNextDest(i){
	if(i>1){
		if(document.frm["to_name"+(i-1)].value!="" && document.frm["from_name"+i].value==""){
			document.frm["startpt"+i].value = document.frm["endpt"+(i-1)].value;
			document.frm["from_name"+i].value = document.frm["to_name"+(i-1)].value;
			document.frm["from_name"+i].focus();
			document.frm["from_name"+i].select();
		}
	}
}

function _ChangePref(d){
	document.frm["prefcode_" + d].value = "";
}

function SetCode(d, city, cid, i){
	var __go = 0;
	if(d==1){
		document.frm["from_name"+i].value = city;
		document.frm["startpt"+i].value = cid;
		
		var tbl = document.getElementById("_from");
		tbl.style.display = "none";
		__go = 1;
	}else if(d==2){
		document.frm["to_name"+i].value = city;
		document.frm["endpt"+i].value = cid;
		
		var tbl = document.getElementById("_to");
		tbl.style.display = "none";
		__go = 1;
	}
	
	if( __go == 1 ){
		document.getElementById("container").style.display = "none";
		document.getElementById("SLayer").style.display = "block";
		document.frm.submit();
	}
	
}

function SetCode2(d, city, cid, i){
	var __go = 0;
	if(d==1){
		document.frm2["from_name"+i].value = city;
		document.frm2["startpt"+i].value = cid;
		
		var tbl = document.getElementById("_from");
		tbl.style.display = "none";
		__go = 1;
	}else if(d==2){
		document.frm2["to_name"+i].value = city;
		document.frm2["endpt"+i].value = cid;
		
		var tbl = document.getElementById("_to");
		tbl.style.display = "none";
		__go = 1;
	}
	
	if( __go == 1 ){
		document.getElementById("container").style.display = "none";
		document.getElementById("SLayer").style.display = "block";
		document.frm2.submit();
	}
}


function SetAirline(i, name, code){
	document.frm["prefav_"+i].value = name;
	document.frm["prefcode_"+i].value = code;
	
	var tbl = document.getElementById("_airline");
	tbl.style.display = "none";
}

function _ShowOptions(){
	var opt = document.frm.options.value;
	var tbl = document.getElementById("_options");
	if(opt==0){
		document.frm.options.value = 1;
		tbl.style.display = "block";
	}else{
		document.frm.options.value = 0;
		tbl.style.display = "none";
	}
}

function CreateOption(frm,list,text,value){
	var opt = new Option(text, value)
  	eval("var sel = document."+frm+"."+list)
  	sel.options[sel.options.length] = opt
}

function ClearSelect(frm,list){
	eval("var sel = document."+frm+"."+list)
	var items = sel.length-1;
	var i = 0;
	for( i=items; i>=0 ; i-- ){
		sel.remove(i);
	}
}

function update_days( frm, days, my ){
	var date_arr = new Array;
	date_arr[1]=31;
	date_arr[2]=28;
	date_arr[3]=31;
	date_arr[4]=30;
	date_arr[5]=31;
	date_arr[6]=30;
	date_arr[7]=31;
	date_arr[8]=31;
	date_arr[9]=30;
	date_arr[10]=31;
	date_arr[11]=30;
	date_arr[12]=31;	
	
	var temp = my.split("-");
	var month = (temp[1]*1);
	var year  = temp[0];
	var temp = document[frm][days].selectedIndex;
	var selection = date_arr[month];
	
	var ret_val = 0;
	if(selection == 28){
        if (year % 4 != 0 || year % 100 == 0 )
			ret_val=0;
		else
           	if (year % 400 == 0)
				ret_val=1;
			else
               	ret_val=1;
	}
	selection = selection + ret_val;
	ClearSelect(frm,days);
	
	var txt = "";
	var x = 0;
	
	for( x=1; x<=selection; x++ ){
		if(x<10)
			txt = "0"+x;
		else
			txt = x;
		CreateOption(frm,days,txt,x);
	}
	
	if (!document[frm][days].options[temp])
		document[frm][days].options[0].selected=true;
	else
    	document[frm][days].options[temp].selected=true;
}

/**********************************************************************/
/* STEP 2 functions
/**********************************************************************/


/**********************************************************************/
/* GLOBAL functions
/**********************************************************************/
function _SetLoader(link)
{
  document.getElementById('netbookcontainer').style.display='none';
  document.getElementById('searchanim').style.display='block';
  if (link)
  window.location=link;
}

function _UnsetLoader()
{
  document.getElementById('netbookcontainer').style.display='block';
  document.getElementById('searchanim').style.display='none';
}

function StartSearchOnEnter(){
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if(keyCode == 13) StartSearch();
}

function StartSearch(){
	ShowSearchAnim()
	document.frm.submit();
}

function StartSearch2(){
	ShowSearchAnim()
	document.frmSearch.submit();
}

function ShowSearchAnim(){
	var div = document.getElementById("layerSearch");
	div.style.display = "block";
}

function Button(norm_src,sel_src) {
	this.norm=new Image();
	this.sel=new Image();
	this.norm.src=norm_src;
	this.sel.src=sel_src;
}

function button_over(Image_Name) {
	if(document.all){
		document.all[Image_Name].src = eval(Image_Name+".sel.src");
	}else{
		eval("document [\'"+Image_Name+"\'].src = " + Image_Name + ".sel.src");
	}
}

function button_out(Image_Name) {
	if(document.all){
		document.all[Image_Name].src = eval(Image_Name+".norm.src");
	}else{
		eval("document [\'"+Image_Name+"\'].src = " + Image_Name + ".norm.src");
	}
}

function mAction(u){
	ShowSearchAnim();
	document.frm.action = u;
	document.frm.submit();
}

function _Confirm(msg, url){
	mode = confirm(msg);
	if(mode){
		document.location = url;
	}
}


/// *********** POPUPS BEGIN
function _ViewRules(doctype, concat) {
	_w = 500;
	_h = 500;
	if( concat != "" ){
		var _css = 1;
	} else {
		var _css = 0;
	}
	_RuleWin = window.open(concat+'popups.php?tool=4&doctype='+doctype+'&is_frame='+_css, 'viewrule','width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=no,menubar=no,dependent=yes');
	_RuleWin.focus()
}

function _ViewSegment(id, concat) {
	_w = 500;
	_h = 400;
	if( concat != "" ){
		var _css = 1;
	} else {
		var _css = 0;
	}
	_SegWin = window.open(concat+'popups.php?tool=5&segid='+id+'&is_frame='+_css, 'segment','width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=no,menubar=no,dependent=yes');
	_SegWin.focus()
}

function _LowCostMore(airline, concat) {
	_w = 780;
	_h = 600;
	if( concat != "" ){
		var _css = 1;
	} else {
		var _css = 0;
	}
	_LowWin = window.open(concat+'popups.php?tool=3&airline='+airline+'&is_frame='+_css, 'lowcost','width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,menubar=no,dependent=yes');
	_LowWin.focus()
}

function _SetPassengers() {
	_w = 700;
	_h = 450;
	_PaxWin = window.open('passengers.php', 'pax','width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,menubar=no,dependent=yes');
	_PaxWin.focus()
}

function _DateSelect(form, field, defdate, y, concat ) {
	if( concat == 0 ){
		concat = "";
	}
	dateWin=window.open(concat+'date.php?field='+field+'&form='+form+'&defdate='+defdate+'&bd='+y,'datepicker','width=220,height=240,left=' + ((screen.width-220)/2) + ',top=' + ((screen.height-200)/2) + ',toolbar=no,location=no,status=no,scrollbars=no,resizable=no,menubar=no,dependent=yes');
	dateWin.focus()
}

function _DateSelect2(form, field, defdate, y, concat ) {
	if( concat == 0 ){
		concat = "";
	}
	dateWin=window.open(concat+'date2.php?field='+field+'&form='+form+'&defdate='+defdate+'&bd='+y,'datepicker','width=220,height=240,left=' + ((screen.width-220)/2) + ',top=' + ((screen.height-200)/2) + ',toolbar=no,location=no,status=no,scrollbars=no,resizable=no,menubar=no,dependent=yes');
	dateWin.focus()
}

function _Popup(url, _w, _h) {
	_PopWin = window.open(url,'popup','width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,menubar=no,dependent=yes');
	_PopWin.focus()
}

function _Destination(str, form, code, frm, concat) {
	_w = 400;
	_h = 300;
	if( concat != "" ){
		var _css = 1;
	} else {
		var _css = 0;
	}
	_PopWin = window.open(concat+'popups.php?tool=1&find='+str+'&form='+form+'&code='+code+'&frm='+frm+'&is_frame='+_css,'dest','width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,menubar=no,dependent=yes');
	_PopWin.focus()
}


function _Destination2(str, form, code, frm, concat) {
	_w = 400;
	_h = 300;
	if( concat != "" ){
		var _css = 1;
	} else {
		var _css = 0;
	}
	_PopWin = window.open(concat+'popups.php?tool=10&find='+str+'&form='+form+'&code='+code+'&frm='+frm+'&is_frame='+_css,'dest','width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,menubar=no,dependent=yes');
	_PopWin.focus()
}


function _Airlines(str, form, code, concat) {
	_w = 400;
	_h = 300;
	if( concat != "" ){
		var _css = 1;
	} else {
		var _css = 0;
	}
	_PopWin = window.open(concat+'popups.php?tool=2&find='+str+'&form='+form+'&code='+code+'&is_frame='+_css, 'dest','width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,menubar=no,dependent=yes');
	_PopWin.focus()
}

function _ETicket(id, etck, concat) {
	_w = 800;
	_h = 600;
	if( concat != "" ){
		var _css = 1;
	} else {
		var _css = 0;
	}
	_PopWin = window.open(concat+'popups.php?tool=6&id='+id+'&eticket='+etck+'&is_frame='+_css, 'eticket','width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,menubar=no,dependent=yes');
	_PopWin.focus()
}

function _SetSSR(i,tp, concat) {
	_w = 600;
	_h = 500;
	if( concat != "" ){
		var _css = 1;
	} else {
		var _css = 0;
	}
	_SSRWin = window.open(concat+'popups.php?tool=8&id='+i+'&tp='+tp+'&is_frame='+_css, 'ssr','width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,menubar=no,dependent=yes');
	_SSRWin.focus()
}

function _SetBooker(concat) {
	_w = 600;
	_h = 320;
	if( concat != "" ){
		var _css = 1;
	} else {
		var _css = 0;
	}
	_BookerWin = window.open(concat+'popups.php?tool=9&is_frame='+_css, 'booker','width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,menubar=no,dependent=yes');
	_BookerWin.focus()
}


function view_svdoc( _id, concat ){
	_w = 600;
	_h = 320;
	if( concat != "" ){
		concat = "../";
	}
	_svWin = window.open( concat+"viewdocument.php?id="+_id, "", 'width='+_w+',height='+_h+',left=' + ((screen.width-_w)/2) + ',top=' + ((screen.height-_h)/2) + ',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,menubar=no,dependent=yes');
	_svWin.focus();
}


//// ********** END OF POPUPS

function _SetOpenWay(){
	if(document.frm.way[2].checked) {
		document.frm.way[0].checked = true;
		document.frm.date2_ym.disabled = false;
		document.frm.date2_d.disabled = false;
		document.frm.time2.disabled = false;
	}
}

function _SetOpenWayMulticity(){
	if(document.frm.way.checked) {
		document.frm.subtype[1].checked = true;
	}
}

function _SetWay(mode){
	if(mode==0){
		document.frm.date2_ym.disabled = false;
		document.frm.date2_d.disabled = false;
		document.frm.time2.disabled = false;
	}else if(mode==1){
		document.frm.date2_ym.disabled = true;
		document.frm.date2_d.disabled = true;
		document.frm.time2.disabled = true;
	}else if(mode==2){
		document.frm.date2_ym.disabled = true;
		document.frm.date2_d.disabled = true;
		document.frm.time2.disabled = true;
		document.frm.type[1].checked = true;
	}
}

function _DateAdd(dt, days){
	var tmp = dt.split("-");
		
	var _dd = new Date( Date.UTC( parseInt( tmp[0], 10), (parseInt(tmp[1], 10)-1), parseInt(tmp[2], 10) )  );
	var n_days = (1000*3600*24*days);
	var tmp_time = (_dd.getTime()+n_days);
		
	var new_date = new Date( tmp_time );
	var tmp_month = (new_date.getMonth()+1);
	var tmp_year = new_date.getFullYear();
	var tmp_day = new_date.getDate();
		
	if( tmp_month < 10 ){
		var month = "0"+tmp_month;
	} else {
		var month = tmp_month;
	}
		
	if( tmp_day < 10 ) {
		var day = "0"+tmp_day;
	} else {
		var day = tmp_day;
	}
		
	var _pr = tmp_year+"-"+month+"-"+day;
	return _pr;
}

function SetNextWeek(frm, dt1, dt2){
	if(document[frm].way[0].checked){
		var out;
		eval("var _x = document."+frm+"."+dt1+".value;");
		out = _DateAdd(_x, 7);
		tmp = out.split("-");
		key = FindIndexKey(frm, dt2, tmp[0]+"-"+tmp[1])
		
		eval("document."+frm+"."+dt2+".value=out;");
		eval("document."+frm+"."+dt2+"_ym.options["+key+"].selected=true");
		eval("document."+frm+"."+dt2+"_d.value="+(tmp[2]*1));
		
		update_days(frm, dt2+"_d", tmp[0]+"-"+tmp[1]);
	}
}

function FindIndexKey(frm, sel, need){
	var found = -1;
	eval("var obj = document."+frm+"."+sel+"_ym");
	for(i=0;i<=obj.length;i++){
		if(need==obj[i].value){
			found = i;
			break;
		}
	}
	return found;
}

function SetNextWeek2(){
	if(document.frmSearch.way[0].checked){
		var out;
		var _x = document.frmSearch.date1.value;
		out = _DateAdd(_x, 7);
		/*
		var _year = out.getFullYear();
		var _month = out.getMonth();
		if(_month<10) _month = "0" + _month;
		var _day = out.getDate();
		if(_day<10) _day = "0" + _day;
		document.frmSearch.date2.value = _year + "-" + _month + "-" + _day;
		*/
		document.frmSearch.date2.value = out;
	}
}

// Java Script to Handle AutoSearch
function selectKeyDown(){
    // Delete Key resets previous search keys
    if(window.event.keyCode == 46)
        clr();
}

function selectKeyPress(){
    var sndr = window.event.srcElement;
    var pre = this.document.Y_searchform.keys.value;
    var key = window.event.keyCode;
    var ch = String.fromCharCode(key);

    // "i" -> ignoreCase
    var re = new RegExp("^" + pre + ch, "i"); 

    for(var i=0; i<sndr.options.length; i++) {
        if(re.test(sndr.options[i].text)) {
            sndr.options[i].selected=true;
            document.Y_searchform.keys.value += ch;
            window.event.returnValue = false;
            break;
        }
    }
}

function clr(){
    document.Y_searchform.keys.value = "";
}

/***************************************************************************************************************/
/* Date Mask function
/***************************************************************************************************************/
var addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? a.call(o, e) : (o._ = a, o._(e), o._ = null)));
        e = null;
    }
};

var removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};

var MaskInput = function(f, m){ //v1.0
    function mask(e){
        var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[A`-y"]/i, "8": /./ },
            rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
        function accept(c, rule){
            for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1)
                if(r & i && patterns[i].test(c))
                    break;
                return i <= r || c == rule;
        }
        var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
        (!k || k == 8 || k == 37 || k==39 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
            r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
            : (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
            r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
    }
    for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1})
        addEvent(f, i, mask);
};

/***************************************************************************************************************/


