function validateLogin(elForm) {
	var valid = false;

	var emailField = elForm.elements["email"];
	if ( emailField )
		valid = verifyEmail( emailField, "Please provide a valid email address." );
	
	var passField = elForm.elements["pwd"];
	if ( passField && valid )
		valid = valid && notEmpty( passField, "Please provide a password." );
	
	if ( valid ) elForm.submit();
}



function validateRegistration(elForm, bitNewUser) {
	var valid = false;
	
	var emailField = elForm.elements["user_email"];
	if ( emailField )
		valid = verifyEmail( emailField, "Please provide a valid email address." );
	
	var passField = elForm.elements["user_passwd"];
	if ( passField && valid )
		valid = valid && notEmpty( passField, "Please provide a password." );
	
	// new registrations must enter their password twice,
	// updaters can enter a new password
	if (bitNewUser) {
		var passField2 = elForm.elements["pwd2"];
		if (passField && valid)
			valid = valid && strCmp(passField, passField2, "The Passwords you entered do not match");
	}
	
	var firstnameField = elForm.elements["user_first"];
	if ( firstnameField && valid )
		valid = valid && notEmpty( firstnameField, "Please provide a first name." );
		
	var lastnameField = elForm.elements["user_last"];
	if ( lastnameField && valid )
		valid = valid && notEmpty( lastnameField, "Please provide a last name." );
	
	var addressField = elForm.elements["user_address"];
	if ( addressField && valid )
		valid = valid && notEmpty( addressField, "Please provide an address." );
	
	var cityField = elForm.elements["user_city"];
	if ( cityField && valid )
		valid = valid && notEmpty( cityField, "Please provide your city." );
		
	var stateField = elForm.elements["user_state"];
	if ( stateField && valid && (elForm.elements["user_country"] == "United States") )
		valid = valid && notEmpty( stateField, "Please provide your state." );
		
	var countryField = elForm.elements["user_country"];
	if ( countryField && valid )
		valid = valid && notEmpty( countryField, "Please provide your country." );

	if ( valid ) elForm.submit();
}



function validateCheckout(elForm) {
	var valid = true;
	
	var firstNameField = elForm.elements["x_First_Name"];
	if ( firstNameField && valid )
		valid = valid && notEmpty( firstNameField, "Please provide your first name." );
		
	var lastNameField = elForm.elements["x_Last_Name"];
	if ( lastNameField && valid )
		valid = valid && notEmpty( lastNameField, "Please provide your last name." );
	
	var addressField = elForm.elements["x_Address"];
	if ( addressField && valid )
		valid = valid && notEmpty( addressField, "Please provide your address." );
		
	var cityField = elForm.elements["x_City"];
	if ( cityField && valid )
		valid = valid && notEmpty( cityField, "Please provide your city." );
		
	var stateField = elForm.elements["x_State"];

	if ( stateField && valid && (elForm.elements["x_country"] == "United States") )
		valid = valid && notEmpty( stateField, "Please provide your state." );
	
	/*
	var zipField = elForm.elements["x_Zip"];
	if ( zipField && valid ) {
		valid = valid && isOfLength( zipField, 5, "Please provide a 5-digit numeric zip code." );
		valid = valid && isNumeric( zipField, true, "Zip codes must be numeric." );
	}
	
	var zip4Field = elForm.elements["zip4"];
	if ( zip4Field && (zip4Field.value.length > 0) && valid ) {
		valid = valid && isOfLength( zip4Field, 4, "Please provide a 4-digit numeric zip code extension." );
		valid = valid && isNumeric( zip4Field, true, "Zip code extensions must be numeric." );
	}
	*/
	var countryField = elForm.elements["x_Country"];
	if ( countryField && valid )
		valid = valid && notEmpty( countryField, "Please provide your country." );
	
	if ( valid ) {
		// elForm.action = "https://secure.authorize.net/gateway/transact.dll";
		// elForm.action = "http://localhost/citrix/message.php";
		
		// swap destination to checkout summary page
		// switchFormValue(elForm, "destination", "/checkout.php");
		elForm.submit();
	}

}


function notEmpty( elField, strErrorMsg ) {
	var valid = false;
	var theText = elField.value;
	
	if ( theText.length > 0 )
		valid = true;
	else alert(strErrorMsg);
	
	return valid;
}


function isOfLength( elField, intLength, strErrorMsg ) {
	var valid = false;
	var theText = elField.value;
	
	if ( theText.length == intLength )
		valid = true;
	else alert(strErrorMsg);
	
	return valid;
}


function isNumeric(elField, bitNeedInteger, strErrorMsg ) {
	var valid = false;
	var theText = elField.value;

	if (theText && theText.length > 0) {
		
		valid = true;
		
		// character, ascii pairs 
		// 0:48 , 9:57, -:45  , .:46
		var lowLim = (bitNeedInteger) ? 48 : 45 ;
		var hiLim = 57;
		var theCharCode = null;
		
		for (var j=0; j < theText.length; j++) {
			theCharCode = theText.charCodeAt(j);
			if ( (theCharCode > hiLim) || (theCharCode < lowLim) )
				valid = false;
		}
	}
	
	if (!valid) alert(strErrorMsg);
	return valid;
}


function verifyEmail(elField, strErrorMsg ) {
	var valid = false;
	var theText = elField.value;
	
	if ( (theText.length > 0) && (theText.indexOf(".", theText.indexOf("@")) != -1 ) )
		valid = true;
	else alert(strErrorMsg);
	
	return valid;
}



function isAlphaNumeric( testString ) {
	// a:97, z:90, A:97, Z:122, 0:48, 9:57 
	var j;	
	for (j=0; j < theFieldValue.length; j++) {
		theCharCode = theFieldValue.charCodeAt(j);
		if (!(
				((theCharCode >= 48) && (theCharCode <= 57)) ||
				((theCharCode >= 97) && (theCharCode <= 122)) ||
				((theCharCode >= 65) && (theCharCode <= 90))
			)) return 0; 
	}
	return 1;
}

function strCmp (str1, str2, errStr){
	if (str1.value != str2.value){
		alert (errStr);
		return 0;
	}else{
	
		return 1;
	}
}

function switchFormValue(elForm, strFieldName, strNewValue) {
	var theField = elForm.elements[strFieldName];
	if (theField && (strNewValue.length > 0)) theField.value = strNewValue;
}