﻿function $m(theVar){
	return document.getElementById(theVar)
}
function remove(theVar){
	var theParent = theVar.parentNode;
	theParent.removeChild(theVar);
}
function addEvent(obj, evType, fn){
	if(obj.addEventListener)
	    obj.addEventListener(evType, fn, true)
	if(obj.attachEvent)
	    obj.attachEvent("on"+evType, fn)
}
function removeEvent(obj, type, fn){
	if(obj.detachEvent){
		obj.detachEvent('on'+type, fn);
	}else{
		obj.removeEventListener(type, fn, false);
	}
}
function isWebKit(){
	return RegExp(" AppleWebKit/").test(navigator.userAgent);
}
function ajaxUpload(form,url_action,id_element,html_show_loading,html_error_http){
	var detectWebKit = isWebKit();
	form = typeof(form)=="string"?$m(form):form;
	var erro="";
	if(form==null || typeof(form)=="undefined"){
		erro += "The form of 1st parameter does not exists.\n";
	}else if(form.nodeName.toLowerCase()!="form"){
		erro += "The form of 1st parameter its not a form.\n";
	}
	if($m(id_element)==null){
		erro += "The element of 3rd parameter does not exists.\n";
	}
	if(erro.length>0){
		alert("Error in call ajaxUpload:\n" + erro);
		return;
	}
	var iframe = document.createElement("iframe");
	iframe.setAttribute("id","ajax-temp");
	iframe.setAttribute("name","ajax-temp");
	iframe.setAttribute("width","0");
	iframe.setAttribute("height","0");
	iframe.setAttribute("border","0");
	iframe.setAttribute("style","width: 0; height: 0; border: none;");
	form.parentNode.appendChild(iframe);
	window.frames['ajax-temp'].name="ajax-temp";
	var doUpload = function(){
		removeEvent($m('ajax-temp'),"load", doUpload);
		var cross = "javascript: ";
		cross += "window.parent.$m('"+id_element+"').innerHTML = document.body.innerHTML; void(0);";
		$m(id_element).innerHTML = html_error_http;
		$m('ajax-temp').src = cross;
		if(detectWebKit){
        	remove($m('ajax-temp'));
        }else{
        	setTimeout(function(){ remove($m('ajax-temp'))}, 250);
        }
    }
	addEvent($m('ajax-temp'),"load", doUpload);
	form.setAttribute("target","ajax-temp");
	form.setAttribute("action",url_action);
	form.setAttribute("method","post");
	form.setAttribute("enctype","multipart/form-data");
	form.setAttribute("encoding","multipart/form-data");
	form.submit();
	if(html_show_loading.length > 0){
		$m(id_element).innerHTML = html_show_loading;
	}
}

/*these functions are for the ajax assist on the search*/
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchCity() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
	  	document.getElementById("search_City").style.zIndex="100";
		var str = escape(document.getElementById('area').value);
		searchReq.open("GET", 'scripts/searchSuggest.php?what=city&search=' + str, true);
		searchReq.onreadystatechange = handleSearchCity; 
		searchReq.send(null);
	}		
}

function handleSearchCity() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_City')
		ss.innerHTML = '';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}

//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}

//Click function
function setSearch(value) {
	document.getElementById('area').value = value;
	document.getElementById('search_City').innerHTML = '';
}

function searchHotel() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
	  	document.getElementById("search_Hotel").style.zIndex="100";
		var stra = escape(document.getElementById('area').value);
		var str = escape(document.getElementById('hotel').value);
		searchReq.open("GET", 'scripts/searchSuggest.php?what=hotel&city=' + stra + '&search=' + str, true);
		searchReq.onreadystatechange = handleSearchHotel; 
		searchReq.send(null);
	}		
}

function handleSearchHotel() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_Hotel')
		ss.innerHTML = '';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch2(this.innerHTML);" ';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}

//Click function
function setSearch2(value) {
	document.getElementById('hotel').value = value;
	document.getElementById('hotelnamelegend').innerHTML = ' for ' + value;
	document.getElementById('search_Hotel').innerHTML = '';
}

function deleteImage(imageid, commentid, idlocation){
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		searchReq.open("GET", 'scripts/deleteimage.php?image=' + escape(imageid) + '&comment=' + escape(commentid) + '&location' + escape(idlocation), true);
		searchReq.onreadystatechange = handledeleteImage; 
		searchReq.send(null);
	}	
}

function handledeleteImage() {
	if (searchReq.readyState == 4) {
		var str = searchReq.responseText;
		document.getElementById('picholder').innerHTML = str;
	}
}
