// based on http://www.alistapart.com/articles/dropdowns and http://www.htmldog.com/articles/suckerfish/dropdowns/
if ((document.all) && (document.getElementById)) {
	function MenuListElementHoverFix() {
		var _this	= this;
		
		if (typeof(Array.prototype.push) == 'undefined') {
			// IE < 5.5 does not support push/pop
			function push() {
				var j = this.length;
				for (var i = 0; i < push.arguments.length; ++i) {
					this[j] = push.arguments[i];
					j++;
				}
			}
			function pop() {
				var i = this[this.length - 1];
				this.length--;
				return i;
			}
			Array.prototype.push	= push;
			Array.prototype.pop		= pop;
		}
		
		this.className				= 'hover';	// change the class name to use here if you are not happy with it
		this.classNameSuffix		= ' ' + this.className;
		this.removeClassNameRegExp	= new RegExp("\\b ?" + this.className + "\\b");
		this.hoverElementStack		= new Array();
		
		this.onmouseover	= function() {
			if (!_this.isHover(this)) {
				if (this.getAttribute('is_hover_class_removing') != 'true') {
					this.className	+= _this.classNameSuffix;
				}
				this.setAttribute('is_hover', 'true');
			}
		}
		this.isHover	= function(element) {
			return (element.getAttribute('is_hover') == 'true');
		}
		this.checkNonHoverState	= function(element) {
			if (!_this.isHover(element)) {
				element.className	= element.className.replace(_this.removeClassNameRegExp, '');
			}
			element.removeAttribute('is_hover_class_removing');
		}
		this.checkLastNonHoverState	= function() {
			var hoverElement	= null;
			if (_this.hoverElementStack.length > 0) {
				hoverElement	= _this.hoverElementStack.pop();
			}
			for (var i = _this.hoverElementStack.length - 1; i >= 0; i--) {
				// we need to apply the "non-hover" class to the children first - else the result is mixed in the IE
				var o = _this.hoverElementStack[i];
				while (o != null) {
					o	= o.offsetParent;
					if (o == hoverElement) {
						_this.checkNonHoverState(_this.hoverElementStack[i]);
						break;
					}
				}
			}
			if (hoverElement != null) {
				_this.checkNonHoverState(hoverElement);
			}
		}
		this.onmouseout		= function() {
			// all this mess to not change the class immediately (which would cause flickering in the IE)
			this.setAttribute('is_hover', 'false');
			if (this.getAttribute('is_hover_class_removing') != 'true') {
				this.setAttribute('is_hover_class_removing', 'true');
				_this.hoverElementStack.push(this);
				window.setTimeout(_this.checkLastNonHoverState, 100);
			}
		}
		this.fixElement		= function(elementRoot) {
			for (var i=0; i < elementRoot.childNodes.length; i++) {
				var node = elementRoot.childNodes[i];
				if (node.nodeName == "LI") {
					node.onmouseover	= this.onmouseover;
					node.onmouseout		= this.onmouseout;
				}
				this.fixElement(node);
			}
		};
		this.findElement	= function(elementRoot) {
			for (var i=0; i< elementRoot.childNodes.length; i++) {
				var node = elementRoot.childNodes[i];
				if (node.nodeName == "UL") {
					if ((node.id != '') && ((node.id.indexOf("menu") >= 0) || (node.id.indexOf("nav") >= 0))) {
						this.fixElement(node);
						continue;	// do not iterate through children of already fixed element
					}
				}
				this.findElement(node);
			}
		};
		this.fix			= function() {
			var node = document.body;
			if (node) {
				_this.findElement(node);
			}
		}
		this.addOnLoad		= function() {
			if (window.attachEvent) {
				window.attachEvent("onload", this.fix);
			} else {
				window.onload	= this.fix;
			}
		}
	}
	(new MenuListElementHoverFix()).addOnLoad();
}

