// =======================
// Component IOS Reservations Frontend Javascript
// copyright: (C) 2008-2011 Is Open Source. All rights reserved
// author: Ioannis Sannos
// link: http://www.isopensource.com
// license: Commercial
//=======================

function ressubmitbutton(pressbutton, formname) {
    if (formname == "") { formname = "resForm"; }
	ressubmitform(pressbutton, formname);
}

function ressubmitform(pressbutton, formname) {
    if (formname == "") { formname = "resForm"; }
    var myform = eval("document."+formname);
	myform.task.value=pressbutton;
	try {
		myform.onsubmit();
		}
	catch(e){}
	myform.submit();
}

function resisChecked(isitchecked, formname){
    if (formname == "") { formname = "resForm"; }
    var myform = eval("document."+formname);
	if (isitchecked == true){
		myform.boxchecked.value++;
	}
	else {
		myform.boxchecked.value--;
	}
}

function resBolder(bid, colorOn, colorOff) {
	var tbox = 'compare'+bid;
	var cboxn = 'cb'+bid;
	var cbox = document.getElementById(cboxn);

	if (cbox.checked == true) {
		document.getElementById(tbox).style.color = ''+colorOn+'';
		document.getElementById(tbox).style.fontWeight = 'bold';
	} else {
		document.getElementById(tbox).style.color = ''+colorOff+'';
		document.getElementById(tbox).style.fontWeight = 'normal';
	}
}

function reservToggle(layer) {
	var el = document.getElementById(layer);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	} else {
		el.style.display = '';
	}
}


