var _WEB_DISCOUNT = 7.5;
//var _WEB_DISCOUNT = 10;


function  dbg(obj) {
	var output = "" ;
	for (var prop in obj) { output +=  "obj." + prop + " = " + obj[prop] + "\n" ; }
	alert(output);
}

function switchOp(url) {
		window.location.href = url;
		return false;
	}

function NewWindow(mypage, myname, w, h, scr) {
			var name = mypage.substr(0, 6);
			if( myname == 'name') myname = name;
			if( myname == 'name') myname = 'name';
		LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
		TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
		settings = 'height=' + h + ',width=' + w + ',top=' + TopPosition + ',left=' + LeftPosition;
		if(scr) settings = settings + ', scrollbars=yes';
		var win = window.open(mypage,myname,settings); win.focus();
		return false;
	}
	var dhtmlwin;
	function NewWindow_dhtml(url, name, width, height) {
	 dhtmlwin=dhtmlwindow.open("editbox", "iframe", url, " ", "width="+width+"px,height="+height+"px,resize=1,scrolling=1,center=1", "recal");
	}
		
function checkAll(name_string) {
		els = document.getElementsByTagName("input");
		for(i = 0; i < els.length; i++) {
			el = els[i];
			if(el.type == "checkbox") {
				if(el.name.indexOf(name_string) > -1) {
					el.checked = !el.checked;
				}
			}
		}
	}

function unCheckAll(name_string) {
		els = document.getElementsByTagName("input");
		for(i = 0; i < els.length; i++) {
			el = els[i];
			if(el.type == "checkbox") {
				if(el.name.indexOf(name_string) > -1) {
					el.checked = false;
				}
			}
		}
	}

	
function getTable(table_id) {
		return document.getElementById(table_id);
	}
	
function clearTable(table) {
		while(table.rows.length > 2) {	// excluding the header
			table.deleteRow(2);
		}
	}

function clearTableEx(table, row_index) {
		while(table.rows.length > row_index) {	// excluding the header
			table.deleteRow(row_index);
		}
	}
	
function addCell(row) {
		return row.insertCell(row.cells.length);
	}

function addCells(row, count) {
		var cells = new Array();
		for(i = 0; i <  count; i++) {
			cells[i] = addCell(row);
		}
		return cells;
	} 	
	
function addRow(tbl, color, row_id) {
			
			var lastRow = tbl.rows.length;
			var row = tbl.insertRow(lastRow);
			row.setAttribute("Bgcolor",  color);
			row.bgColor = color;
			row.id = row_id;
		return row;	  
	}

function addRowAfter(tbl, idx, row_id) {
			
			var row = tbl.insertRow(idx+1);
			row.id = row_id;
		return row;	  
	}
	
function addBoldText(txt) {
		var bold = document.createElement("B");
		bold.innerHTML = txt;
		return bold;
	}

function addText(txt) {
		var bold = document.createElement("span");
		bold.innerHTML = txt;
		return bold;
	}
		
function addPopupLink(txt, url, w, h) {
		var linkNode = document.createElement('A');
		//var txtNode = document.createTextNode(txt);
		linkNode.innerHTML = txt;
		linkNode.href = '#';
		linkNode.onclick = function() { NewWindow(url, 'name', w, h, true); return false; }; 
		//linkNode.appendChild(txtNode);
		return linkNode;
	}

function addLink(txt, url) {
		var linkNode = document.createElement('A');
		linkNode.href = url;
		linkNode.innerHTML=txt;
		return linkNode;
	}
		
function addCheckBox(name, value, checked) {
			  var input = document.createElement('input');
			  input.name = name;
			  input.value = value;
			  input.type = 'checkbox';
		return input;
	}

function addHidden(name, value) {
			  var input = document.createElement('input');
			  input.name = name;
			  input.value = value;
			  input.type = 'HIDDEN';
		return input;
}

function addCountSelectBox(name, id, from, to, onchange, dash, default_value) {
		var select = document.createElement('SELECT');
		select.name = name;
		if(id) select.id = id;
		else select.id = name;
		if(onchange) select.onchange = onchange;

		if(dash) {
		var opt_st = document.createElement('OPTION');
		opt_st.value = "-";
		opt_st.innerHTML = "-";
		select.appendChild(opt_st);
		}
		for(i = from; i<= to; i++) {
			var opt = document.createElement('OPTION');
			opt.value = i;
			opt.innerHTML = i;
			select.appendChild(opt);
		}
		return select;
	}

