// convert the number to currency
function convertToCurrency(cur) {
	
	cur = cur != 0 ? (Math.round(cur * 100)) / 100 : 0.00;
	
	// make sure there is decimal point with two digits afte rit
	var parts = (cur + "").split(".");
	if (parts.length < 2) 
		cur += ".00";
	else if (parts[1].length == 1) 
		cur += "0";
	
	return "$" + cur;
}

// validate the address contained in the form
function validateAddressForm(frm, checkPhone, ignoreName) {
		
	var msg = "";
    var val;
    
    if (ignoreName) {
	    val = document.getElementById("attention").value;	    
	    if (!val || val.length == 0) {
		    msg += "Name is required.<br />";
	    } else {
		    var regex = /\w+ \w+/;
		    if (!val.match(regex)) {
			    msg += "Enter a valid name.<br />";
		    }
	    }
	}
		
	val = document.getElementById("address1").value;
	if (!val || val.length == 0) {
		msg += "Address line 1 is required.<br />";
	} else {
		var regex = /\d+ \w+/;
		if (!val.match(regex)) {
			msg += "Enter a valid address.<br />";
		}
	}
	
	val = document.getElementById("city").value;
	if (!val || val.length == 0) {
		msg += "Your city is required.<br />";
	} else {
		var regex = /\d/;
		if (val.match(regex)) {
			msg += "Enter a valid city.<br />";
		}
	}
	
	val = document.getElementById("state").value;
	if (!val || val.length == 0) {
		msg += "Your state is required.<br />";
	} else {
		var regex = /\d/;
		if (val.match(regex) || val.length != 2) {
			msg += "Enter a valid state.<br />";
		}
	}
	
	val = document.getElementById("zip").value;
	if (!val || val.length == 0) {
		msg += "Your zipcode is required.<br />";
	}	 else {
		var regex = /\d{5}-?\d*/;
		if (!val.match(regex)) {
			msg += "Enter a valid zipcode.<br />";
		}
	}
	
	if (checkPhone) {
	    val = document.getElementById("phone").value;
	    if (!val || val.length == 0) {
		    msg += "Your phone number is required.<br />";
	    }	 else {
		    var regex = /\d{3}-?.?\d{3}/;
		    if (!val.match(regex)) {
			    msg += "Enter a valid phone number. (ex 775-324-3233)<br />";
		    }
	    }
	}
	
	// if there were errors show the error msg
	var valid = true;
	if (msg != "") {		
	
		var msgSpan = document.getElementById("errorMessage");
		if (msgSpan) {
			msgSpan.innerHTML = msg;
			msgSpan.style.display = "inline";
		}
		
		msgSpan = document.getElementById("errorMessageNote");
		if (msgSpan) {
			msgSpan.innerHTML = "Error - check requirements above.";
			msgSpan.style.display = "inline";
		}
		
		scroll(0, 0);
		
		valid = false;
	} 
	
	return valid;
}

function showRFQ() {

	var o = document.getElementById("rfqInfo");
	showHideDiv(o);
	o = document.getElementById("rfqButton");
	if (o) {
		if (o.value == "Request a quote")
			o.value = "Cancel request";
		else
			o.value = "Request a quote";
	}
}

function validateEmail() {

	var frm = document.getElementById("emailForm");
	
	if (frm) {
		if (frm.custName.value == "") {
			alert("Please enter your name.");
			return false;
		}
		if (!isValidEmail(frm.custEmail.value)) {
			alert("Please enter a valid email address.");
			return false;
		}
	
		frm.submit();
	}
}

function validateEmailAddress(frm) {
		
	var email = frm.emailAddress.value;
		
	var valid = email && email.length > 0 && isValidEmail(email);
		
	return valid;
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0); 
}

function validatePassword(frm) {
	
	var pwd = frm.password.value;
	var pwd2 = frm.password2.value;
		
	var valid = pwd && pwd.length > 4 && pwd2 && pwd == pwd2;
		
	return valid;
}

function showHideDiv(theItem) {
	if (typeof(theItem) != "undefined") {		
		if (theItem.style.display == 'none')
			theItem.style.display = 'block'
		else
			theItem.style.display = 'none';
	}
}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
	  	c_start = document.cookie.indexOf(c_name + "=")
	  
	  	if (c_start != -1) { 
			c_start = c_start + c_name.length + 1 
			c_end = document.cookie.indexOf(";",c_start)
			if (c_end == -1)
				c_end = document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return null
}

function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setTime(exdate.getTime() + (expiredays*24*3600*1000));
	document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : "; expires=" + exdate)
}

function isLoggedIn() {
	
	var personID = getCookie("PersonID");
	//alert(personID);
	return personID != null;
}

function getCartItemCount() {
	
	var itemCount = getCookie("CartItemCount");
	return itemCount && itemCount != "" ? itemCount : 0;
}


function prepPage() {
		    	
	document.getElementById("cartItemCount").innerHTML = "(" + getCartItemCount() + " items)";

	return;
}

function goToPage(url) {
    window.location.href = url;
}

function submitSearch() {

    var frm = document.getElementById("SearchForm");
    if (frm) {
        if (frm.search && frm.search.value != "") {
            frm.submit();
        }
    }
}

