// VERIFY NOT ALL SPACES
function notSpaces(word)
{
	//alert("in notSpaces = "+word);
	w = new String(word);
	len = w.length;
	cnt = 0;
	spc = 0;
	while(cnt < len)
	{
		c = w.charAt(cnt);
		if(c.match(/\s/)){ spc++; }
		cnt++;
	}
	if(spc == len)return 0;
	return 1;
}

// VERIFY ZIP
function goodZip( postalCode )
{
	//# not present or ""?
	if (!postalCode || (postalCode.length == 0) )
	{
		return 0;
	}

	//# contain any wacky chars?
	for( var ii = 0; ii < postalCode.length; ii++ )
	{
		if( postalCode.charCodeAt(ii) > 127 )
		{
  		return 0;  //# invalid characters
		}
	}

	// //# no whitespace allowed
	// if( postalCode.match(/\s/) )
	// {
	//   return( -30 );  //# invalid format
	// }

	//# can only contain letters, numbers and dashes
	//if( postalCode.match(/[^a-zA-Z0-9\-]/) )
	//{
	//	return 0;  //# invalid format
	//}

	//# must be at least 5 characters long
	if( postalCode.length < 5 )
	{
		return 0;  //# invalid format
	}

	return( 1 ); //# appears to be valid
}

// VERIFY NO SPACES
function hasspace(word)
{
	//alert("in hasspace = "+word);
	w = new String(word);
	if(w.match(/ /)){ return 1; }
	else { return 0; }

}

//# FOR USE IN REGISTRATION
function timezone()
{
	now = new Date();
 	gmt_offset = Math.abs(now.getTimezoneOffset());
 	day_offset = now.getUTCDate() - now.getDate();
 	if( day_offset != 0 )
 	{
        	 if( day_offset > 0 )
         	{
                	 gmt_offset *= -1;
         	}
 	}
 	else
 	{
        	 hour_offset = now.getUTCHours() - now.getHours();
         	if( hour_offset != 0 )
         	{
                 if( hour_offset > 0 )
                 {
                         gmt_offset *= -1;
                 }
         	}
 	}
	return gmt_offset;
}

function checkCountryStateZipPhone() {
 	var err = '';
	var errMsg = '';
	var el = document.registration.r_country;
	var cntryCode = el.options[el.selectedIndex].value;

	//state/provice/region and zip/postal code are required
	el = document.registration.r_state;
	if (typeof el == "undefined") {
		el = document.registration.state;
	}
	err = validate_state_address( el.value, cntryCode ); //Function lives in ../shared/validate_state_address.js
	if(err) {
		switch (err) {
			case -10:
				errMsg = validate_js_text[2071]; // "Please enter a valid State/Province/Region."
				break;
                        case -20:
                                errMsg = validate_js_text[5023]; // "Please enter a valid State (ie. CA, WA, etc).";
                                break;
                        case -30:
                                errMsg = validate_js_text[5024]; // "Please enter a valid Province (ie. AB, BC, etc).";
                                break;
			default:
				errMsg = validate_js_text[5025]; // "Validation error in State/Province/Region field.";
		}
                el.focus();
		return errMsg;
	}
	el.value = validate_state_address( el.value, cntryCode, "returnStateCode" ); //Function lives in ../shared/validate_state_address.js

	el = document.registration.zip;
	err = validate_postal_code(el.value, cntryCode); //Function lives in ../shared/validate_postal_code.js
        if(err) {
                switch (err) {
                         case -10:
                                errMsg = validate_js_text[2072]; // "Zip or Postal Code is required"
                                break;
                         case -20:
                                errMsg = validate_js_text[2073]; // "Invalid ZIP/Postal Code"
                                break;
                         case -30:
                                errMsg = validate_js_text[2073]; // "Invalid ZIP/Postal Code"
                                break;
                         default:
                                errMsg = validate_js_text[5026]; // "Validation error in ZIP/Postal Code field.";
                }
		el.focus();
                return errMsg;
        }

	// Phone is not required. Only validate if a phone number is entered.
	el = document.registration.homephone;
	if (el.value.length > 0) {
        	err = validate_phone_number(el.value, cntryCode); //Function lives in ../shared/validate_phone_number.js
	        if(err) {
        	        switch (err) {
	                         case -10:
        	                        errMsg = validate_js_text[5027]; // "Please enter a valid Phone number.";
                	                break;
	                         case -20:
        	                        errMsg = validate_js_text[5028]; // "Invalid Phone number.";
                	                break;
	                         case -30:
        	                        errMsg = validate_js_text[5028]; // "Invalid Phone number.";
                	                break;
	                         default:
        	                        errMsg = validate_js_text[5029]; // "Validation error in Phone field.";
                	}
	                el.focus();
        	        return errMsg;
		}
	}
	return err;
}

function validate(notedit) {

		var r = document.registration.elements.length;
		var cnt = 0;
		var el;
		while(cnt < r )
		{
			el = document.registration.elements[cnt];
			if(el.name == "email")
			{
				if (el.value.length > 1)
				{
					var isok = validate_email_address(el.value);
 					if(isok == 20) {
		 				alert(validate_js_text[2062]);
						return;
 					}
 					if(isok == 30) {
		 				alert(validate_js_text[2063]);
						return;
 					}
				}
			}
			else if( el.name.indexOf("r_") == 0 && el.name != 'r_state' && el.name != 'r_country' )
			{
				if(el.value.length < 1 || !notSpaces(el.value))
				{
					alert(validate_js_text[2064]);
					return;
				}
			}
			cnt++;
		}
	if(notedit)
	{
		el = document.registration.login;
		if(el.value.length < 3 || el.value.length > 33 )
		{
	 		alert(validate_js_text[2065]);
			return;
		}
		if(el.value.match(/[?{}\[\]()%&*/\\"':!]/) != null)
		{
			alert("Username must not contain any of the following characters:\n ? ( ) [ ] { } % & * \\ / \" \' :");
			return;
		}
		/* var haschar = containsNonDigits(el.value);
		if(haschar == -1)
		{
			alert("User Name needs to contain a letter");
			return;
		}*/
		var isOk1 = validate_password(el.value);

		/* if(isOk1 == 20 || hasspace(document.registration.login.value) || */

		if(isOk1 == 20)
		{
			alert (validate_js_text[2066]);
			return;
		}
		el = document.registration.password;
		if(el.value.length < 3 || el.value.length > 33)
		{
			alert(validate_js_text[2067]);
			return;
		}
		if(el.value != document.registration.vpassword.value)
		{
			alert(validate_js_text[2026]);
			return;
		}
		/* var haschar2 = containsNonDigits(el.value);
		if(haschar2 == -1 )
		{
			alert(validate_js_text[2068]);
			return;
		} */
		var isOk2 = validate_password(el.value);

		/* if(isOk2 == 20 || hasspace(el.value)|| */

		if(isOk2 == 20 )
		{
			alert (validate_js_text[2069]);
			return;
		}

		if(document.registration.r_country)
		{
			el = document.registration.r_country;
			if( el.options[el.selectedIndex].value == -1)
			{
				alert(validate_js_text[2070]);
				return;
			}
		}

		if(document.registration.time_zone)
		{
			document.registration.time_zone.value = timezone();
		}
	}

	if(document.registration.r_country)
	{
		var err = '';
		err = checkCountryStateZipPhone();
		if( err ) {
			alert( err );
		return;
		}
	}

	document.registration.register.value = "update";
	document.registration.submit();
}
function back() { parent.window.history.back(); }
