/*

Error handling functions for form validation. TrŽs cool, man.

*/

var error = new Array();
var field = new Array();

function pushErr(err,fld) {
	error[error.length] = err;
	field[field.length] = fld;
	return;
}

function clearErr(form) {

	for (i=0;i<form.elements.length;i++) {
		form.elements[i].style.backgroundColor='#ffffff';
	}

}

function showErr() {

	if (arguments.length > 0) str = arguments[0] + "\n";
	else str = '';

	if (arguments.length > 1) color = arguments[1];
	else color = 'pink';

	if (error.length == 0) return true;

	for(i=0;i<error.length;i++) {
		str += error[i] + "\n";
	}
	alert(str);

	for(i=0;i<field.length;i++) {
		field[i].style.backgroundColor=color;
	}

	field[0].focus();
	error = new Array();
	field = new Array();
	return false;
}




function validLogin(form) {

	clearErr(form);

	if (isEmpty(form.first_name.value)) {		
		pushErr("Please enter a first name.",form.first_name)
	}
	if (isEmpty(form.last_name.value)) {		
		pushErr("Please enter a last name.",form.last_name)
	}
	if (isEmpty(form.address_1.value)) {		
		pushErr("Please enter an address.",form.address_1)
	}
	if (isEmpty(form.city.value)) {		
		pushErr("Please enter a city.",form.city)
	}
	if (isEmpty(form.state.value)) {		
		pushErr("Please enter a state.",form.state)
	}
	if (isEmpty(form.postal_code.value)) {		
		pushErr("Please enter a postal code.",form.postal_code)
	}
	if (isEmpty(form.country.value)) {		
		pushErr("Please enter a country.",form.country)
	}
	if (isEmpty(form.phone_day.value) && isEmpty(form.phone_eve.value)) {		
		pushErr("Please enter at least one phone number.",form.phone_day)
	}
	if (isEmpty(form.dob.value)) {		
		pushErr("Please enter date of birth.",form.dob)
	}

	return showErr("The following must be addressed before submitting:");

}

function validCheckoutLogin(form) {

	clearErr(form);
	form.supportscript.value = true;
	if (form.email) {
		if (isEmpty(form.email.value)) {		
			pushErr("Please enter your email address.",form.email)
		} else if(!validEmail(form.email.value)){
			pushErr("Please enter a valid email address.",form.email)
		}
	}
	if (form.password) {
		if (isEmpty(form.password.value)) {		
			pushErr("Please enter your password.",form.password)
		}	
	}
	return showErr("The following must be addressed before submitting:");
}