function getCheckedValue(radioObj) {
		if(!radioObj)
			return "";
		var radioLength = radioObj.length;
		if(radioLength == undefined)
			if(radioObj.checked)
				return radioObj.value;
			else
				return "";
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) {
				return radioObj[i].value;
			}
		}
		return "";
	}

	
function $$(element_id) {
		return document.getElementById(element_id);
	}

function clearSelect(select_id) {
		var selectbox = $$(select_id);
		selectbox.options.length = 0 ;
	}
function addSelectBox(name, id, onchange) {
		var select = document.createElement('SELECT');
		select.name = name;
		if(id) select.id = id;
		else select.id = name;
		if(onchange) select.onchange = onchange;
		return select;
	}


function addSelectOptionEx(select, value, text) {
			var opt = document.createElement('OPTION');
			opt.value = value;
			opt.innerHTML = text;
			select.appendChild(opt);
	}

function addSelectOption(select_id, value, text, selected) {
		var selectbox = $$(select_id);
		var selectedIndex = 0;
		selectbox.options.length++ ;
		selectbox.options[selectbox.options.length-1].value = value;
		selectbox.options[selectbox.options.length-1].innerHTML = text;
		selectbox.options[selectbox.options.length-1].selected = selected;
		if(selected) {
			selectedIndex = selectbox.options.length-1;
			selectbox.selectedIndex = selectedIndex;
		}

	}

function getSelectObjValue(selectbox) {
		return selectbox.options[selectbox.selectedIndex].value;
	}

function getSelectValue(select_id) {
		var selectbox = $$(select_id);
		return selectbox.options[selectbox.selectedIndex].value;
	}

function quickHelp(obj, tag, width) {
	    var w;
		if(width) {
			w = width;
		} else {
			w = 40;
		}
		var div_tag = "quickHelp";
		var div = document.getElementById(div_tag);
		var coords = findPos(obj);
		var x = coords[0];
		var y = coords[1];
		div.style.top = (y - 27) + "px";
		div.style.left = (x + w) + "px";
		showDiv(div_tag);
		$.getJSON("/index.php?op=1000&func=quickHelp&tag="+tag, function(data){
				document.getElementById('qhelp1').innerHTML = data.description;
				document.getElementById('qhelp2').innerHTML = data.title;
		});
	}

function quickHelpMe(obj, displayTitle, displayMessage) {
                var div_tag = "quickHelp";
                var div = document.getElementById(div_tag);
                var coords = findPos(obj);
                var x = coords[0];
                var y = coords[1];
                div.style.top = (y - 27) + "px";
                div.style.left = (x + 40) + "px";
                showDiv(div_tag);
                document.getElementById('qhelp1').innerHTML = displayMessage;
                document.getElementById('qhelp2').innerHTML = displayTitle;
        }

function findPos(obj) {
		var curleft = curtop = 0;
		do {
		curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
	} while (obj = obj.offsetParent);
		return [curleft,curtop];
	}
	
function getSyncJSON( url, data, callback ) {
		// shift arguments if data argument was ommited
		if ( jQuery.isFunction(data )) {
			callback = data;
			data = null;
		}

		return jQuery.ajax({
			type: "GET",
			url: url,
			async: false, 
			data: data,
			success: callback,
			dataType: "json"
		}); 
}		

function setCheckedValue(radioObj, newValue) {
	document.frm[radioObj.name][0].checked = true;
//	document.frm.search_type[0].checked = true;
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function addToFavorites() {
	var url = "/index.php?op=1000&func=addToFavorites";
	$.get(url, function(data){
		alert(data);
	});
}


	function myFavorites(obj) {
		var div_tag = "myFavs";
		var div = document.getElementById(div_tag);
		var coords = findPos(obj);
		var x = coords[0];
		var y = coords[1];
		div.style.top = (y + 20) + "px";
		div.style.left = (x - 120) + "px";
		showDiv(div_tag);
		$.get("/index.php?op=1000&func=loadFavorites", function(data){
				document.getElementById('favData').innerHTML = data;
		});
	}
	
	function deleteFavorite(acc_code) {
		$.get("/index.php?op=1000&func=deleteFavorites&code="+acc_code, function(data){
				document.getElementById('favData').innerHTML = data;
		});
	}
	
	function showFlightProgram(country, edition) {
		NewWindow('/index.php?op=35&country='+country+'&edition='+edition,'', 1000,600, true);
	}

	function quickSearch() {
		var s = document.getElementById('srch').value;
		window.location.href = '/index.php?op=29&srch='+s;
	}
	
	
function getWinSize() {
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
    	myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	 } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	}
  
 	return [myWidth, myHeight];
}

function getScrollOffset() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}


