/*
 * Copyright (c) Apnasoft Ltd 2008
 *
 */
 
 var use_ajax = false;

function show_form(div_show,div_hide) {
	document.getElementById(div_hide).style.display = "none";
	document.getElementById(div_show).style.display = "block";
}

function initPage() {
    if (use_ajax) {
        if (navigator.userAgent.indexOf("Firefox")!=-1) {
            pagetop = 'pagetop';
        }
        window.location.hash=pagetop;
    }
}

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 showProductPhoto(iurl) {
    var iobj = document.getElementById('curr_img');
    if (iobj) {
        iobj.src = iurl;
    }
}

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 show_booking_options() {
    showHideDiv('book-button', false);
    showHideDiv('course-booking-questions',true);
}

function showHideDiv(divName, boolShow) {
    var divObj = document.getElementById(divName);
    if (divObj) {
        if (boolShow) {
            divObj.style.display = "block";
        }
        else {
            divObj.style.display = "none";
        }
    }
}

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 bookCourse(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_")) {
			    if (opts[o].options[opts[o].selectedIndex].value.indexOf("Booked") > 0) {
			        alert("The date selected is fully booked, please select an alternative date.");
                    return;
			    }
				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;
			}
		}
	}
    // Now get the answers
	var bq1 = document.getElementById("radVoucher1");
	if (bq1) {
	    if (bq1.checked) {
	        vName = document.getElementById("txtVoucherName").value;
            if (vName.length == 0) {
                alert("Please enter the name you wish to appear on the Voucher");
                document.getElementById("txtVoucherName").focus();
                return;
            }
            c_url += "&poption_Voucher_Name="+escape(vName+'|0|0.00');
	    }
    }

    var bq2 = document.getElementById("radDietary1");
	if (bq2) {
	    if (bq2.checked) {
	        vName = document.getElementById("txtDietRequirements").value;
            if (vName.length == 0) {
                alert("Please enter details of your special dietary requirements");
                document.getElementById("txtDietRequirements").focus();
                return;
            }
            c_url += "&poption_Dietary_Requirements="+escape(vName+'|0|0.00');
	    }
    }

    var bq3 = document.getElementById("radFitness2");
	if (bq3) {
	    if (bq3.checked) {
	        vName = document.getElementById("txtFitness").value;
            if (vName.length == 0) {
                alert("Please enter details of your fitness levels");
                document.getElementById("txtFitness").focus();
                return;
            }
            c_url += "&poption_Fitness_Notes="+escape(vName+'|0|0.00');
	    }
    }

    var bq4 = document.getElementById("radMedical1");
	if (bq4) {
	    if (bq4.checked) {
	        vName = document.getElementById("txtMedical").value;
            if (vName.length == 0) {
                alert("Please enter details of your medical conditions");
                document.getElementById("txtMedical").focus();
                return;
            }
            c_url += "&poption_Medical_Conditions="+escape(vName+'|0|0.00');
	    }
    }

    var bq5 = document.getElementById("radDeliveryEmailed");
	if (bq5) {
	    if (bq5.checked) {
            c_url += "&poption_Course_Delivery="+escape('Emailed|0|0.00');
	    }
        else {
            c_url += "&poption_Course_Delivery="+escape('Posted|0|0.00');
        }
    }

	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;
}

// The following used for category display
function show_hide_cat(cat_div) {
    cdiv = document.getElementById(cat_div);
    cimg = document.getElementById('i'+cat_div);
    if ((cdiv) && (cimg)) {
        if ((cdiv.style.display == 'none') || (cdiv.style.display == '')) {
            cdiv.style.display = 'block';
            cimg.src = "images/nav-icon-minus.gif";
        }
        else {
            cdiv.style.display = 'none';
            cimg.src = "images/nav-icon-plus.gif";
        }
    }
}

function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

function expandTARows(textArea){
    var iever = getInternetExplorerVersion();
    if ((iever > -1) && (iever < 7.0)) {
        textArea.style.overflow = 'visible';
        return;
    }

    while (textArea.rows > 1 && textArea.scrollHeight < textArea.offsetHeight) {
        textArea.rows--;
    }

    while (textArea.scrollHeight > textArea.offsetHeight) {
        textArea.rows++;
    }

    return;
}