/*
 * 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 hide_form(div_hide) {
	var hd = document.getElementById(div_hide);
    if (hd) {
        hd.style.display = "none";
    }
}

function initPage() {
    scroll(0,0);
    var sharepage = document.getElementsByTagName("box-share-this");
    if (sharepage) {
        addthis.toolbox(".addthis_toolbox");
    }
}

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');
    if (s) {
        if (s.value == "type title/author/isbn here") {
            s.value = "";
        }
        else {
            s.focus();
            s.select();
        }
    }
}

function onPasswordKeyPress(event) {
    if (event.keyCode==13) loginCheck(document.getElementById('loginform'));
}

function search() {
    var s = document.getElementById('txtSearch');
    if (s) {
        if (s.value == "") {
            alert("Please enter a search term");
            s.focus();
        }
        else {
            if (typeof webstore_id == "undefined") {
                show_page('products.php?sbk='+escape(s.value));
            }
            else {
                show_page('store.php?sid='+webstore_id+'&sbk='+escape(s.value));
            }
        }
    }
}

//change the text below to reflect your own,
var montharray = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

function countdown(divName, yr ,m ,d) {
    theyear=yr;
    themonth=m;
    theday=d;
    var today = new Date();
    var todayy = today.getYear();
    if (todayy < 1000) todayy += 1900;
    var todaym = today.getMonth();
    var todayd = today.getDate();
    var todayh = today.getHours();
    var todaymin = today.getMinutes();
    var todaysec = today.getSeconds();
    var todaystring = montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec;
    futurestring = montharray[m-1]+" "+d+", "+yr;
    dd = Date.parse(futurestring)-Date.parse(todaystring);
    dday = Math.floor(dd/(60*60*1000*24)*1);
    dhour = Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1);
    dmin = Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
    dsec = Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
    var divObj = document.getElementById(divName);
    if (dday==0 && dhour==0 && dmin==0 && dsec==1) {
        if (divObj) {
            divObj.innerHTML = "Offer Ended";
        }
        return;
    }
    else {
        if (divObj) {
            var op = "";
            if (dhour > 1) {
                op = dhour+" hours, ";
            }
            else {
                if (dhour == 1) {
                    op = dhour+" hour, ";
                }
            }
            divObj.innerHTML = op+dmin+" minutes, and "+dsec+" seconds";
        }
    }

    setTimeout("countdown('"+divName+"',theyear,themonth,theday)",1000);
}