//Validation Refferal Pad Request Form//
function formvalidation()
{
	if(document.referralpad.Physician_Name.value=='')
	{
		alert("The Physician's Name field can not be left empty");
		document.referralpad.Physician_Name.focus();
		return false;
	}
	if(document.referralpad.Mailing_Address.value=='')
	{
		alert("The Mailing Address field can not be left empty");
		document.referralpad.Mailing_Address.focus();
		return false;
	}
	if(document.referralpad.Attention.value=='')
	{
		alert("The Attention field can not be left empty");
		document.referralpad.Attention.focus();
		return false;
	}
	if(document.referralpad.Phone_Number.value=='')
	{
		alert("The Phone Number field can not be left empty");
		document.referralpad.Phone_Number.focus();
		return false;	
	}
	
	else
	{
		if(numbercheck(document.referralpad.Phone_Number.value)==false){
			document.referralpad.Phone_Number.focus();
			return false;
		}
	}
	
	var rad_flag=false;
	for(var i=0;i<document.referralpad.Quantity_Needed.length;i++){
		if(document.referralpad.Quantity_Needed[i].checked==true){
			rad_flag=true;
		}
	}
	if(rad_flag==false){
		alert("The Quantity Needed field can not be left empty");
		return false;	
	}
	if(document.referralpad.Quantity_Needed[3].checked==true){
		if(document.referralpad.Specify_Quantity.value==''){
			alert("The Specify Quantity field can not be left empty");
			document.referralpad.Specify_Quantity.focus();
			return false;
		}
		else{
			if(isNaN(document.referralpad.Specify_Quantity.value)){
				alert("The Specify Quantity should be numeric");
				document.referralpad.Specify_Quantity.focus();
				return false;
			}	
		}
	}
	else{
		document.getElementById("sp_quantity").value=''; 
	}
	
}


//email errors
function echeck(str) 
{
	var email = str;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (email.match(filter)) {
	  // Yay! valid
	  return true;
	}
	else
	{
		alert("The email address supplied in the Mailing Address field is in incorrect format.")
		return false;
	}
}


function numbercheck(str) 
{
	var number = str;
	var phoneNumberPattern = /^[1-9]\d{2}-\d{3}-\d{4}$/;
	
	if (number.match(phoneNumberPattern)) {
	  // Yay! valid
	  return true;
	}
	else
	{
		alert("The Phone number supplied in the Phone Number field is an incorrect format Ex:111-222-3333.")
		return false;
	}
}


//Validation Refferal Pation Satisfaction Questionnarie Form//
function formvalidation2()
{
	if(document.questions.contacted[0].checked==true)
	{
		if(document.questions.name.value=='')
		{
			alert("The Name field can not be left empty");
			document.questions.name.focus();
			return false;
		}
		if(document.questions.Telephone_number.value=='')
		{
			alert("The Telephone Number field can not be left empty");
			document.questions.Telephone_number.focus();
			return false;	
		}
	
		else
		{
			if(numbercheck(document.questions.Telephone_number.value)==false){
				document.questions.Telephone_number.focus();
				return false;
			}
		}
		
		if(document.questions.Time_to_Call.value=='')
		{
			alert("The Time to Call field can not be left empty");
			document.questions.Time_to_Call.focus();
			return false;	
		}
	}
	
}