/* INITIALIZE AJAX CALL */
function resajaxcall() {
    var ro;
    if(window.XMLHttpRequest) { // Non-IE browsers
        ro = new XMLHttpRequest();
    } else if (window.ActiveXObject){ // IE
        ro=new ActiveXObject("Msxml2.XMLHTTP");
        if (!ro) {
            ro=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return ro;
}


/* PUBLISH COMMENT (AJAX) */
function respubcomment(id, livesite) {
	var ajaxbox = 'rescombox'+id;
	var el = document.getElementById(ajaxbox);
	var rhttp = resajaxcall();
    var rnd = Math.random();
    try {
        rhttp.open('POST', livesite+"/index2.php");
        rhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        rhttp.setRequestHeader('charset', 'utf-8');
        rhttp.onreadystatechange = function () {
			if (rhttp.readyState == 4) {
				if (rhttp.status!=200) { alert('Error, please retry'); }
				var nstat = parseInt(rhttp.responseText);
				if (nstat > 0) {
					alert('Comment successfully published!');
					el.innerHTML = '';
					el.style.display = 'none';
				} else {
					alert('Could not publish comment!');
				}
			}
		};
        rhttp.send('option=com_reservations&task=pubcomment&id='+id+'&rnd='+rnd);
    }
    catch(e){}
    finally{}
}


/* DELETE COMMENT (AJAX) */
function resdelcomment(id, livesite) {
	var ulel = document.getElementById('rescomul');
	var ajaxbox = 'rescomline'+id;
	var el = document.getElementById(ajaxbox);
	var rhttp = resajaxcall();
    var rnd = Math.random();
    try {
        rhttp.open('POST', livesite+"/index2.php");
        rhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        rhttp.setRequestHeader('charset', 'utf-8');
        rhttp.onreadystatechange = function () {
			if (rhttp.readyState == 4) {
				if (rhttp.status!=200) { alert('Error, please retry'); }
				var nstat = parseInt(rhttp.responseText);
				if (nstat > 0) {
					alert('Comment deleted successfully!');
					el.innerHTML = '';
					el.style.display = 'none';
					ulel.removeChild(el);
				} else {
					alert('Could not delete comment!');
				}
			}
		};
        rhttp.send('option=com_reservations&task=delcomment&id='+id+'&rnd='+rnd);
    }
    catch(e){}
    finally{}
}


/* LIMIT TEXT IN TEXT BOX */
function resLimitComment(fieldid) {
	var limel = document.getElementById(fieldid);
	if (limel.value.length > 200) {
		limel.value = limel.value.substring(0, 200);
	}
}


/* TOGGLE ROOT-LOCATION VISIBILITY */
function restoggleroot(rootlid) {
  	if (!document.getElementById) { return false; }
	if (!document.getElementById('resrootul'+rootlid)) { return false; }
	var ulroot = document.getElementById('resrootul'+rootlid);
	var liitems = ulroot.getElementsByTagName("li");
	var matcher1 = new RegExp('resli-1-'+rootlid,"i");
	var matcher2 = new RegExp('resli-2-'+rootlid,"i");
	var orato = -1;
	for (var i = 0; i < liitems.length; i++) {
		if (liitems[i].getAttribute('id')) {
			var strid = liitems[i].getAttribute('id');
			if (orato == -1) {
				if (strid.match(matcher1) || strid.match(matcher2)) {
					if (liitems[i].style.display == 'none') {
						liitems[i].style.display = 'block';
						orato = 1;
					} else {
						liitems[i].style.display = 'none';
						orato = 0;
					}
				}
			} else if (orato == 1) {
				liitems[i].style.display = 'block';
			} else {
				liitems[i].style.display = 'none';
			}
		}
	}
}


/* TOGGLE SUB-LOCATION VISIBILITY */
function restogglesub(rootlid, parlid) {
  	if (!document.getElementById) { return false; }
	if (!document.getElementById('resrootul'+rootlid)) { return false; }
	var ulroot = document.getElementById('resrootul'+rootlid);
	var liitems = ulroot.getElementsByTagName('li');
	var matcher = new RegExp('resli-2-'+rootlid+'-'+parlid,"i");
	for (var i = 0; i < liitems.length; i++) {
		if (liitems[i].getAttribute('id')) {
			var strid = liitems[i].getAttribute('id');
			if (strid.match(matcher)) {
				if (liitems[i].style.display == 'none') {
					liitems[i].style.display = 'block';
				} else {
					liitems[i].style.display = 'none';
				}
			}
		}
	}
}


/* SUBMIT SEARCH NAVIGATION FORM */
function resnavsearchpg(pg) {
	var rsfm = document.getElementById('respgsearchfm');
	rsfm.page.value = pg;
	try {
		rsfm.onsubmit();
	}
	catch(e){}
	rsfm.submit();
}


/* LOAD TRIPADVISOR FEED */
function loadtripadvisor(feedurl, livesite) {
	if (feedurl == '') { alert('Invalid tripadvisor feed URL!'); return false; }
	var el = document.getElementById('restripadvbox');
	el.innerHTML = '';
	var rhttp = resajaxcall();
    var rnd = Math.random();
    try {
        rhttp.open('POST', livesite+"/index2.php");
        rhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        rhttp.setRequestHeader('charset', 'utf-8');
		rhttp.onreadystatechange = function () {
			if (rhttp.readyState == 1) {
				el.innerHTML = '<div class="reswarning">Fetching reviews from tripadvisor. Please wait...</div>';
			}
			if (rhttp.readyState == 2) {
				el.innerHTML = '<div class="reswarning">Fetching reviews from tripadvisor. Please wait...</div>';
			}
			if (rhttp.readyState == 3) {
				el.innerHTML = '<div class="reswarning">Fetching reviews from tripadvisor. Please wait...</div>';
			}
			if (rhttp.readyState == 4) {
				if (rhttp.status!=200) {
					el.innerHTML = '<div class="resalert">Could not load tripadvisor reviews</div>';
				} else {
					el.innerHTML = rhttp.responseText;
				}
			}
		};
        rhttp.send('option=com_reservations&task=tripadvisor&feedurl='+feedurl+'&rnd='+rnd);
    }
    catch(e){}
    finally{}
}

/* HIDE TRIPADVISOR REVIEWS */
function hidetripadvisor() {
	if (document.getElementById('restripadvbox')) {
		document.getElementById('restripadvbox').innerHTML = '';
	}
}

/*********************** IOSR v3.5 *******************************/

/* LOAD DESTINATIONS TREE */
function resloadloctree(root, livesite) {
	root = parseInt(root);
	if (isNaN(root)) { return; }
	if (root < 1) { return; }
	var el = document.getElementById('resDestinationsBox');
	var rhttp = resajaxcall();
    var rnd = Math.random();
    try {
        rhttp.open('POST', livesite+"/index2.php");
        rhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        rhttp.setRequestHeader('charset', 'utf-8');
		rhttp.onreadystatechange = function () {
			if (rhttp.readyState == 1) { el.innerHTML = 'Please wait...'; }
			if (rhttp.readyState == 2) { el.innerHTML = 'Please wait...'; }
			if (rhttp.readyState == 3) { el.innerHTML = 'Please wait...'; }
			if (rhttp.readyState == 4) {
				if (rhttp.status!=200) {
					el.innerHTML = '<div class="resalert">Could not load sub-locations</div>';
				} else {
					el.innerHTML = rhttp.responseText;
				}
			}
		};
        rhttp.send('option=com_reservations&task=destree&lid='+root+'&rnd='+rnd);
    }
    catch(e){}
    finally{}
}


/* DISCOUNT DETAILS */
function resdiscountdetails(typ, hid, livesite) {
	typ = parseInt(typ);
	hid = parseInt(hid);
	var el = document.getElementById('resdiscountpolicy'+typ);
	var rhttp = resajaxcall();
    var rnd = Math.random();
    try {
        rhttp.open('POST', livesite+"/index2.php");
        rhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        rhttp.setRequestHeader('charset', 'utf-8');
		rhttp.onreadystatechange = function () {
			el.style.display = '';
			if (rhttp.readyState == 1) { el.innerHTML = 'Please wait...'; }
			if (rhttp.readyState == 2) { el.innerHTML = 'Please wait...'; }
			if (rhttp.readyState == 3) { el.innerHTML = 'Please wait...'; }
			if (rhttp.readyState == 4) {
				if (rhttp.status!=200) {
					el.innerHTML = '<div class="resalert">Could not load details</div>';
				} else {
					el.innerHTML = rhttp.responseText;
				}
			}
		};
        rhttp.send('option=com_reservations&task=discountdetails&typ='+typ+'&hid='+hid+'&rnd='+rnd);
    }
    catch(e){}
    finally{}
}

/* AVAILABILITY CHECK (PUBLIC) */
function resavailcheck(rid, livesite) {
	var rooms = document.getElementById('resrooms'+rid).options[document.getElementById('resrooms'+rid).selectedIndex].value;
	rooms = parseInt(rooms);
	var adults = document.getElementById('resadults'+rid).options[document.getElementById('resadults'+rid).selectedIndex].value;
	adults = parseInt(adults);
	var children = document.getElementById('reschildren'+rid).options[document.getElementById('reschildren'+rid).selectedIndex].value;
	children = parseInt(children);
	if (isNaN(rooms)) {
		var msg = document.getElementById('lng_plselroom').innerHTML;
		alert(msg);
		return false;
	}
	if (rooms < 1) {
		var msg = document.getElementById('lng_plselroom').innerHTML;
		alert(msg);
		return false;
	}
    var aritemid = document.getElementById('avroomItemid').value;
    aritemid = parseInt(aritemid);
	var el = document.getElementById('resavroomcheck'+rid);
	var rhttp = resajaxcall();
    var rnd = Math.random();
    try {
        rhttp.open('POST', livesite+"/index2.php");
        rhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        rhttp.setRequestHeader('charset', 'utf-8');
		rhttp.onreadystatechange = function () {
			el.style.display = '';
			if (rhttp.readyState == 1) { el.innerHTML = 'Please wait...'; }
			if (rhttp.readyState == 2) { el.innerHTML = 'Please wait...'; }
			if (rhttp.readyState == 3) { el.innerHTML = 'Please wait...'; }
			if (rhttp.readyState == 4) {
				if (rhttp.status!=200) {
					el.innerHTML = '<div class="resalert">Could not check availability</div>';
				} else {
					el.innerHTML = rhttp.responseText;
				}
			}
		};
        rhttp.send('option=com_reservations&task=avcheckpub&rid='+rid+'&adults='+adults+'&children='+children+'&rooms='+rooms+'&Itemid='+aritemid+'&rnd='+rnd);
    }
    catch(e){}
    finally{}
}

/* CALCULATE ADDON SERVICES TOTAL AMOUNT */
function rescalcservices(asidsstr, decs) {
	if (asidsstr == '') { return false; }
	var asids = asidsstr.split(',');
	var q = 0;
	var am = 0.00;
	var total = 0.00;
	if (asids instanceof Array) {
		decs = parseInt(decs);
		for (var i in asids) {
			asid = parseInt(asids[i]);
			if (asid > 0) {
				if (document.getElementById('resservices'+asid)) {
					q = document.getElementById('resservices'+asid).options[document.getElementById('resservices'+asid).selectedIndex].value;
					q = parseInt(q);
					if (document.getElementById('userviceprice'+asid)) {
						am = document.getElementById('userviceprice'+asid).value;
						total += parseFloat(am) * q;
					}
				}
			}
		}
		document.getElementById('servicestotal').innerHTML = total.toFixed(decs);
	} else {
		return false;
	}
}

/* LOAD NESTED SUB-LOCATIONS */
function resloadlocations(level, prf) {
	var selbox = document.getElementById(prf+'level'+level+'lid');
	var lid = parseInt(selbox.options[selbox.selectedIndex].value);
	var conbox = document.getElementById(prf+'sublocationsbox');
	if (lid > 0) {
		var nlevel = level + 1;
		document.getElementById(prf+'ajaxlid').value = lid;
		conbox.style.display = 'block';
		for (var i=1; i < 10; i++) {
			var zlevel = level + i;
			if (document.getElementById(prf+'subcon'+zlevel)) {
				var conbox2 = document.getElementById(prf+'subcon'+zlevel);
				var selbox2 = document.getElementById(prf+'level'+zlevel+'lid');
				conbox2.removeChild(selbox2);
				conbox.removeChild(conbox2);
			}
		}
    	var rnd = Math.random();
		var rhttp = resajaxcall();
    	try {
        	rhttp.open('POST', 'index2.php');
        	rhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        	rhttp.setRequestHeader('charset', 'utf-8');
        	rhttp.onreadystatechange = function () {
				if (rhttp.readyState == 4) {
					if (rhttp.status!=200) {
						alert('Error, could not load sub-locations!');
					} else {
						var data = rhttp.responseText;
						if (data != '') {
							var subconbox = document.createElement('DIV');
							subconbox.setAttribute('id', prf+'subcon'+nlevel);
							subconbox.setAttribute('class', 'ressubselect'+nlevel);
							subconbox.innerHTML = data;
							conbox.appendChild(subconbox);
						}
					}
				}
			};
        	rhttp.send('option=com_reservations&task=loadsubselect&lid='+lid+'&level='+level+'&prf='+prf+'&rnd='+rnd);
    	}
		catch(e){}
    	finally{}
	} else {
		var xlevel = level - 1;
		if (xlevel < 0) { xlevel = 0; }
		if (document.getElementById(prf+'level'+xlevel+'lid')) {
			var selbox2 = document.getElementById(prf+'level'+xlevel+'lid');
			lid = parseInt(selbox2.options[selbox2.selectedIndex].value);
			document.getElementById(prf+'ajaxlid').value = lid;
		}

		for (var i=1; i < 10; i++) {
			var zlevel = level + i;
			if (document.getElementById(prf+'subcon'+zlevel)) {
				var conbox3 = document.getElementById(prf+'subcon'+zlevel);
				var selbox3 = document.getElementById(prf+'level'+zlevel+'lid');
				conbox3.removeChild(selbox3);
				conbox.removeChild(conbox3);
			}
		}
	}
}

