/*
 * Copyright (c) Apnasoft Ltd 2008
 *
 */
 
 var use_ajax = true;

function show_form(div_show,div_hide) {
	document.getElementById(div_hide).style.display = "none";
	document.getElementById(div_show).style.display = "block";
}

function initPage() {
}

function resetQuickNavs() {
	var qnavs = document.getElementsByTagName("select");
	if (qnavs) {
		for(o=0; o<qnavs.length; o++) {
			if (qnavs[o].name.match("qnav_")) {
				qnavs[o].selectedIndex = 0;
			}
		}
	}	
}

function quick_nav() {
	var qnavs = document.getElementsByTagName("select");
	var sel_filter = "";
	if (qnavs) {
		for(o=0; o<qnavs.length; o++) {
			if (qnavs[o].name.match("qnav_")) {
				qval = qnavs[o].options[qnavs[o].selectedIndex].value;
				if (qval.length > 0) {
					if (sel_filter.length > 0) {
						sel_filter += "_" + qval;
					}
					else {
						sel_filter = qval;
					}
				}				
			}			
		}
	}
	
	if (sel_filter.length > 0) {		
		url = "products.php?qnav="+escape(sel_filter);
	}
	else {
		url = "index.php";
	}	
	
	get_page('main_content',url);	
}

// Shopping cart functionality
function addToCart(pid) {
	c_url = "shoppingcart.php?op=add&pid="+pid;
	var opts = document.getElementsByTagName("select");
	if (opts) {
		for(o=0; o<opts.length; o++) {
			if (opts[o].name.match("poption_")) {
				c_url += "&"+opts[o].name+"="+escape(opts[o].options[opts[o].selectedIndex].value);
			}
			if (opts[o].name.match("qty")) {
				c_url += "&qty="+opts[o].options[opts[o].selectedIndex].value;
			}
		}
	}
	if (use_ajax) {
		get_page('main_content',c_url);
	}
	else {
		window.location.href = c_url;
	}
}

function ajax_req(ho) {
    if (ho) {
        show_page(ho.href);
    }
}

function show_page(c_url) {
	if (use_ajax) {
		get_page('main_content',c_url);
	}
	else {
		window.location.href = c_url;
	}
}

function updateCart(numItems, basketTotal) {
	ci = document.getElementById('cart_items');
	if (ci) ci.innerHTML = numItems;
	ct = document.getElementById('cart_total');
	if (ct) ct.innerHTML = basketTotal;
}

function onSearchKeyPress(event) {
    if (event.keyCode==13) search();
}

function onSearchBoxClick() {
    var s = document.getElementById('txtSearch');
    s.value = "";
}

function search() {
    var s = document.getElementById('txtSearch');
    if (s) {
        if (s.value == "") {
            alert("Please enter a search term");
            s.focus();
        }
        else {
            show_page('products.php?kw='+escape(s.value));
        }
    }
}

function isSecure() {
    switch (window.location.protocol)
    {
        case "https:":
            return true
            break;

        default:
            return false;

    }
    return false;
}