//Validation Refferal Payment Form//
function formvalidaton3()
{
		
	if(document.paymentform.patient_name.value=='')
	{
		alert("The Patient Name field can not be left empty");
		document.paymentform.patient_name.focus();
		return false;
	}
	if(document.paymentform.account_number.value=='')
	{
		alert("The Account  Number field can not be left empty");
		document.paymentform.account_number.focus();
		return false;
	}
	if(document.paymentform.payment_amount.value=='')
	{
		alert("The Payment Amount field can not be left empty");
		document.paymentform.payment_amount.focus();
		return false;
	}
	else
	{
		if(isNaN(document.paymentform.payment_amount.value))
		{
			alert("Thep Payment Amount should be numeric");
			document.paymentform.account_number.focus();
			return false;
		}
	}
	if(document.paymentform.Credit_Card_Number.value=='')
	{
		alert("The Credit Card Number field can not be left empty");
		document.paymentform.Credit_Card_Number.focus();
		return false;
	}
	else
	{
		if(isNaN(document.paymentform.Credit_Card_Number.value))
		{
			alert("The Credit Card Number is in an invalid format");
			document.paymentform.Credit_Card_Number.focus();
			return false;
		}
		else
		{
			if(checkCreditCard(document.paymentform.Credit_Card_Number.value,document.paymentform.card_type.value)==false)
			{
				if(document.paymentform.card_type.value=='Visa')
					alert("The Credit Card Number is in an in valid format. Ex:4xxxxxxxxxxxxxxx");
				else if(document.paymentform.card_type.value=='MasterCard')
					alert("The Credit Card Number is in an in valid format. Ex:5xxxxxxxxxxxxxxx");
				else if(document.paymentform.card_type.value=='Discover')
					alert("The Credit Card Number is in an in valid format. Ex:6011xxxxxxxxxxxx");

				document.paymentform.Credit_Card_Number.focus();
				return false;	
			}
		}
	}
	if(document.paymentform.Expiration_Date.value=='')
	{
		alert("The Expiration Date field can not be left empty");
		document.paymentform.Expiration_Date.focus();
		return false;
	}
	if(document.paymentform.Signature_Code.value=='')
	{
		alert("The Signature Code field can not be left empty");
		document.paymentform.Signature_Code.focus();
		return false;
	}
	else
	{
		if(isNaN(document.paymentform.Signature_Code.value))
		{
			alert("The Signature Code should be numeric");
			document.paymentform.Signature_Code.focus();
			return false;
		}
	}
	if(document.paymentform.Zip_Code.value=='')
	{
		alert("The Zip Code field can not be left empty");
		document.paymentform.Zip_Code.focus();
		return false;
	}
	else
	{
		if(isNaN(document.paymentform.Zip_Code.value))
		{
			alert("The Zip Code should be numeric");
			document.paymentform.Zip_Code.focus();
			return false;
		}
	}

}


function getVal(val)
{
	var floatval =parseFloat(val);
	var fval=floatval.toFixed(2);
	document.paymentform.payment_amount.value=fval;	
}

