/*
	Form Validation Functions
	$Id: validation.js 2059 2006-01-24 08:25:31Z cvs $
*/

// {{{ ValidateSignupForm ( string step )
function ValidateSignupForm ( step ) {
	var frm = document.forms['signup'];
	
	var strError = '';

	if ( frm ) {
		if ( step == 1 ) {
			// Validate Personal Information fields
			var pUser	= frm.elements['username'];
			var pPassword	= frm.elements['password'];
			var pCPassword	= frm.elements['confirm_password'];
			var pFName	= frm.elements['firstname'];
			var pLName	= frm.elements['lastname'];
			
			var pEmail	= frm.elements['email'];
			var pGender	= frm.elements['gender'];	// An array of two elements
			
			var pDate	= frm.elements['bd_day'];
			var pMonth	= frm.elements['bd_month'];
			var pYear	= frm.elements['bd_year'];
			
			var pCountry	= frm.elements['country'].options[frm.elements['country'].options.selectedIndex];
			var pPCode	= frm.elements['zip'];
			
			var pPlan	= frm.elements['ptype'];	// An array of elements
			var pConfirm	= frm.elements['agree'];
			
			if ( pConfirm.checked == false ) {
				strError = 'You must agree to the terms of service and select the checkbox below to proceed';
			}

			var pPlanChecked = 0;
			
			for ( i = 0; i < pPlan.length; i++ ) {
				if ( pPlan[i].checked == true ) {
					pPlanChecked = 1;
				}
			}
			
			if ( pPlanChecked == 0 ) {
				strError = 'Please choose a plan';
			}
			
			if ( pCountry.value == 'United States' ) {
				var re = /^\d{5}(-\d{4})?$/;
				
				if ( ! re.test( pPCode.value ) ) {
					strError = 'Post Code does not appear to be valid';
				}
			} else if ( pCountry.value == 'United Kingdom' ) {
				var re = /^[A-Z]{1,2}\d[A-Z\d]?( )?\d[ABD-HJLNP-UW-Z]{2}$/;
				
				if ( ! re.test( pPCode.value ) ) {
					strError = 'Post Code does not appear to be valid';
				}
			} else if ( pCountry.value == 'Canada' ) {
				var re = /^[ABCEGHJKLMNPRSTVXY]\d[A-Z]( )?\d[A-Z]\d$/;
				
				if ( ! re.test( pPCode.value ) ) {
					strError = 'Post Code does not appear to be valid';
				}
			}
		
			var pGenderChecked = 0;
			
			for ( i = 0; i < pGender.length; i++ ) {
				if ( pGender[i].checked == true ) {
					pGenderChecked = 1;
				}
			}

			if ( pGenderChecked == 0 ) {
				strError = 'Please choose a gender';
			}
			
			if ( pYear.options.selectedIndex == 0 || pMonth.options.selectedIndex == 0 || pDate.options.selectedIndex == 0 ) {
				strError = 'Birth date does not appear to be valid';
			}
			
			var re = /^[a-z0-9\-\.]{2,255}@([a-z0-9][a-z0-9\-]{0,61}[a-z0-9]\.)+[a-z]{2,20}$/i;
			
			if ( ! re.test( pEmail.value ) ) {
				strError = 'The Email does not appear to be valid';
			}
			
			var re = /[^a-z\-\s]/i;
			
			if ( String( pFName.value ).replace( /\s/g, '' ) == '' || re.test( pFName.value ) ) {
				strError = 'First Name may contain only letters, hypens, and spaces';
			}
			
			if ( String( pLName.value ).replace( /\s/g, '' ) == '' || re.test( pLName.value ) ) {
				strError = 'Last Name may contain only letters, hypens, and spaces';
			}
			
			if ( pPassword.value != pCPassword.value ) {
				strError = 'Passwords do not match';
			}
				
			if ( pPassword.value < 6 ) {
				strError = 'Passwords must be at least 6 characters';
			}
		
			if ( pPassword.value.length > 24 ) {
				strError = 'Passwords must be 24 or less characters';
			}

			var re = /[`<>\*]/;
			
			if ( re.test( pPassword.value ) || re.test( pCPassword.value ) ) {
				strError = 'Password contains illegal characters';
			}
			
			if ( pUser ) {
				if ( String( pUser.value ).replace( /\s/g, '' ) == '' || pUser.value.length < 3 ) {
					strError = 'Username must be at least 3 characters';
				}
				
				if ( pUser.value.length > 255 ) {
					strError = 'Username must be 255 or less characters';
				}

				var re = /^[a-z0-9_]+$/i;

				if ( ! re.test( pUser.value ) ) {
					strError = 'Username contains illegal characters';
				}
				
				var re = /^(inactive_)/i;
				
				if ( re.test( pUser.value ) ) {
					strError = 'Username $name cannot begin with `inactive_`';
				}
				
				if ( pUser.value == 'file_manager.html' || pUser.value == 'images' || pUser.value == 'main' ) {
					strError = 'This Username cannot be used';
				}
			}
		} else if ( step = 2 ) {
			// Validate Credit Card Information fields
			var cCCNumber	= frm.elements['cc_number'];
			var cType	= frm.elements['cc_type'];
			var cCountry	= frm.elements['cc_country'].options[frm.elements['cc_country'].selectedIndex];
			var cState	= frm.elements['cc_state'];
			var cYear	= frm.elements['cc_year'];
			var cMonth	= frm.elements['cc_month'];
			var cCVV	= frm.elements['cc_vcode'];
			var cCity	= frm.elements['cc_city'];
			var cPhone	= frm.elements['cc_phone'];
			var cZIP	= frm.elements['cc_zip'];
			var cAddress1	= frm.elements['cc_address1'];
			var cAddress2	= frm.elements['cc_address2'];
			var cAddress	= cAddress1.value + cAddress2.value;
			var cFirstName	= frm.elements['cc_firstname'];
			var cLastName	= frm.elements['cc_lastname'];
			var cName	= cFirstName + cLastName;
			
			var re = /^\d+$/;
			var re1 = /^\*+$/;
			
			if ( ! re.test( cPhone.value ) || cPhone.value.length > 30 ) {
				strError = 'Phone does not appear to be valid';
			}
			
			if ( ! re.test( cCVV.value ) || cCVV.value.length < 3 ) {
				strError = 'Credit Card Verification Code does not appear to be valid';
			}
			
			if ( ! re1.test( cCCNumber.value ) && ! re.test( cCCNumber.value ) ) {
				strError = 'Credit Card number does not appear to be valid';
			}
			
			if ( cType.options.selectedIndex == 0 ) {
				strError = 'Please choose Credit Card Type';
			}
		
			if ( cMonth.options.selectedIndex == 0 || cYear.options.selectedIndex == 0 ) {
				strError = 'Credit Card Expiration Date does not appear to be valid';
			}
			
			if ( cCountry.value == 'United States' && cState.options.selectedIndex == 0 ) {
				strError = 'Please choose State';
			}
				
			if ( cCountry.value == 'United States' ) {
				var re = /^\d{5}(-\d{4})?$/;
				
				if ( ! re.test( cZIP.value ) ) {
					strError = 'Post Code does not appear to be valid';
				}
			} else if ( cCountry.value == 'United Kingdom' ) {
				var re = /^[A-Z]{1,2}\d[A-Z\d]?( )?\d[ABD-HJLNP-UW-Z]{2}$/;
				
				if ( ! re.test( cZIP.value ) ) {
					strError = 'Post Code does not appear to be valid';
				}
			} else if ( cCountry.value == 'Canada' ) {
				var re = /^[ABCEGHJKLMNPRSTVXY]\d[A-Z]( )?\d[A-Z]\d$/;
				
				if ( ! re.test( cZIP.value ) ) {
					strError = 'Post Code does not appear to be valid';
				}
			}
		
			var re = /[`<>\*\s]/;
			
			if ( re.test( cCity.value ) ) {
				strError = 'City contains illegal characters';
			} else if ( cCity.value.length < 3 ) {
				strError = 'City must be at least 3 characters';
			} else if ( cCity.value.length > 255 ) {
				strError = 'City can contain maximum 255 characters';
			}
			
			var re = /[`<>\*]/;
			
			if ( re.test( cAddress ) ) {
				strError = 'Address contains illegal characters';
			} else if ( cAddress.length < 8 ) {
				strError = 'Address must be at least 8 characters';
			} else if ( cAddress.length > 255 ) {
				strError = 'Address can contain maximum 255 characters';
			}
			
			var re = /[`<>\*]/;
			
			if ( re.test( cName ) ) {
				strError = 'Name does not appear to be valid';
			} else if ( cName.length < 8 ) {
				strError = 'Name must be at least 8 characters';
			} else if ( cName.length > 255 ) {
				strError = 'Name can contain maximum 255 characters';
			}
		}
		
		if ( strError ) {
			alert( strError );
			return false;
		}
	}
	
	return true;
}	
// }}}

// {{{ ValidateFields ( oRef frm )
function ValidateFields ( frm ) {
	//
}
// }}}

