﻿function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function padLeft(str, pad, count) {
	while(str.length<count)
	str=pad+str;
	return str;
}

function formatPercentage(num) {
	num = num.toString().replace(/\D/,'');
	if(isNaN(num)) num = "0";
	if(parseInt(num)>100) num="100";
	if(parseInt(num)<0) num="0";
	sign = (num == (num = Math.abs(num)));
	return (((sign)?'':'-') + num + "%");
}
		
function validateEmailAddress(txtEmail) {
	var reEmail =  /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return reEmail.test(txtEmail);
}
			
function validatePassword(txtPassword, txtInvalidChars) {
	if (txtPassword.length<6) { return false; }
	
	// Need to ensure that the password does not include any of the invalid characters
	return true;
}

function validateUserName(txtUserName, txtInvalidChars) {
	if (txtUserName.length<6) { return false; }
	// Need to ensure that the user name does not include any of the invalid characters
	return true;
}

function defaultSelectBox(selectObjectId, defaultValue) {
	var i=0;
	var selectObject=document.getElementById(selectObjectId);
		
	while ((selectObject.options[i].value != defaultValue)&&(i < selectObject.options.length)) {i++;}
	if (i < selectObject.options.length) {selectObject.selectedIndex = i;}
	
	return true;
} 

function validateSubscription(form, captureUserName) {
	var txtInvalUserChars=" :;~`!@#$%^&*()_+=-{}[]|\'<>?,./";
	var txtInvalPassChars="' ?";
	var FormType=form.FormType.value;
	var Password1=form.Password1.value;				
	var Password2=form.Password2.value;
	
	if (FormType!="changepassword") {
		var ChangePassword=form.ChangePassword.checked;
		var RequestedUserName=form.RequestedUserName.value;
		var EmailAddress1=form.EmailAddress1.value;
		var EmailAddress2=form.EmailAddress2.value;
			
		if (captureUserName) {						
			if (!validateUserName(RequestedUserName, txtInvalUserChars)) {
				alert("User name must be at least 6 characters in length and not contain any of the following invalid characters "+txtInvalUserChars);
				form.RequestedUserName.focus();
				return false;
			}
		}
		if (EmailAddress1=="" || !validateEmailAddress(EmailAddress1)){	
			alert("You must provide a valid email address");
			form.EmailAddress1.focus();
			return false;
		} 
		if (EmailAddress1!=EmailAddress2) {
			alert("Email addresses do not match. Please ensure email and confirmation email are the same.");
			form.EmailAddress1.focus();					
			return false;
		} 
	} else {
		ChangePassword=true;
	}
		
	if ((FormType=="create")||(ChangePassword==true)) {	
		if (!validatePassword(Password1, txtInvalPassChars)) {
			alert("Password must be at least 6 characters in length and not contain any of the following invalid characters "+txtInvalPassChars);
			form.Password1.value="";
			form.Password2.value="";
			form.Password1.focus();
			return false;
		}
				
		if (Password1!=Password2) {
			alert("Passwords do not match. Please ensure password and confirmation password are the same.");
			form.Password1.value="";
			form.Password2.value="";
			form.Password1.focus();
			return false;
		}
 	}
				

	if (FormType!="changepassword") {
		// Set the requested username to it's lowercase equivalent
		if (FormType=="create") {
			form.RequestedUserName.value=RequestedUserName.toLowerCase();
		}
	}
	return true;
}


function validateInputEmail(form) {
	var EmailAddress1=form.EmailAddress1.value;
		
	if (EmailAddress1=="" || !validateEmailAddress(EmailAddress1)){	
		alert("That's not a valid email address");
		form.EmailAddress1.focus();
		return false;
	} 
	
	return true;
}


function validateAddress(form) {
	
	if (form.Addressee.value=="") {
		form.Addressee.focus();					
		alert("You must provide an addressee for this address.");
		return false;
	}

	if (form.AddressLine1.value=="") {
		form.AddressLine1.focus();					
		alert("You must provide at least the first line of the address.");
		return false;
	}

	if (form.City.value=="") {
		form.City.focus();					
		alert("You must provide a City for this address.");
		return false;
	}

	if (form.State.value=="") {
		form.State.focus();					
		alert("You must provide a State for this address.");
		return false;
	}

	if (form.Country.selectedIndex==0) {
		form.Country.focus();					
		alert("You must provide a Country for this address.");
		return false;
	}

	
	return true;
}



function menuToggledisplay(togglearea, toggleswitch, sessionVar) {
	if (document.getElementById(togglearea).style.display=="") {
		document.getElementById(togglearea).style.display="none";
		document.getElementById(toggleswitch).innerHTML="▼";
		document.getElementById('hiddenIFrame').src=
			"nbc/include/nbc_setsessionvar.php?var="+sessionVar+"&val=1";
	} else {
		document.getElementById(togglearea).style.display="";
		document.getElementById(toggleswitch).innerHTML="▲";
		document.getElementById('hiddenIFrame').src=
			"nbc/include/nbc_setsessionvar.php?var="+sessionVar+"&val=0";
	}
}


function togglediv(divid, checkid, form) {
	if (document.getElementById(checkid).checked==true) {
		document.getElementById(divid).style.display="";
	} else {
		document.getElementById(divid).style.display="none";		
	}
	return;
}