var _WEB_DISCOUNT = 7.5;


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 addCountSelectBox(name, id, from, to, onchange, dash) {
		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 getSelectValue(select_id) {
		var selectbox = $$(select_id);
		return selectbox.options[selectbox.selectedIndex].value;
	}

function quickHelp(obj, tag) {
		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);
		$.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 num * (_WEB_DISCOUNT/100); 
}
