// FORM VALIDATION FUNCTIONS

function isEmail(str){
	var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/	
	return reg.test(str);
}

function isEmpty(str){
	if( str == "" ){return true; }else{return false; }
	//	var reg = /^((\.+)(\s*)(\.+))+$/;
	//	return reg.test(str);
}

function isNumber(str){
	var reg = /^\d+$/;
	return reg.test(str); 
	// var rval = isNaN(str)?false:true;
	// return rval;
}

function isDecimal(str){
	var reg = /^(\d+)?\.\d+$/;
	return reg.test(str); 
}

function isMoney(str){
	var reg = /^\d+\.\d{2}$/;
	return reg.test(str); 
	// var rval = isNaN(str)?false:true;
	// return rval;
}

function isPhone(str){
	//var reg = /^\(?(\d{3})\)?[ \.\-]?(\d{3})[\.\-]?(\d{4})$/
	var reg = /^\(?(\d{3})\)?[ \.\-]?(\d{3})[\.\-]?(\d{4})([ ]?((e|E)(x|X)(t|T))[\.]?[ ]?(\d{2,5}))?$/
	return reg.test(str);
}

function formatPhone(str){	
	var reg = /^\(?(\d{3})\)?[ \.\-]?(\d{3})[\.\-]?(\d{4})([ ]?((e|E)(x|X)(t|T))[\.]?[ ]?(\d{2,5}))?$/
	var rval = reg.exec(str);
	var ret = "";
		ret += "(" + rval[1] + ") ";
		ret += rval[2];
		ret += "-";
		ret += rval[3];
		if( rval[4] ){
			ret += rval[4];
		}		
		return ret;
}

function isSocial(str){
	var reg = /^\d{3}-\d{2}-\d{4}$/; // WITH AREA CODE
	return reg.test(str);
}

function isZip(str){
	var rval = false;
	var reg = /^\d{5}-\d{4}$/; // WITH AREA CODE WITH EXTENSION
	var reg2 = /^\d{5}$/; // WITH AREA CODE
	if( reg.test(str) || reg2.test(str) ){ rval = true; } else {rval = false; }
	return rval;
}

function radioChecked(obj){
	for( var i = 0; i < obj.length; i++ ){
		if( obj[i].checked ){ return true; }
	}
	return false;
}

function formError( message, obj ){
	var out = "Form is incomplete\n============================\n\n";
	out += message;
	alert( out );
	if( obj ){
		obj.focus(); 
	}
	return;
}

function validationError ( err, timeStamp ){
		errSep = "\n*****************************************************\n";
		errTitle = "                  JavaScript Validation Error";
		alert( errTitle + errSep + "\n" + err + "\n" + errSep + "\n" + timeStamp );
}

function showNewImage(fieldObj, imageObj){
	imageObj = MM_findObj( imageObj );
	imageObj.src = fieldObj.value;
}

function cartQTY( obj ){
	if( obj && document.getElementById && document.getElementById( obj ) ){
		obj = document.getElementById( obj );
		if( isNumber( obj.value ) && obj.value > 0 ){ return true; 	}
		else{ obj.focus(); obj.select(); return false; }
	}else{
		return true;	
	}
}

function stripSpaces( str ){
	return str.replace(/ /g, "");
}

function isAlphaNumeric(str){
	var reg = /^[0-9A-Za-z]+$/;
	return reg.test(str); 
}

function ValidateRegister(f){
	
	var er = "";
	var erObj = false;

	if( f.login.value == "" ){
		er += " - Login ID is a required field \n";
		if( !erObj ){ erObj = f.login; }
	} else {
		if( !isAlphaNumeric( f.login.value ) ){
			er += " - Login ID must contain alpha-numeric characters only \n";
			if( !erObj ){ erObj = f.login; }
		}
	}
	
	if( f.password.value == "" ){
		er += " - Password is a required field \n";
		if( !erObj ){ erObj = f.password; }
	}

	if( f.password.value != "" && f.password2.value == "" ){
		er += " - Please re-type password \n";
		if( !erObj ){ erObj = f.password2; }
	}

	if( f.password.value != "" && f.password2.value != "" && f.password.value != f.password2.value ){
		er += " - Passwords must match \n";
		if( !erObj ){ erObj = f.password2; }
	}

	if( f.firstname.value == "" ){
		er += " - First Name is a required field \n";
		if( !erObj ){ erObj = f.firstname; }
	}

	if( f.lastname.value == "" ){
		er += " - Last Name is a required field \n";
		if( !erObj ){ erObj = f.lastname; }
	}
	
	if( !isEmail( f.email.value ) ){
		er += " - Email is a required field \n";
		if( !erObj ){ erObj = f.email; }
	}
	
	if( isEmail( f.email.value ) && !isEmail( f.email2.value ) ){
		er += " - Please re-type email \n";
		if( !erObj ){ erObj = f.email2; }
	}

	if( isEmail( f.email.value ) && isEmail( f.email2.value ) && f.email.value != f.email2.value ){
		er += " - Emails must match \n";
		if( !erObj ){ erObj = f.email2; }
	}

	if( f.phone.value == "" ){
		er += " - Phone is a required field \n";
		if( !erObj ){ erObj = f.phone; }
	}

	if( f.b_address.value == "" ){
		er += " - Billing Address is a required field \n";
		if( !erObj ){ erObj = f.b_address; }
	}

	if( f.b_city.value == "" ){
		er += " - Billing City is a required field \n";
		if( !erObj ){ erObj = f.b_city; }
	}

	if( f.b_country.value == "" ){
		er += " - Billing Country is a required field \n";
		if( !erObj ){ erObj = f.b_country; }
	}

	if( f.s_address.value == "" && !f.chkSameAddr.checked ){
		er += " - Shipping Address is a required field \n";
		if( !erObj ){ erObj = f.s_address; }
	}

	if( f.s_city.value == "" && !f.chkSameAddr.checked ){
		er += " - Shipping City is a required field \n";
		if( !erObj ){ erObj = f.s_city; }
	}

	if( f.s_country.value == "" && !f.chkSameAddr.checked ){
		er += " - Shipping Country is a required field \n";
		if( !erObj ){ erObj = f.s_country; }
	}

	if( er == "" ){ return true; }else{ formError( er, erObj ); return false; }
}

function ValidateGcert(f){
	
	var er = "";
	var erObj = false;

	if( f.amount.value == "" ){
		er += " - Amount is a required field \n";
		if( !erObj ){ erObj = f.amount; }
	}

	if( !isNumber(f.amount.value) && !isMoney(f.amount.value) ){
		er += " - Amount requires a numeric value only \n";
		if( !erObj ){ erObj = f.amount; }
	}

	if( f.fname.value == "" ){
		er += " - Recipient's First Name is a required field \n";
		if( !erObj ){ erObj = f.fname; }
	}

	if( f.lname.value == "" ){
		er += " - Recipient's Last Name is a required field \n";
		if( !erObj ){ erObj = f.lname; }
	}

	if( (f.gift_to_email.value == "" || !isEmail(f.gift_to_email.value)) && ( f.gift_to_address.value == "" && f.gift_to_city.value == "" && f.gift_to_state.value == "" && f.gift_to_zip.value == "" ) ){
		er += " - Recipient's Email is a required field \n";
		if( !erObj ){ erObj = f.gift_to_email; }
	}

	if( er == "" ){ return true; }else{ formError( er, erObj ); return false; }
}

function formError( message, obj ){
	var out = "Form is incomplete\n============================\n\n";
	out += message;
	alert( out );
	if( obj ){
		obj.focus(); 
	}
	return;
}