// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function roundNumber(float,dec){
    return Math.round(float*Math.pow(10,dec))/Math.pow(10,dec)
}

function calcTotal() {
    var base_price
    var tax
    var total
    if (document.forms[0].base_price.value.substring(0,1) == "$") {
        base_price = parseFloat(document.forms[0].base_price.value.substring(1));
    } else {
        base_price = parseFloat(document.forms[0].base_price.value);
    }

    if (document.forms[0].tax.value.substring(0,1) == "$") {
        tax = parseFloat(document.forms[0].tax.value.substring(1));
    } else {
        tax = parseFloat(document.forms[0].tax.value);
    }

    total = base_price + tax;

    document.forms[0].price.value = total.toFixed(2);
}

function calcTax() {
    var base_price
    var tax
    if (document.forms[0].base_price.value.substring(0,1) == "$") {
        base_price = parseFloat(document.forms[0].base_price.value.substring(1));
    } else {
        base_price = parseFloat(document.forms[0].base_price.value);
    }

    tax = roundNumber(base_price * 0.06, 2);

    document.forms[0].tax.value = tax.toFixed(2);

    calcTotal();
}

function validate_timeslot(form) {
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}

	if (form.purchased_service_variant_id) {
		reqDate = form.purchased_service_used_on_1i.value + "-" + form.purchased_service_used_on_2i.value + "-" +
			form.purchased_service_used_on_3i.value + " " + form.purchased_service_used_on_4i.value + ":" +
			form.purchased_service_used_on_5i.value;
		getURL = "/providers/" + form.purchased_service_provider_id.value + "/check_timeslot?rt=" + reqDate + "&vid=" + form.purchased_service_variant_id.value;
	}
	if (form.purchased_service_vie_class_instance_id) {
		getURL = "/providers/check_timeslot?vciid=" + form.purchased_service_vie_class_instance_id.value;
	}

	subOk = true
	xmlhttp.open("GET", getURL, false);
	xmlhttp.send(null);
	if (xmlhttp.responseText != "") {
		if (xmlhttp.responseText == "Please choose a provider.") {
			alert(xmlhttp.responseText);
			subOk = false;
		} else {
			subOk = confirm(xmlhttp.responseText);
		}
	}

	return subOk;
}


sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
