var isNav, isIE;
var coll = "";
var styleObj = "";
if (parseInt(navigator.appVersion) >= 4) {
	if (navigator.appName == "Netscape") {
		isNav = true;
	} else {
		isIE = true;
		coll = "all.";
		styleObj = ".style";
	}
}

function ShowMsg(Msg)
{
	if (Msg.length > 0)
		alert(Msg);
}

function Post(field, value)
{
	document.forms[0].action += '&' + field + '=';
	if (value != null)
		document.forms[0].action += value;
	else
		document.forms[0].action += 'Yes';

	if (typeof(Validate) == 'undefined' || Validate())
		document.forms[0].submit();
}

function Get(field, value)
{
	var url = document.forms[0].action += '&' + field + '=';
	if (value != null)
		url += value;
	else
		url += 'Yes';
	document.location = url;
}

function SetOption(name, value)
{
	for (var i=0; i<document.forms[0].length; i++)
	{
		if (document.forms[0].elements[i].name == name)
			var element = document.forms[0].elements[i];
	}
	if (element == null)
		return;

	if (element.type == 'checkbox')
	{
		if (element.value == value)
			element.checked = true;
	}
	else if (element.type == 'radio')
	{
		for (var i=0; i<document.forms[0].length; i++)
		{
			if (document.forms[0].elements[i].name == name &&
				document.forms[0].elements[i].value == value)
				document.forms[0].elements[i].checked = true;
		}
	}
	else if (element.type == 'select-one')
	{
		for (var i=0; i<element.length; i++)
		{
			if (element.options[i].value == value)
				element.selectedIndex = i;
		}
	}
	else if (element.type == 'text')
	{
		element.value = value;
	}
}

/* List View Functions */
function Sort(SortKey)
{
	var F=document.forms[0];
	if (F.Sort.value.indexOf(SortKey) == -1)
		F.Sort.value = SortKey;
	else
	{
		if (F.Sort.value.indexOf('Rev') == -1)
			F.Sort.value = 'Rev' + SortKey;
		else
			F.Sort.value = SortKey;
	}
	F.submit(); 
}

function SelectAll() {
	for (var i=0;i<document.forms[0].elements.length;i++) {
		var e=document.forms[0].elements[i];
		e.checked=!e.checked;
	}
}

function ChangeFolder() {
	var F=document.forms[0];
	F.DestFolder.selectedIndex=F.BottomFolder.selectedIndex;
}

function ChangeBottomFolder() {
	var F=document.forms[0];
	F.BottomFolder.selectedIndex=F.DestFolder.selectedIndex;
}

/* Message View Functions */
function Print(url) {
	window.open(url,'Print','height=400,width=600,scrollbars=yes,status=yes,resizable=1,toolbar=yes,menubar=yes');
}

function Source(url) {
	window.open(url,'Source','height=400,width=600,scrollbars=yes,status=yes,resizable=1');
}

/*!!!*/
function OpenLink(url) {
	window.open(url);
}

function OpenDetails(frm, itm, fk) {
	url="/Run.aspx?F="+frm+"&I="+itm+"&L="+fk+"&V=F&Cmd=P";
	dw=window.open(url, 'Details', 'height=350,width=650,scrollbars=no,resizable=1');
	dw.focus();
}

function CloseDetails() {
	opener.location.reload();
	this.close();
}

function SaveCloseDetails() {
	document.forms[0].submit();
	opener.location.reload();
	this.close();
}

/* Act on in/out/click of fields */
function oN(obj) {
	obj.style.background = "#ffffff";
}

function oF(obj) {
	obj.style.background = "#E0E8F1";
}
function oC(obj) {
//	if (document.all) eval("location.href = " + obj + ".href"); 
//	else location.href = document.links[obj].href;
	if (document.all) eval("parent.Frame_2.location.href = " + obj + ".href"); 
}
function oC2(obj, frm) {
	if (document.all) eval("parent."+frm+".location.href = " + obj + ".href"); 
}

/* Submit login to server */
function Login() {
	//url="http://www.minipoint.com";
	//url="http://www.minipoint.com?"+document.forms[0].elements["UserName"].value;
	//document.forms[0]["UserName"];
	//window.open(url);
	//document.location = url;
	document.forms[0].submit();
}

function GoToForm(frm, itm, viw) {
	url="/Run.aspx?F="+frm+"&I="+itm+"&V="+viw;
	document.location = url;
//	dw=window.open(url, 'Details', 'height=350,width=650,scrollbars=yes,resizable=1');
//	dw.focus();
	document.forms[0].action=url;
	document.forms[0].submit();
}