function validCheckoutBillingAddr(form,min_pw_len) {
	if (arguments.length < 2) {
		var require_email = true;
	}

	clearErr(form);
	
	// elements array
	var x = form.elements;
	var bill_country = '';
	var ship_country = '';
	for (var i=0; i<x.length; i++) {
		
		if (x[i].id == 'bill_first_name' && !x[i].value) {		
			pushErr("Please enter your billing first name.",x[i]);
		}
		if (x[i].id == 'bill_last_name' && !x[i].value) {		
			pushErr("Please enter your billing last name.",x[i]);
		}
		if (x[i].id == 'bill_address_1' && !x[i].value) {		
			pushErr("Please enter your billing address.",x[i]);
		}
		if (x[i].id == 'bill_city' && !x[i].value) {		
			pushErr("Please enter your billing city.",x[i]);
		}
		if (x[i].id == 'bill_state'){

			for (var j=0; j<x.length; j++) {
				if (x[j].id == 'bill_country') {
					var bill_country = x[j].options[x[j].selectedIndex].value;
					break;
				}
			}
			if (bill_country == 'US' && !x[i].value) {
				pushErr("Please enter your two-letter billing state.",x[i]); 			
			} else if (bill_country == 'US') {
	 			if (!isState(x[i].value)) {
					pushErr("Please enter a valid two-letter billing state abbreviation.",x[i]); 			
	 			}
			}
 		} 		
 		// http://en.wikipedia.org/wiki/Postal_code
		if ((x[i].id == 'bill_postal_code' && !x[i].value) && (bill_country!='HK' && bill_country!='MO')) {		
			pushErr("Please enter your billing postal/zip code.",x[i]);
		}
		if (x[i].id == 'bill_postal_code' && (x[i].value.length < 5) && bill_country == 'US') {		
			pushErr("Please enter a minimum 5-digit billing postal/zip code.",x[i]);
		}
		if (x[i].id == 'bill_phone_day' && !x[i].value) {		
			pushErr("Please enter your billing phone number.",x[i]);
		}		
		if (x[i].id == 'bill_email') {
			if (!validEmail(x[i].value,true)) {
				pushErr("Please enter a valid email address.",x[i]);
			}
		}	
		if (x[i].id == 'password') {
			var pw;
			pw = x[i].value;
			
			for (var j=0; j<x.length; j++) {
				if (x[j].id == 'verify_password') {
					var verify;
					verify = x[j].value;
					break;
				}
			}
			if (pw.length == 0) {
				pushErr("Please create a password at least "+min_pw_len+" characters in length.",x[i]);
			} else if (pw.length < min_pw_len) {
				pushErr("For your protection, password must be a minimum of "+min_pw_len+" characters in length.",x[i]);
			} else if (pw != verify) {
				pushErr("Your password and password verification do not match.",x[i]);
				field[field.length] = x[j];
			}
			if (verify.length==0) {
				field[field.length] = x[j];
			}
		}	
		if (!form.same_as_billing.checked) {	

 			if (x[i].id == 'ship_first_name' && !x[i].value){
 				pushErr("Please enter your shipping first name.",x[i]);
 			}
 			if (x[i].id == 'ship_last_name' && !x[i].value){
 				pushErr("Please enter your shipping last name.",x[i]);
 			}
 			if (x[i].id == 'ship_address_1' && !x[i].value){
 				pushErr("Please enter your shipping address.",x[i]);
 			}
 			if (x[i].id == 'ship_city' && !x[i].value){
 				pushErr("Please enter your shipping city.",x[i]);
 			}
 			if (x[i].id == 'ship_state'){

				for (var j=0; j<x.length; j++) {
					if (x[j].id == 'ship_country') {
						var ship_country = x[j].options[x[j].selectedIndex].value;
						break;
					}
				}
				if (ship_country == 'US' && !x[i].value) {
					pushErr("Please enter your two-letter shipping state.",x[i]); 			
				} else if (ship_country == 'US') {
					if (!isState(x[i].value)) {
						pushErr("Please enter a valid two-letter shipping state abbreviation.",x[i]); 			
					}
				}
 			}
 			if (x[i].id == 'ship_postal_code' && !x[i].value){
 				pushErr("Please enter your shipping postal/zip code.",x[i]);
 			} 		
			if (x[i].id == 'ship_postal_code' && (x[i].value.length < 5) && bill_country == 'US') {		
				pushErr("Please enter a minimum 5-digit shipping postal/zip code.",x[i]);
			}
			if (x[i].id == 'ship_phone_day' && !x[i].value) {		
				pushErr("Please enter your shipping phone number.",x[i]);
			}		
 		}
	}	
	
	return showErr("The following must be addressed before submitting:");
}

function validForgotPassword(form){
	clearErr(form);

	if (isEmpty(form.email_address.value)) {		
			pushErr("Please enter your email address.",form.email_address)
	}
	return showErr("The following must be addressed before submitting:");
}

function validNewShipAddr(form) {

	clearErr(form);

	if (form.payment_plan.selectedIndex == 0) {		
		pushErr("Please select a payment plan.",form.payment_plan)
	}

	return showErr("");

}