function dimSearch(how) {
	var image = new Array();	
	var size = getWinSize();
	var scrolling = getScrollOffset();
	var image_psearch = new Image(); image_psearch.src = "/gfx/dim_psearch1.gif";
	var image_pay = new Image(); image_pay.src = "/gfx/dim_payment1.gif";
	
	document.getElementById("dimmer_img").width = size[0];
	document.getElementById("dimmer_img").height = size[1] + scrolling[1];
	document.body.style.overflow = "hidden";
	document.getElementById("dimmer").style.visibility = "visible";
	
	if (how == "hs") {
		document.getElementById("dimmer_msg_img").src = image_psearch.src;
	}

	if (how == "fs") {
		document.getElementById("dimmer_msg_img").src = image_fsearch.src;
	}
	
	if (how == "pay") {
		document.getElementById("dimmer_msg_img").src = image_pay.src;
	}


	document.getElementById("dimmer_msg").style.left = size[0]/2 - 160 + "px";
	document.getElementById("dimmer_msg").style.top = size[1]/2 - 200 + scrolling[1] + "px";
	document.getElementById("dimmer_msg").style.visibility = "visible";
	/*
	if (how == "clear") {
		document.getElementById("dimmer_msg_img").src = image_pay.src;
		document.getElementById("dimmer_msg").style.visibility = "none";
	}*/
	
}
	
function chooseProduct(who) {
	
	if (document.getElementById('agent_no').value != "") {
		if (who.options[who.options.selectedIndex].value == "FS10") location.href= "http://www.balkanholidays.co.uk/flight_only/index.html"; 
		if (who.options[who.options.selectedIndex].value != "HS10" && who.options[who.options.selectedIndex].value != "FS10" ) location.href= "http://bulgaria.balkanholidays.co.uk/agents.htm";
	}
	
	if (document.getElementById('agent_no').value == "") {
	switch(who.options[who.options.selectedIndex].value) {
		case "HS10": break;
		case "HS09": location.href = "http://bulgaria.balkanholidays.co.uk/bulgaria_summer_beach/s09/index.htm"; break;
		case "HW09": location.href = "http://bulgaria.balkanholidays.co.uk/bulgaria_winter_ski/w09/index.htm"; break;
		case "AS10": location.href = "http://www.balkanholidays.co.uk/hotel_only/index.html"; break;
		case "AS09": location.href = "http://bulgaria.balkanholidays.co.uk/bulgaria_summer_beach/s09/accommodation_only.htm"; break;
		case "AW09": location.href = "http://bulgaria.balkanholidays.co.uk/bulgaria_winter_ski/w09/accommodation_only.htm"; break;
		case "FS09": location.href = "http://bulgaria.balkanholidays.co.uk/bulgaria_summer_beach/s09/flightonly.htm"; break;
		case "FW09": location.href = "http://bulgaria.balkanholidays.co.uk/bulgaria_winter_ski/w09/flightonly.htm"; break;
		case "FS10": location.href = "http://www.balkanholidays.co.uk/flight_only/index.html"; break;
		}
	}
	
}


// Navigation Menu
function navMenu(who) {
	who.style.cursor = 'hand';
	var slotIndex = parseInt(who.id.replace("slot", ""));
	var slotOn = 1;
	if (who.style.backgroundImage.indexOf("_1.gif") == -1) slotOn = 1; else slotOn = 2;
		who.style.backgroundImage = "url(/gfx/layout/nav_s" + COLOR_SCHEME + "_" + who.id + "_" + slotOn + ".gif)";
	}

function accSearch(how) {
	if (how == 1) { 
		document.getElementById("accSearchOn").style.display = "block";
		document.getElementById("accSearchOff").style.display = "none";
		}
	if (how == 0) { 
		document.getElementById("accSearchOn").style.display = "none";
		document.getElementById("accSearchOff").style.display = "block";
	}
	}

function calculateDiscount(num) {
	//return 0;	
	return num * (_WEB_DISCOUNT/100); 
}

function MyPopUp(who, width, height) {
        var win = window.open(who, "_win", "toolbar=0, height= " + height + ", width= " + width + ",top=350,left=300 scrollbars=no,status=no,location=no,menubar=no,directories=no");
        win.focus();
}

