var illegalChars = /[\W_]/; // allow only letters and numbers
var nospaces = /^\s+|\s+$/g; //remove all spaces
var onlynumbers = /[\(\)\.\-\ a-z]/g; //only numbers
function useshippinginfo () {
	setter = !document.getElementById("bline1").disabled;
	document.getElementById("bline1").disabled = setter;
	document.getElementById("bline2").disabled = setter;
	document.getElementById("bcity").disabled = setter;
	document.getElementById("bstate").disabled = setter;
	document.getElementById("bpostal").disabled = setter;
}
function chooseshipping (which) {
	document.getElementById('row'+which).style.color = '#D55224';
	document.getElementById('rowa'+which).style.color = '#D55224';
	if (which == 1) {
		document.getElementById('row2').style.color = '#000000';
		document.getElementById('rowa2').style.color = '#000000';;
		document.getElementById('row3').style.color = '#000000';
		document.getElementById('rowa3').style.color = '#000000';
	}
	if (which == 2) {
		document.getElementById('row1').style.color = '#000000';
		document.getElementById('rowa1').style.color = '#000000';
		document.getElementById('row3').style.color = '#000000';
		document.getElementById('rowa3').style.color = '#000000';
	}
	if (which == 3) {
		document.getElementById('row2').style.color = '#000000';
		document.getElementById('rowa2').style.color = '#000000';;
		document.getElementById('row1').style.color = '#000000';
		document.getElementById('rowa1').style.color = '#000000';
	}
}
function handlereturn (message) {
	if (message != "") {
		alert("The Following Errors Occured:\n"+message);
		return false;
	}
	else return true;
}
function validateemail (email) {
	var error = "";
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(email))) { 
	   alert("'"+email+"' is an Invalid Email Address");
	   return false;
	}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (email.match(illegalChars) && error == "") {
	   alert("'"+email+"' Contains Illegal Characters");
	   return false;
	}
	else {
		return true;
	}
}
function cleargift() {
	document.cleargift.submit();
}
function validategallery() {
	thetitle = document.getElementById("title").value;//.replace(/^\s+|\s+$/g, "");
	thedescription = document.getElementById("description").value;//.replace(/^\s+|\s+$/g, "");
	var message = "";
	if (thedescription.length < 1) message += "\nThe Gallery Must Have A Title";
	if (thedescription.length < 5) message += "\nThe Gallery Must Have A Description.";
	return handlereturn(message);
}
function validateregistration() {
	theform = document.newaccount;
	thealias = document.getElementById("alias").value;//.replace(/^\s+|\s+$/g, "");
	thepass = document.getElementById("pass").value;//.replace(/^\s+|\s+$/g, "");
	theemail = document.getElementById("email").value;//.replace(/^\s+|\s+$/g, "");
	theterms = document.getElementById("acceptterms");
	
	var message = "";
	if (illegalChars.test(thealias) || thealias.length < 5) message += "\nThe Username Can Only Be AlphaNumeric, And Must Be At Least 5 Characters.";
	if (illegalChars.test(thepass) || thepass.length < 5) message += "\nThe Password Can Only Be AlphaNumeric, And Must Be At Least 5 Characters.";
	if (!validateemail(theemail)) return false;
	if (!theterms.checked) message += "\nYou Must Agree To The Terms.";
	return handlereturn(message);
}
function validateprofile() {
	theform = document.profileinfo;
	thefirstname = document.getElementById("firstname").value;//.replace(/^\s+|\s+$/g, "");
	thelastname = document.getElementById("lastname").value;//.replace(/^\s+|\s+$/g, "");
	theemail = document.getElementById("email").value;//.replace(/^\s+|\s+$/g, "");
	
	var message = "";
	if (thefirstname.replace(nospaces, "") == "" || thelastname.replace(nospaces, "") == "") message += "\nFirst and Last Names Can Not Be Empty.";
	if (!validateemail(theemail)) return false;
	return handlereturn(message);
}
function validateshipping() {
	theform = document.shippinginfo;
	thesname = document.getElementById("sname").value;//.replace(/^\s+|\s+$/g, "");
	thesline1 = document.getElementById("sline1").value;//.replace(/^\s+|\s+$/g, "");
	thescity = document.getElementById("scity").value;//.replace(/^\s+|\s+$/g, "");
	thesstate = document.getElementById("sstate").value;//.replace(/^\s+|\s+$/g, "");
	thespostal = document.getElementById("spostal").value;//.replace(/^\s+|\s+$/g, "");
	var message = "";
	if (thesname.replace(nospaces, "") == "") message += "\nThe Shipping Name Can Not Be Empty.";
	if (thesline1.replace(nospaces, "") == "") message += "\nThe Street Address Line 1 Can Not Be Empty.";
	if (thescity.replace(nospaces, "") == "") message += "\nThe City Can Not Be Empty.";
	if (thesstate.replace(nospaces, "") == "") message += "\nThe State Can Not Be Empty.";
	if (thespostal.replace(nospaces, "") == "") message += "\nThe Postal / Zip Code Can Not Be Empty.";
	return handlereturn(message);
}
function validatebilling() {
	theform = document.billinginfo;
	thebfname = theform.name_on_card1.value;//.replace(/^\s+|\s+$/g, "");
	theblname = theform.name_on_card2.value;//.replace(/^\s+|\s+$/g, "");
	thebline1 = document.getElementById("bline1").value;//.replace(/^\s+|\s+$/g, "");
	thebcity = document.getElementById("bcity").value;//.replace(/^\s+|\s+$/g, "");
	thebstate = document.getElementById("bstate").value;//.replace(/^\s+|\s+$/g, "");
	thebpostal = document.getElementById("bpostal").value;//.replace(/^\s+|\s+$/g, "");
	if (document.getElementById("updatecc1")) docc = document.getElementById("updatecc1").checked;
	else if (document.getElementById("newcc")) docc = document.getElementById("newcc").checked;
	
	var message = "";
	if (theblname.replace(nospaces, "") == "") message += "\nThe Billing Last Name Can Not Be Empty.";
	if (thebfname.replace(nospaces, "") == "") message += "\nThe Billing First Name Can Not Be Empty.";
	if (thebline1.replace(nospaces, "") == "") message += "\nThe Street Address Line 1 Can Not Be Empty.";
	if (thebcity.replace(nospaces, "") == "") message += "\nThe City Can Not Be Empty.";
	if (thebstate.replace(nospaces, "") == "") message += "\nThe State Can Not Be Empty.";
	if (thebpostal.replace(nospaces, "") == "") message += "\nThe Postal / Zip Code Can Not Be Empty.";
	if (docc == true) {
		if (!checkCCNumber(document.getElementById("card_number"),document.getElementById("name_on_card1"),document.getElementById("name_on_card2"),document.getElementById("exp_month"),document.getElementById("exp_year"),document.getElementById("card_type"))) return false;
	}
	return handlereturn(message);
}
function validatenewpassword() {
	theform = document.passwordreset;
	thepass = document.getElementById("pass").value;//.replace(/^\s+|\s+$/g, "");
	thenewpass = document.getElementById("newpass").value;//.replace(/^\s+|\s+$/g, "");
	theverifypass = document.getElementById("verifypass").value;//.replace(/^\s+|\s+$/g, "");
	var message = "";
	if (illegalChars.test(thenewpass) || thenewpass.length < 5 || theverifypass != thenewpass) message += "\nThe Password Can Only Be AlphaNumeric, Must Be At Least 5 Characters, And Must Be Verified.";
	return handlereturn(message);
}
function validategiftcert() {
	theform = document.giftform;
	thefname = document.getElementById("fname").value;
	thelname = document.getElementById("lname").value;
	thefromname = document.getElementById("fromname").value;
	themsg = document.getElementById("msg").value;
	thetoemail = document.getElementById("toemail").value;
	theenvelope = document.getElementById("envelope").value;
	theaddress1 = document.getElementById("address1").value;
	theaddress2 = document.getElementById("address2").value;
	thecity = document.getElementById("city").value;
	thestate = document.getElementById("state").value;
	thepostal = document.getElementById("postal").value;
	theamount = document.getElementById("amount").value;
	theemail = document.getElementById("email").checked;
	theship = document.getElementById("ship").checked;
	var message = "";
	if (thefname.replace(nospaces, "") == "") message += "\nYou Need At Least The First Name Of The Recipient.";
	if (thefromname.replace(nospaces, "") == "") message += "\nYou Need To Enter The Sender's Name.";
	if (!theemail && !theship) {
		message += "\nYou Need To Choose a Delivery Method.";
	}
	else {
		if (theemail) {
			//email
			if (thetoemail.replace(nospaces, "") == "" || !validateemail(thetoemail)) message += "\nYou Need A Valid Recipient Email Address.";
		}
		else {
			//ship
			if (theenvelope.replace(nospaces, "") == "") message += "\nYou Need An Envelope Name.";
			if (theaddress1.replace(nospaces, "") == "") message += "\nYou Need The First Line of The Address.";
			if (thecity.replace(nospaces, "") == "") message += "\nYou Need A City To Ship To.";
			if (thestate.replace(nospaces, "") == "") message += "\nYou Need A State To Ship To.";
			if (thepostal.replace(nospaces, "") == "") message += "\nYou Need A Postal Code To Ship To.";
		}
	}
	if (theamount.replace(nospaces, "") == "" || isNaN(theamount) || theamount <= 0) message += "\nYou Need To Enter A Valid Gift Amount.";
	return handlereturn(message);
}
function validatecheckout() {
	theform = document.checkoutform;
	theemail = document.getElementById("email").value;
	thesname = document.getElementById("sname").value;//.replace(/^\s+|\s+$/g, "");
	thesline1 = document.getElementById("sline1").value;//.replace(/^\s+|\s+$/g, "");
	thescity = document.getElementById("scity").value;//.replace(/^\s+|\s+$/g, "");
	thesstate = document.getElementById("sstate").value;//.replace(/^\s+|\s+$/g, "");
	thespostal = document.getElementById("spostal").value;//.replace(/^\s+|\s+$/g, "");
	thebfname = theform.name_on_card1.value;//.replace(/^\s+|\s+$/g, "");
	theblname = theform.name_on_card1.value;//.replace(/^\s+|\s+$/g, "");
	thebline1 = document.getElementById("bline1").value;//.replace(/^\s+|\s+$/g, "");
	thebcity = document.getElementById("bcity").value;//.replace(/^\s+|\s+$/g, "");
	thebstate = document.getElementById("bstate").value;//.replace(/^\s+|\s+$/g, "");
	thebpostal = document.getElementById("bpostal").value;//.replace(/^\s+|\s+$/g, "");
	if (validateemail(theemail) == "invalid") message += "\nThe Email Address Seems to Be Invalid.";
	if (validateemail(theemail) == "badchars") message += "\nThe Email Address Can Only Be AlphaNumeric.";
	if (!document.getElementById("shipping1").checked && !document.getElementById("shipping2").checked) message += "You Must Choose a Shipping Option.";
	var message = "";
	if (document.getElementById("newshipping").checked) {
		if (thesname.replace(nospaces, "") == "") message += "\nThe Shipping Name Can Not Be Empty.";
		if (thesline1.replace(nospaces, "") == "") message += "\nThe Street Address Line 1 Can Not Be Empty.";
		if (thescity.replace(nospaces, "") == "") message += "\nThe City Can Not Be Empty.";
		if (thesstate.replace(nospaces, "") == "") message += "\nThe State Can Not Be Empty.";
		if (thespostal.replace(nospaces, "") == "") message += "\nThe Postal / Zip Code Can Not Be Empty.";
	}
	if (document.getElementById("newbilling").checked) {
		if (!checkCCNumber(document.getElementById("card_number"),document.getElementById("name_on_card1"),document.getElementById("name_on_card2"),document.getElementById("exp_month"),document.getElementById("exp_year"),document.getElementById("card_type"))) return false;
		if (thebfname.replace(nospaces, "") == "") message += "\nThe Billing First Name Can Not Be Empty.";
		if (theblname.replace(nospaces, "") == "") message += "\nThe Billing Last Name Can Not Be Empty.";
		if (thebline1.replace(nospaces, "") == "") message += "\nThe Street Address Line 1 Can Not Be Empty.";
		if (thebcity.replace(nospaces, "") == "") message += "\nThe City Can Not Be Empty.";
		if (thebstate.replace(nospaces, "") == "") message += "\nThe State Can Not Be Empty.";
		if (thebpostal.replace(nospaces, "") == "") message += "\nThe Postal / Zip Code Can Not Be Empty.";
	}
	return handlereturn(message);
}
function validatecheckout1() {
	theform = document.checkoutform;
	theemail = document.getElementById("email").value;
	thephone = document.getElementById("phone1").value + document.getElementById("phone2").value + document.getElementById("phone3").value;
	thephone = thephone.replace(onlynumbers,"")
	var message = "";
	if (!validateemail(theemail)) return false;
	if (isNaN(parseInt(thephone)) || thephone.length < 10) {
		message += "\nPlease Enter a Valid Phone Number.";
	}
	if(document.getElementById("needtoship").value) {
		thesname = document.getElementById("sname").value;//.replace(/^\s+|\s+$/g, "");
		thesline1 = document.getElementById("sline1").value;//.replace(/^\s+|\s+$/g, "");
		thescity = document.getElementById("scity").value;//.replace(/^\s+|\s+$/g, "");
		thesstate = document.getElementById("sstate").value;//.replace(/^\s+|\s+$/g, "");
		thespostal = document.getElementById("spostal").value;//.replace(/^\s+|\s+$/g, "");
		if (!document.getElementById("shipping1").checked && !document.getElementById("shipping2").checked && !document.getElementById("shipping3").checked) message += "You Must Choose a Shipping Option.";
		if (document.getElementById("newshipping").checked) {
			if (thesname.replace(nospaces, "") == "") message += "\nThe Shipping Name Can Not Be Empty.";
			if (thesline1.replace(nospaces, "") == "") message += "\nThe Street Address Line 1 Can Not Be Empty.";
			if (thescity.replace(nospaces, "") == "") message += "\nThe City Can Not Be Empty.";
			if (thesstate.replace(nospaces, "") == "") message += "\nThe State Can Not Be Empty.";
			if (thespostal.replace(nospaces, "") == "") message += "\nThe Postal / Zip Code Can Not Be Empty.";
		}
	}
	return handlereturn(message);
}
function validatecheckout2() {
	theform = document.checkoutform;
	thebfname = theform.name_on_card1.value;//.replace(/^\s+|\s+$/g, "");
	theblname = theform.name_on_card2.value;//.replace(/^\s+|\s+$/g, "");
	thebline1 = document.getElementById("bline1").value;//.replace(/^\s+|\s+$/g, "");
	thebcity = document.getElementById("bcity").value;//.replace(/^\s+|\s+$/g, "");
	thebstate = document.getElementById("bstate").value;//.replace(/^\s+|\s+$/g, "");
	thebpostal = document.getElementById("bpostal").value;//.replace(/^\s+|\s+$/g, "");
	var message = "";
	if (document.getElementById("newbilling").checked) {
		if (!checkCCNumber(document.getElementById("card_number"),document.getElementById("name_on_card1"),document.getElementById("name_on_card2"),document.getElementById("exp_month"),document.getElementById("exp_year"),document.getElementById("card_type"))) return false;
		if (!document.getElementById("useshipping").checked) {
			if (thebfname.replace(nospaces, "") == "") message += "\nThe Billing First Name Can Not Be Empty.";
			if (theblname.replace(nospaces, "") == "") message += "\nThe Billing Last Name Can Not Be Empty.";
			if (thebline1.replace(nospaces, "") == "") message += "\nThe Street Address Line 1 Can Not Be Empty.";
			if (thebcity.replace(nospaces, "") == "") message += "\nThe City Can Not Be Empty.";
			if (thebstate.replace(nospaces, "") == "") message += "\nThe State Can Not Be Empty.";
			if (thebpostal.replace(nospaces, "") == "") message += "\nThe Postal / Zip Code Can Not Be Empty.";
		}
	}
	return handlereturn(message);
}
function validatecontact() {
	theform = document.email;
	thename = document.getElementById("name").value;//.replace(/^\s+|\s+$/g, "");
	theemail = document.getElementById("email").value;//.replace(/^\s+|\s+$/g, "");
	themsg = document.getElementById("message").value;//.replace(/^\s+|\s+$/g, "");
	var message = "";
	if (!validateemail(theemail)) return false;
	if (thename.replace(nospaces, "") == "") message += "\nYou Must Enter A Name.";
	if (themsg.replace(nospaces, "") == "") message += "\nYou Must Enter A Message.";
	return handlereturn(message);
}