/**
 *   set focus on selected form element
 */

function appSetFocus(el_id){
    if(document.getElementById(el_id)){
        document.getElementById(el_id).focus();
    }    
}

/**
 *   Change location (go to another page)
 */
function appGoTo(page, params){
	params = (params != null) ? params : "";
    document.location.href = 'index.php?'+page + params;
}

/**
 *   Change location (go to another page)
 */
function appGoToPage(page, params, method){
	var params_ = (params != null) ? params : "";
	var method_ = (method != null) ? method : "";
	
	if(method_ == "post"){		
		var m_form = document.createElement('form');
			m_form.setAttribute('id', 'frmTemp');
			m_form.setAttribute('action', page);
			m_form.setAttribute('method', 'post');
		document.appendChild(m_form);
		
		params_ = params_.replace("?", "");
		var vars = params_.split("&");
		var pair = "";
		for(var i=0;i<vars.length;i++) { 
			pair = vars[i].split("="); 
			var input = document.createElement('input');
				input.setAttribute('type', 'hidden');
				input.setAttribute('name', pair[0]);
				input.setAttribute('id', pair[0]);
				input.setAttribute('value', unescape(pair[1]));
			document.getElementById('frmTemp').appendChild(input);
		}
		document.getElementById('frmTemp').submit();
	}else{
		document.location.href = page + params_;		
	}
}

/**
 *   Change location (go to current page)
 */
function appGoToCurrent(page, params){
	var page_  = (page != null) ? page : "index.php";
	var params_ = (params != null) ? params : "";
    document.location.href = page_ + params_;
}

/**
 *   Open popup window
 */
function appOpenPopup(page){
	new_window = window.open(page, "blank", "location=1,status=1,scrollbars=1,width=400,height=300");
    new_window.moveTo(100,100);
	if(window.focus) new_window.focus();
}

/**
 *   set cookie
 */
function appSetCookie(name, value, days) {
    if (days){
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = '; expires='+date.toGMTString();
    }
    else var expires = '';
    document.cookie = name+'='+value+expires+'; path=/';
}

/**
 *   get cookie
 */
function appGetCookie(name) {
	if (document.cookie.length > 0){
		start_c = document.cookie.indexOf(name + "=");
		if(start_c != -1){
			start_c += (name.length + 1);
			end_c = document.cookie.indexOf(";", start_c);
			if(end_c == -1) end_c = document.cookie.length;
			return unescape(document.cookie.substring(start_c,end_c));
		}
	}	
	return "";
}

/**
 *   get menu status
 */
function appGetMenuStatus(ind){
	var status = document.getElementById("side_box_content_"+ind).style.display;
	if(status == "none"){			
		return "none";
	}else{
		return "";
	}
}

/**
 *   toggle rss
 */
function appToggleRss(val){
	if(val == 1){
		if(document.getElementById("rss_feed_type")){
			document.getElementById("rss_feed_type").disabled = false;
		}
	}else{
		if(document.getElementById("rss_feed_type")){
			document.getElementById("rss_feed_type").disabled = true;
		}
	}
}

/**
 *   email validation
 */
function appIsEmail(str){
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1) return false; 

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
	if (str.indexOf(at,(lat+1))!=-1) return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
	if (str.indexOf(dot,(lat+2))==-1) return false;
	if (str.indexOf(" ")!=-1) return false;

 	return true;
}

/**
 *  submit logout 
 */
function frmLogout_Submit(){
	if(document.getElementById('frmLogout')){
		document.getElementById('frmLogout').submit();					
	}									
}								

/**
 *  submit site search
 */
function appPerformSearch(page){
	document.forms['frmQuickSearch'].p.value = page;
	document.forms['frmQuickSearch'].submit();
}

/**
 *   toggle element
 */
function appToggleElement(key){
	$("#"+key).toggle("fast");
}

/**
 *  submit form 
 */
function appFormSubmit(frm_name, vars){
	if(document.getElementById(frm_name)){
		if(vars != null){
			vars_parts = vars.split('=');
			for(var i=0; i<vars_parts.length; i+=2) {
				if(document.getElementById(vars_parts[i])) document.getElementById(vars_parts[i]).value = vars_parts[i+1];
			}
		}	
		document.getElementById(frm_name).submit();					
	}									
}								

/**
 *  submit site quick search
 */
function appQuickSearch(){
	var keyword = document.frmQuickSearch.keyword.value;
	if(keyword == '' || keyword.indexOf("...") != -1){
		return false;
	}else{
		document.frmQuickSearch.submit();
		return true;
	}
}

/////////////////////////////////////////////////////////////////////////////

/**
 *  toggle credit card info
 */
function toggleCreditCardInfo(val){
	if(val == "online"){
		$("#checkout-form").attr("action", "index.php?page=booking_checkout");
		$("#ccDetailsPanel").show("fast");	
	}else{
		$("#checkout-form").attr("action", "index.php?page=booking_payment");
		$("#ccDetailsPanel").hide("fast");
	}	
}

/**
 *   toggle elements
 */
function appToggleElements(key1, lnk1, key2, lnk2, key3, lnk3){
	$("#"+key1).show("fast");
	$("#"+lnk1).attr("style", "font-weight:bold");
	if($("#"+key2)){
		$("#"+key2).hide("fast");
		$("#"+lnk2).attr("style", "font-weight:normal");
	}
	if($("#"+key3)){
		$("#"+key3).hide("fast");
		$("#"+lnk3).attr("style", "font-weight:normal");
	}
}

//--------------------------------------------------------------------------
// invoice preview (used for admin and customer)
function Invoice_Preview(){
	var new_window = window.open('html/templates/invoice.tpl.html','blank','location=0,status=0,toolbar=0,height=480,width=680,scrollbars=yes,resizable=1,screenX=100,screenY=100');
	if(window.focus) new_window.focus();
	var message = document.getElementById('divInvoiceContent').innerHTML;
	message = '<div style=\"width:99%;height:24px;margin:0px;padding:4px 5px 0px 0px;background-color:#e1e2e3;text-align:right;\">[ <a href=\"javascript:void(0);\" onclick=\"javascript:window.print();\">Print</a> ]</div>' + message;
	new_window.document.write(message); 
}