function PopUpForm(frm, itm, viw, whr) {
	url="/Run.aspx?F="+frm+"&I="+itm+"&V="+viw+"&W="+whr;
	dw=window.open(url, 'Select', 'height=350,width=450,scrollbars=yes,resizable=1');
	dw.focus();
}

function SetFormPos(x, y, w, h) {
	//if (window.screenLeft==0) {
		window.moveTo(x,y);
	//}
	window.resizeTo(w,h);
}

function isblank(s) {
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '')) return false;
	}

	return true;
}

function check_no_licenses() {
	f = document.Purchase;
	if (((f.NoLi_MP_upgrade.value == null) || (f.NoLi_MP_upgrade.value == "") || isblank(f.NoLi_MP_upgrade.value)) && ((f.NoLi_MP_new.value == null) || (f.NoLi_MP_new.value == "") || isblank(f.NoLi_MP_new.value))) res_li = 'Ordered Licenses (new or upgrade)';
	else res_li = '';
	return(res_li);
}

function check_send() {
	f = document.Purchase;
	if ((f.shipment[0].checked) && ((f.E_mail.value == null) || (f.E_mail.value == "") || isblank(f.E_mail.value))) res = 'E-mail Address';
	else if ((f.shipment[1].checked) && ((f.Fax.value == null) || (f.Fax.value == "") || isblank(f.Fax.value))) res = 'Fax Number';
	else res = '';
	res_send = res;
	return(res_send);
}

function verify(f) {
	var msg;
	var empty_fields = "";
	var errors = "";
	for(var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
				empty_fields +="\n" + e.id;
				continue;
			}
			if (e.numeric || (e.min != null) || (e.max != null)) {
				var v = parseFloat(e.value);
				if (isNaN(v) || ((e.min != null) && (v < e.min)) || ((e.max != null) && (v > e.max))) {
					errors += "- The field " + e.id + " must be a number";
					if (e.min != null) errors += " that is greater than " + e.min;
					if (e.max != null && e.min != null) errors += "and less than " + e.max;
					else if (e.max != null) errors += " that is less than " + e.max;
					errors += ".\n";
				}
			}
		}
	}
	no_msg = check_no_licenses();
	if (no_msg) empty_fields += "\n" + no_msg;
	send_msg = check_send();
	if (send_msg) empty_fields += "\n" + send_msg;
	if (!empty_fields && !errors) return true;
	msg = "The form was not submittet because of the following error(s).\n";
	msg += "Please correct these error(s) and re-submit.\n\n";
	if (empty_fields) {
		msg = "The following required field(s) are empty:" + empty_fields + "\n";
		if (errors) msg += "\n";
	}
	msg += errors;
	alert(msg);
	return false;
}

function Check_input(i) {
	e = parseInt(i.value);
	f = document.Purchase;
	g = i.value;
	if (isNaN(e) && (g!='') && (g!=null)) {
		alert('Number of Licenses must be a number');
		i.focus();
		i.select();
		return 0;
	}
	// CalculateTotals();
	return 1;
}

function Check_existing_clients(i) {
	f = document.Purchase;
	MP_e = parseInt(f.NoLi_MP_existing.value);
	if ((MP_e > 0) && (MP_e < 10)) {
		msg = ('Number of existing licences must be 0 or greater than 10');
		document.getElementById('Number_of_Existing_MiniPoint_Licenses').select();
		document.getElementById('Number_of_Existing_MiniPoint_Licenses').focus();
		alert(msg);
	}
}

function Check_upgrade_clients(i) {
	f = document.Purchase;
	MP_e = parseInt(f.NoLi_MP_existing.value);
	if (isNaN(MP_e)) MP_e = 0;
	MP_u = parseInt(f.NoLi_MP_upgrade.value);
	if (MP_e < MP_u) {
		msg = ('Number of upgrades can not be greater than existing licenses');
		document.getElementById('Number_of_MiniPoint_Upgrades').select();
		document.getElementById('Number_of_MiniPoint_Upgrades').focus();
		alert(msg);
	}
}

function Check_new_clients(i) {
	f = document.Purchase;
	MP_e = parseInt(f.NoLi_MP_existing.value);
	MP_n = parseInt(f.NoLi_MP_new.value);
	if (isNaN(MP_e)) MP_e = 0;
	if ((MP_e < 10) && (MP_n < 10)) {
		document.getElementById('Number_of_new_MiniPoint_Licenses').select();
		document.getElementById('Number_of_new_MiniPoint_Licenses').focus();
		msg = ('Number of New Licenses must be 10 or more.\n\n');
		msg += ('10 Licenses will be shipped with the server.\n');
		alert(msg);
	}
}