function validBillingCC(form,accepted) {
	clearErr(form);
	var x = form.elements;
	var cc_exp_mo;
	var cc_exp_mo_obj;
	var cc_exp_yr;
	var cc_exp_yr_obj;
	var validate_cvv;

	// Check to see if validate_cvv is set first
	for (var i=0; i<x.length; i++) {
		//alert(x[i].name + ' ' + x[i].value);
		if (x[i].id == 'validate_cvv'){
			validate_cvv = x[i].value;
		}
	}
	
	
	for (var i=0; i<x.length; i++) {
		if (x[i].id == 'cc_num' && isEmpty(x[i].value)){
			pushErr ("Please enter your credit card number.", x[i]);
		} else if (x[i].id == 'cc_num' && !isCreditCard(x[i].value)){
			pushErr ("The credit card number you entered is not a valid card number.", x[i]);
		} else if (x[i].id == 'cc_num') {
			var cc_type;
			cc_type = false;
			for(var j=0; j<accepted.length; j++){
				if (accepted[j]=='visa' && isVisa(x[i].value)) {
					cc_type = 'Visa';
					break;
				} else if (accepted[j]=='mastercard' && isMasterCard(x[i].value)) {
					cc_type = 'MasterCard';
					break;
				} else if (accepted[j]=='amex' && isAmericanExpress(x[i].value)) {
					cc_type = 'American Express';
					break;
				} else if (accepted[j]=='discover' && isDiscover(x[i].value)) {
					cc_type = 'Discover';
					break;
				} else if (accepted[j]=='dinersclub' && isDinersClub(x[i].value)) {
					cc_type = 'Diners Club';		
					break;
				} else if (accepted[j]=='jcb' && isJCB(x[i].value)) {
					cc_type = 'JCB';
					break;
				}
			}
			if (cc_type==false){
				pushErr ("The credit card number you entered is not accepted by the merchant.", x[i]);		
			}
		} else if (x[i].id == 'cc_exp_mo'){
			// subtract 0 to cast it as an integer
			cc_exp_mo = x[i].options[x[i].selectedIndex].value - 0;
			cc_exp_mo_obj = x[i];
		} else if (x[i].id == 'cc_exp_yr'){
			cc_exp_yr = x[i].options[x[i].selectedIndex].value - 0;
			cc_exp_yr_obj = x[i];
		} else if (x[i].id == 'name' && isEmpty(x[i].value)){
			pushErr ("Please enter the name as it appears on the credit card.", x[i]);
		} else if (validate_cvv == '1') {
			if (x[i].id == 'cc_cvv' && isEmpty(x[i].value)){
				pushErr ("Please enter your credit card's security code.", x[i]);
			} else if (x[i].id == 'cc_cvv' && !isPosInteger(x[i].value)){
				pushErr ("Your credit card's security code should be a three- or four-digit number.", x[i]);
			} else if (x[i].id == 'cc_cvv' && (x[i].value.length != 3) && (x[i].value.length != 4)){
				pushErr ("Your credit card's security code should be a three- or four-digit number.", x[i]);
			}
		}
	}

	var today = new Date( );
	var year = today.getFullYear( ) - 2000;
	var month = today.getMonth( ) + 1;

	if ((year > cc_exp_yr) || (year == cc_exp_yr && month > cc_exp_mo)) {
		pushErr ("Please enter a valid expiration date.",cc_exp_mo_obj);
		field[field.length] = cc_exp_yr_obj;
	}
	
	return showErr("The following must be addressed before summitting:");
}


function validContactForm(form){
	clearErr(form);
	
	if (isEmpty(form.name.value)) {		
			pushErr("Please enter your name.",form.name)
	}
	if (isEmpty(form.email.value)) {		
			pushErr("Please enter your email address.",form.email)
	} else if (!validEmail(form.email.value)) {		
			pushErr("Please enter a valid email address.",form.email)
	}
	if (isEmpty(form.subject.value)) {		
			pushErr("Please enter a subject.",form.subject)
	}
	if (isEmpty(form.comments.value)) {		
			pushErr("Please enter your comments.",form.comments)
	}	
	return showErr("The following must be addressed before submitting:");	
}

function validLoginForm(form){

	if (isEmpty(form.email.value)) {		
			pushErr("Please enter your email address.",form.email)
	}
	if (isEmpty(form.password.value)) {		
			pushErr("Please enter your password.",form.password)
	}
	return showErr("The following must be addressed before submitting:");
}

function validShipStatus(status,qtyobj,qty,shipped) {
	if (status.value == 'Pending' ||
		status.value == 'Back-ordered' ||
		status.value == 'Cancelled' ||
		status.value == 'Hold') {
		qtyobj.value = 0;
		return true;
	} else if (status.value == 'Complete') {
		qtyobj.value = qty - shipped;
		return true;
	}

	return false;
}