function toCurrency(num) {
	var n = new Number(num);
	return n.toFixed(2);
}

	function togglePricesEx(p1, p2) {
				if(!$("#"+p1).is(":visible")) {
					$("#"+p2).hide();
				} 
			$("#"+p1).toggle("bounce", null, 1000, function(){
				if(!$("#"+p1).is(":visible")) {
					$("#"+p2).show();
				} 
				setTimeout("togglePrices('prices1_page1', 'prices1_page2')", 5000);
				
			});
	}

	function togglePrices(p1, p2) {
			var to_show = "";
			var to_hide = "";
			if(!$("#"+p1).is(":visible")) {
				to_show = "#"+p1;
				to_hide = "#"+p2;
			} 
			if(!$("#"+p2).is(":visible")) {
				to_show = "#"+p2;
				to_hide = "#"+p1;
			} 
			$(to_hide).toggle("slide", null, 500, function(){
				$(to_show).toggle("slide", {direction: "right"}, 500);
				setTimeout("togglePrices('prices1_page1', 'prices1_page2')", 5000);
			});
	}

	var slider_runing = false;
	function switchSeasonPrice(season) {
		$("#prices1").hide();
		$("#prices2").hide();
		if(season == "W") {	$("#prices2").show(); 		}
		if(season == "S") {	
			$("#prices1").show(); 
			/*
			if(!slider_runing) {
				slider_runing = true;
				$("#slider").easySlider({
					auto: true,
					continuous: true,
					controlsShow: false,
					checkElement: 'prices1'
				});
			}
			*/

		}
		return false;
	}
	function switchTab(idx) {
		document.getElementById('home_tabs').className="tab"+idx;
		for(i = 1; i <= 4; i++) {
			if(idx == i) {
				$("#tab_content"+i).show();
				
			} else {
				$("#tab_content"+i).hide();
			}
		}
		return false;
	}

function marque_start(){
	document.write('<marquee style="width:200px; height:100px;" scrollamount="2" direction="up">');
}
function marque_end(){
	document.write('</marquee>');
}

	function fadeSwitch(id1, id2) {
		$('#'+id1).fadeOut(100,function(){
			$('#'+id2).fadeIn(100);
			
		});
	}
	
	function classSwitch(id, cls) {
		
		$('#'+id).removeClass().addClass(cls);
	}

	var keyStr = "ABCDEFGHIJKLMNOP" +
	               "QRSTUVWXYZabcdef" +
	               "ghijklmnopqrstuv" +
	               "wxyz0123456789+/" +
	               "=";
	 
	  function encode64(input) {
	     input = escape(input);
	     var output = "";
	     var chr1, chr2, chr3 = "";
	     var enc1, enc2, enc3, enc4 = "";
	     var i = 0;
	 
	     do {
	        chr1 = input.charCodeAt(i++);
	        chr2 = input.charCodeAt(i++);
	        chr3 = input.charCodeAt(i++);
	 
	        enc1 = chr1 >> 2;
	        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	        enc4 = chr3 & 63;
	 
	        if (isNaN(chr2)) {
	           enc3 = enc4 = 64;
	        } else if (isNaN(chr3)) {
	           enc4 = 64;
	        }
	 
	        output = output +
	           keyStr.charAt(enc1) +
	           keyStr.charAt(enc2) +
	           keyStr.charAt(enc3) +
	           keyStr.charAt(enc4);
	        chr1 = chr2 = chr3 = "";
	        enc1 = enc2 = enc3 = enc4 = "";
	     } while (i < input.length);
	 
	     return output;
	  }
	 
	  function decode64(input) {
	     var output = "";
	     var chr1, chr2, chr3 = "";
	     var enc1, enc2, enc3, enc4 = "";
	     var i = 0;
	 
	     // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	     var base64test = /[^A-Za-z0-9\+\/\=]/g;
	     if (base64test.exec(input)) {
	        alert("There were invalid base64 characters in the input text.\n" +
	              "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
	              "Expect errors in decoding.");
	     }
	     input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	 
	     do {
	        enc1 = keyStr.indexOf(input.charAt(i++));
	        enc2 = keyStr.indexOf(input.charAt(i++));
	        enc3 = keyStr.indexOf(input.charAt(i++));
	        enc4 = keyStr.indexOf(input.charAt(i++));
	 
	        chr1 = (enc1 << 2) | (enc2 >> 4);
	        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	        chr3 = ((enc3 & 3) << 6) | enc4;
	 
	        output = output + String.fromCharCode(chr1);
	 
	        if (enc3 != 64) {
	           output = output + String.fromCharCode(chr2);
	        }
	        if (enc4 != 64) {
	           output = output + String.fromCharCode(chr3);
	        }
	 
	        chr1 = chr2 = chr3 = "";
	        enc1 = enc2 = enc3 = enc4 = "";
	 
	     } while (i < input.length);
	 
	     return unescape(output);
	  }
