// ========================================================
// VERIFICATION FUNCTIONS

function validate_email(field,alerttxt) {
	with (field) {
		// THERE IS A VALUE NOW CHECK IF IT IS VALID
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
			
		if (apos<1||dotpos-apos<2) {
			alert(alerttxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_field(field,alerttxt) {
	with (field) {
  		if (value==null||value=="") {
  			alert(alerttxt);
			return false;
  		} else {
  			return true;
  		}
	}
}

function validate_dropdown(field,alerttxt) {
	with (field) {
		// MAKE SURE THE USER SELECTED A VALUE
  		if (options[selectedIndex] == options[0]) {
  			alert(alerttxt);
			return false;
  		} else {
  			return true;
  		}
	}
}

function validate_number(field,alerttxt){
	with(field) {
		// CHECK IF THERE IS A VALUE
		if (value==null||value=="") {
  			alert(alerttxt);
			return false;
  		} else {
			// THERE IS A VALUE NOW CHECK IF ITS VALID
			valid_chars = "0123456789.-";
			
			var char_status = "valid";
			// LOOP THROUGH STRING TO FIND VALID CHARACTERS
			for(i=0;i<value.length;i++){
				if(valid_chars.indexOf(value.charAt(i)) < 0){
					// DETECT THE INVALID CHARACTER VALUE TO REMOVE IT
					var invalid_character = value.charAt(value.length - 1);
				
					// AN INVALID CHARACTER WAS FOUND, ALERT USER
					char_status = "invalid";
			
				}
			}
			if(char_status == "invalid") {
				alert(alerttxt);
				return false;
			} 
		}
	}
}
// ============= CALL ALL VALIDATION FUNCTIONS ===================


function validate_form(thisform) {
	with (thisform) {
		
		if (validate_field(firstname,"Please Enter Your First Name!")==false) {
			firstname.focus();
			return false;
		}
		
		if (validate_field(lastname,"Please Enter Your Last Name!")==false) {
			lastname.focus();
			return false;
		}
		
		if (validate_field(ci_billaddr1,"Please Enter a Street Address!")==false) {
			ci_billaddr1.focus();
			return false;
		}
		
		if (validate_field(ci_billcity,"Please Enter a City!")==false) {
			ci_billcity.focus();
			return false;
		}
		
		if (validate_dropdown(ci_billstate,"Please Select a State!")==false) {
			ci_billstate.focus();
			return false;
		}
		
		if (validate_number(ci_billzip,"Please Enter a Valid Zip Code!")==false) {
			ci_billzip.focus();
			return false;
		}
		
		
		if (validate_email(ci_email,"Please Enter a Valid Email!")==false) {
			ci_email.focus();
			return false;
		}
		
		if (validate_field(ci_phone,"Please Enter a Phone Number!")==false) {
			ci_phone.focus();
			return false;
		}
		
		if (validate_field(ccname,"Please Enter the name on the Credit Card!")==false) {
			ccname.focus();
			return false;
		}
		
		if (validate_number(ccnum,"Please Enter a Credit Card Number!")==false) {
			ccnum.focus();
			return false;
		}
		
	}
}