function validNewAccount(form,pass_min_len) {
	clearErr(form);

	if (isEmpty(form.first_name.value)) {		
		pushErr("Please enter your first name.",form.first_name)
	} 
	if (isEmpty(form.last_name.value)){
		pushErr("Please enter your last name.",form.last_name)
	} 	
	
	if(pass_min_len>0){
		if(!isEmpty(form.password.value) && isEmpty(form.confirm_password.value)){
			pushErr("Please enter the confirmation password.",form.confirm_password);
		}
		if(isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)){
			pushErr("Please enter your password.",form.password);
		}
		if((!isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)) && form.password.value != form.confirm_password.value){
			pushErr("Your passwords do not match.",form.password);
		}	
		if((!isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)) && form.password.value.length<pass_min_len){
			pushErr("Your password must be at least "+pass_min_len+" characters long.",form.password);
		}
	}
	
	return showErr("The following must be addressed before submitting:");
}
function validNewWholesaleAccount(form,pass_min_len) {

	clearErr(form);
		
	if (isEmpty(form.company_name.value)) {		
		pushErr("Please enter your company name.",form.company_name)
	}
	if (isEmpty(form.first_name.value)) {		
		pushErr("Please enter your first name.",form.first_name)
	} 
	if (isEmpty(form.last_name.value)){
		pushErr("Please enter your last name.",form.last_name)
	}
	if (isEmpty(form.address_1.value)) {		
		pushErr("Please enter your street address.",form.address_1)
	}	
	if (isEmpty(form.city.value)) {		
		pushErr("Please enter your city.",form.city)
	}
	if (isEmpty(form.state.value)) {		
		pushErr("Please enter your state.",form.state)
	}
	if (isEmpty(form.postal_code.value)) {		
		pushErr("Please enter your postal code.",form.postal_code)
	}
	if (isEmpty(form.phone_day.value)) {		
		pushErr("Please enter your phone number.",form.phone_day)
	}	
	if(pass_min_len>0){
		if(!isEmpty(form.password.value) && isEmpty(form.confirm_password.value)){
			pushErr("Please enter the confirmation password.",form.confirm_password);
		}
		if(isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)){
			pushErr("Please enter your password.",form.password);
		}
		if((!isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)) && form.password.value != form.confirm_password.value){
			pushErr("Your passwords do not match.",form.password);
		}	
		if((!isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)) && form.password.value.length<pass_min_len){
			pushErr("Your password must be at least "+pass_min_len+" characters long.",form.password);
		}
	}

	return showErr("The following must be addressed before submitting:");
}

function validPostAuth(form,auth) {
	clearErr(form);

	if (isEmpty(form.amount.value) || form.amount.value==0) {		
		pushErr("Please enter an amount.",form.amount)
	} 
	if (form.amount.value>auth){
		form.amount.value = auth;
		pushErr("You may not postauth for more than "+auth+", which was the authorization amount.",form.amount)
	} 	

	return showErr("The following must be addressed before submitting:");
}


function validConfirm(form,agree) {
	clearErr(form);
	var x = form.elements;
	for (var i=0; i<x.length; i++) {
		if (x[i].id == 'cc_cvv' && isEmpty(x[i].value)){
			pushErr ("Please enter your credit card's security code.", x[i]);
		} else if (x[i].id == 'cc_cvv' && (!isPosInteger(x[i].value) || (x[i].value.length < 3))){
			pushErr ("Your credit card's security code should be a three- or four-digit number.", x[i]);
		}
		if (x[i].id == 'checkout_confirm_i_agree' && !x[i].checked){
			pushErr ("Please check the box labelled: \n\""+agree+"\"\nbefore proceeding.", x[i]);
		}
		
	}

	return showErr();
}
function validReviewPost(form) {

	clearErr(form);
		
	if (isEmpty(form.review_text.value)) {		
		pushErr("Please enter your review text.",form.review_text)
	}
	if (form.rating_score.options[form.rating_score.selectedIndex].value < 0) {		
		pushErr("Please select a rating score.",form.rating_score)
	} 

	return showErr("The following must be addressed before submitting:");
}