function Update_new_clients(i) {
	e = parseInt(i.value);
	g = i.value
	if ((Check_input(i) == 1) && ((g!='0') || (g!='') || (g!=null))) {
		// f.NoLi_MP_new.value = MP_s * 10;
	}
}

function CalculateTotals() {

	f = document.Purchase;
	MP_e = parseInt(f.NoLi_MP_existing.value);
	if (isNaN(MP_e)) MP_e = 0;
	MP_n = parseInt(f.NoLi_MP_new.value);
	if (isNaN(MP_n)) MP_n = 0;
	MP_u = parseInt(f.NoLi_MP_upgrade.value);
	if (isNaN(MP_u)) MP_u = 0;
	
	if (f.Product.value == 'MPStandard') { // MiniPoint Standard
		s_cost = 400;
		n_cost_0 = 400;
		n_cost_1 = 35;
		n_cost_2 = 30;
		n_cost_3 = 25;
		u_cost_1 = 15;
		u_cost_2 = 12;
		u_cost_3 = 10;
	}
	else { // MiniPoint Enterprise
		s_cost = 750
		n_cost_0 = 750;
		n_cost_1 = 55;
		n_cost_2 = 45;
		n_cost_3 = 35;
		u_cost_1 = 28;
		u_cost_2 = 24;
		u_cost_3 = 20;
	}

	MP_newcost = 0;
	MP_upgradecost = 0;
	
	MP_existing = MP_e;
	MP_new = MP_n;
	if ((MP_e == 0) && (MP_n != 0)) {
		if (MP_n < 11) MP_newcost = n_cost_0;
		else if ((MP_n > 10) && (MP_n < 51)) {
			MP_newcost = n_cost_0 + ((MP_n - 10) * n_cost_1);
		}
		else if ((MP_n > 50) && MP_n < 201) {
			MP_newcost = n_cost_0 + (40 * n_cost_1) + ((MP_n - 50) * n_cost_2);
		}
		else if (MP_n > 200) {
			MP_newcost = n_cost_0 + (40 * n_cost_1) + (150 * n_cost_2) + ((MP_n - 200) * n_cost_3);
		}
	}

	if ((MP_e != 0) && (MP_n != 0))  {
		if ((MP_n + MP_e) < 51) MP_newcost = MP_new * n_cost_1;
		else if (((MP_n + MP_e) > 50) && ((MP_n + MP_e) < 201)) {
			if (MP_e < 51) MP_newcost = ((50 - MP_e) * n_cost_1) + ((MP_n - (50 - MP_e)) * n_cost_2);
			if (MP_e > 50) MP_newcost = MP_n * n_cost_2;
		}
		else if (((MP_n + MP_e) > 200)) {
			if (MP_e < 51) MP_newcost = ((50 - MP_e) * n_cost_1) + (150 * n_cost_2) + ((MP_n - 200) * n_cost_3);
			if ((MP_e > 50) && (MP_e < 200)) MP_newcost = ((200 - MP_e) * n_cost_2) + (((MP_n + MP_e) - 200) * n_cost_3);
			if (MP_e > 199) MP_newcost = MP_n * n_cost_3;
		}
	}
	
	MP_upgrade = MP_u;
	if (MP_u < 51){
		MP_upgradecost = MP_u * u_cost_1;
	}
	else if ((MP_u > 50) && MP_n < 201){
		MP_upgradecost = (50 * u_cost_1) + ((MP_upgrade - 50) * u_cost_2);
	}
	else MP_upgradecost = (50 * u_cost_1) + (150 * u_cost_2) + ((MP_upgrade - 200) * u_cost_3);

	document.getElementById('tot_new').innerHTML = MP_new;
	document.getElementById('tot_upgrades').innerHTML = MP_upgrade;
	document.getElementById('tot_clients').innerHTML = MP_new + MP_upgrade;
	document.getElementById('tot_cost').innerHTML = MP_newcost + MP_upgradecost;
	document.Purchase.hidden_tot_cost.value = MP_newcost + MP_upgradecost;
}

function field_required() {
	f = document.Purchase;
	if (f.shipment[0].checked) {
		document.getElementById('email_label').innerHTML = "<b>E-mail Address:</b>";
		document.getElementById('fax_label').innerHTML = "Fax Number:";
	}
	else if (f.shipment[1].checked) {
		document.getElementById('email_label').innerHTML = "E-mail Address:";
		document.getElementById('fax_label').innerHTML = "<b>Fax Number:</b>";
	}
	else if (f.shipment[2].checked) {
		document.getElementById('email_label').innerHTML = "E-mail Address:";
		document.getElementById('fax_label').innerHTML = "Fax Number:";
	}

}