function checkCreditCard (cardnumber, cardname) {
	 
	var ccErrorNo = 0;
	var ccErrors = new Array ()
	ccErrors [0] = "Unknown card type";
	ccErrors [1] = "No card number provided";
	ccErrors [2] = "Credit card number is an invalid format";
	ccErrors [3] = "Credit card number is an invalid format";
	ccErrors [4] = "Credit card number has an inappropriate number of digits";
	// Array to hold the permitted card characteristics
	var cards = new Array();
	
	// Define the cards we support. You may add addtional card types.
	
	//  Name:      As in the selection box of the form - must be same as user's
	//  Length:    List of possible valid lengths of the card number for the card
	//  prefixes:  List of possible prefixes for the card
	//  checkdigit Boolean to say whether there is a check digit
	
	cards [0] = {name: "Visa", 
			   length: "13,16", 
			   prefixes: "4",
			   checkdigit: true};
	cards [1] = {name: "MasterCard", 
			   length: "16", 
			   prefixes: "51,52,53,54,55",
			   checkdigit: true};
	cards [2] = {name: "DinersClub", 
			   length: "14,16", 
			   prefixes: "305, 36, 38, 54,55",
			   checkdigit: true};
	cards [3] = {name: "CarteBlanche", 
			   length: "14", 
			   prefixes: "300,301,302,303,304,305",
			   checkdigit: true};
	cards [4] = {name: "AmEx", 
			   length: "15", 
			   prefixes: "34,37",
			   checkdigit: true};
	cards [5] = {name: "Discover", 
			   length: "16", 
			   prefixes: "6011,622,64,65",
			   checkdigit: true};
	cards [6] = {name: "JCB", 
			   length: "16", 
			   prefixes: "35",
			   checkdigit: true};
	cards [7] = {name: "enRoute", 
			   length: "15", 
			   prefixes: "2014,2149",
			   checkdigit: true};
	cards [8] = {name: "Solo", 
			   length: "16,18,19", 
			   prefixes: "6334, 6767",
			   checkdigit: true};
	cards [9] = {name: "Switch", 
			   length: "16,18,19", 
			   prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
			   checkdigit: true};
	cards [10] = {name: "Maestro", 
			   length: "12,13,14,15,16,18,19", 
			   prefixes: "5018,5020,5038,6304,6759,6761",
			   checkdigit: true};
	cards [11] = {name: "VisaElectron", 
			   length: "16", 
			   prefixes: "417500,4917,4913,4508,4844",
			   checkdigit: true};
	cards [12] = {name: "LaserCard", 
			   length: "16,17,18,19", 
			   prefixes: "6304,6706,6771,6709",
			   checkdigit: true};
			   
	// Establish card type
	var cardType = -1;
	for (var i=0; i<cards.length; i++) {
	
	// See if it is this card (ignoring the case of the string)
	if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
	  cardType = i;
	  break;
	}
	}
	
	// If card type not found, report an error
	if (cardType == -1) {
	 ccErrorNo = 0;
	 return false; 
	}
	
	// Ensure that the user has provided a credit card number
	if (cardnumber.length == 0)  {
	 ccErrorNo = 1;
	 return false; 
	}
	
	// Now remove any spaces from the credit card number
	cardnumber = cardnumber.replace (/\s/g, "");
	
	// Check that the number is numeric
	var cardNo = cardnumber
	var cardexp = /^[0-9]{13,19}$/;
	if (!cardexp.exec(cardNo))  {
	 ccErrorNo = 2;
	 return false; 
	}
	   
	// Now check the modulus 10 check digit - if required
	if (cards[cardType].checkdigit) {
	var checksum = 0;                                  // running checksum total
	var mychar = "";                                   // next char to process
	var j = 1;                                         // takes value of 1 or 2
	
	// Process each digit one by one starting at the right
	var calc;
	for (i = cardNo.length - 1; i >= 0; i--) {
	
	
	  // Extract the next digit and multiply by 1 or 2 on alternative digits.
	  calc = Number(cardNo.charAt(i)) * j;
	
	  // If the result is in two digits add 1 to the checksum total
	  if (calc > 9) {
		checksum = checksum + 1;
		calc = calc - 10;
	  }
	
	  // Add the units element to the checksum total
	  checksum = checksum + calc;
	
	  // Switch the value of j
	  if (j ==1) {j = 2} else {j = 1};
	} 
	
	// All done - if checksum is divisible by 10, it is a valid modulus 10.
	// If not, report an error.
	if (checksum % 10 != 0)  {
	 ccErrorNo = 3;
	 return false; 
	}
	}  
	
	// The following are the card-specific checks we undertake.
	var LengthValid = false;
	var PrefixValid = false; 
	var undefined; 
	
	// We use these for holding the valid lengths and prefixes of a card type
	var prefix = new Array ();
	var lengths = new Array ();
	
	// Load an array with the valid prefixes for this card
	prefix = cards[cardType].prefixes.split(",");
	  
	// Now see if any of them match what we have in the card number
	for (i=0; i<prefix.length; i++) {
	var exp = new RegExp ("^" + prefix[i]);
	if (exp.test (cardNo)) PrefixValid = true;
	}
	  
	// If it isn't a valid prefix there's no point at looking at the length
	if (!PrefixValid) {
	 ccErrorNo = 3;
	 return false; 
	}
	
	// See if the length is valid for this card
	lengths = cards[cardType].length.split(",");
	for (j=0; j<lengths.length; j++) {
	if (cardNo.length == lengths[j]) LengthValid = true;
	}
	
	// See if all is OK by seeing if the length was valid. We only check the 
	// length if all else was hunky dory.
	if (!LengthValid) {
	 ccErrorNo = 4;
	 return false; 
	};   
	
	// The credit card is in the required format.
	return true;
}

