var $req;

function validate_newuser() {
	$valid_characters = /^[a-zA-Z0-9_]+$/;
	$valid_numbers = /^[0-9]/;
	$valid_alpha = /[a-zA-Z0-9]/;

	if ( document.getElementById("contact_name").value == "" ) {
		alert( "Please enter a contact name." );
		document.getElementById("contact_name").focus();
		return;
	}
	
	if ( document.getElementById("company_name").value == "" ) {
		alert( "Please enter a company name." );
		document.getElementById("company_name").focus();
		return;
	}
	
	if ( document.getElementById("customer_code")  ) {
		if ( document.getElementById("customer_code").value == "" ) {
			alert( "Please enter a customer code." );
			document.getElementById("customer_code").focus();
			return;
		}
	}

	if ( document.getElementById("address").value == "" ) {
		alert( "Please enter a valid address." );
		document.getElementById("address").focus();
		return;
	}

	if ( document.getElementById("city").value == "" ) {
		alert( "Please enter a valid city." );
		document.getElementById("city").focus();
		return;
	}
	
	if ( document.getElementById("state").selectedIndex == 0 ) {
		alert( "Please select a state." );
		document.getElementById("state").focus();
		return;
	}
	
	if ( document.getElementById("zip").value == "" ) {
		alert( "Please enter a zip code." );
		document.getElementById("zip").focus();
		return;
	} 

	if ( document.getElementById("zip").value.length != 5 ) {
		alert( "Please enter a valid zip code." );
		document.getElementById("zip").focus();
		return;
	}
	
	if ( document.getElementById("phone").value == "" ) {
		alert( "Please enter a phone number." );
		document.getElementById("phone").focus();
		return;
	}

	if ( document.getElementById("phone").value.length < 10 ) {
		alert( "Please enter a valid phone number." );
		document.getElementById("phone").focus();
		return;
	}
	
	if ( document.getElementById("fax").value.length != 0 ) {
		if ( document.getElementById("fax").value.length < 9 ) {
			alert( "Please enter a valid fax number." );
			document.getElementById("fax").focus();
			return;
		}
	}

	if ( document.getElementById("password_1") ) {
		if ( document.getElementById("user_name").value == "" ) {
			alert(" Please enter a User Name.");
			document.getElementById("user_name").focus();
			return;
		}

		if ( !$valid_alpha.test ( document.getElementById("user_name").value ) ) {
			alert(" Please enter a valid User Name.");
			document.getElementById("user_name").focus();
			return;
		}

		if ( document.getElementById("user_name").value.length < 5 ) {
			alert(" Please enter a User Name that's longer than 5 characters..");
			document.getElementById("user_name").focus();
			return;
		}

		if ( document.getElementById("password_1").value == "" ) {
			alert( "Please supply a password." );
			document.getElementById("password_1").focus();
			return;
		}

		if ( document.getElementById("password_1").value != document.getElementById("password_2").value ) {
			alert( "The passwords do not match." );
			document.getElementById("password_1").focus();
			return;
		}

		if ( document.getElementById("email").value == "" ) {
			alert( "Please enter your e-mail address." );
			document.getElementById("email").focus();
			return;
		}
	}
	document.forms[0].submit();
	//alert( "SUBMIT" );
}

function validate_password() {
	$valid_characters = /^[a-zA-Z0-9_]+$/;
	$password_1 = document.getElementById( "password_1" ).value;
	$password_2 = document.getElementById( "password_2" ).value;
	
	if ( document.getElementById("contact_name").value == "" ) {
		alert( "Please enter a contact name." );
		document.getElementById("contact_name").focus();
		return;
	}
			
	if ( document.getElementById("company_name").value == "" ) {
		alert( "Please enter a company name." );
		document.getElementById("company_name").focus();
		return;
	}
	
	if ( document.getElementById("address_1").value == "" ) {
		alert( "Please enter a valid address." );
		document.getElementById("address").focus();
		return;
	}
				
	if ( document.getElementById("city").value == "" ) {
		alert( "Please enter a valid city." );
		document.getElementById("city").focus();
		return;
	}
				
	if ( document.getElementById("state").value == 0 ) {
		alert( "Please enter a valid state." );
		document.getElementById("state").focus();
		return;
	}
				
	if ( document.getElementById("zip").value == "" ) {
		alert( "Please enter a zip code." );
		document.getElementById("zip").focus();
		return;
	} 

	if ( document.getElementById("zip").value.length != 5 ) {
		alert( "Please enter a valid zip code." );
		document.getElementById("zip").focus();
		return;
	}
			
	if ( document.getElementById("phone").value == "" ) {
		alert( "Please enter a phone number." );
		document.getElementById("phone").focus();
		return;
	}
			
	if ( document.getElementById("phone").value.length < 10 ) {
		alert( "Please enter a valid phone number." );
		document.getElementById("phone").focus();
		return;
	}
					
	if ( $password_1 != $password_2 ) { 
		alert( "The passwords do not match." );
		document.getElementById("password_1").focus();
		return;
	}
	if ( !$valid_characters.test( $password_1 ) ) {
		if ( $password_1 != "" ) {
			alert( "The password you have specified is invalid." );
			return;
		}
	} 
	if ( document.getElementById( "old_password" ).value == "" ) {
		alert( "Please enter your current password." );
		return;
	}
	document.forms[0].submit();
}

function getXMLTag( $tag) {
	var anXML = $req.responseXML.getElementsByTagName($tag);
	var anArray = new Array();
	// alert( anXML[0].firstChild ) ;
	if ( anXML.length == 1 ) {
		if ( anXML[0].firstChild == null ) {
			return "";
		}
	} 
	for (var i = 0; i < anXML.length; i++) {
		anArray.push( anXML[i].firstChild.nodeValue );
	}
	return anArray;
}

function autofill_contactus() {
	if ($req.readyState == 4) {
		if ($req.status == 200 ) {	
			fill_item( "store_name", getXMLTag( "store" ) );
			fill_item( "address_1", getXMLTag( "address_1" ) );
			fill_item( "address_2", getXMLTag( "address_2" ) );
			fill_item( "city", getXMLTag( "city" ) );
			$state = getXMLTag( "state" );
			$state_dropdown = document.getElementById("state")
			for ( $k = 0; $k < $state_dropdown.length; $k++ ) {
				if ( $state_dropdown.options[$k].value == $state ) {
					$state_dropdown.selectedIndex = $k;
				}
			}
			fill_item( "zip", getXMLTag( "zip" ) );
			fill_item( "contact", getXMLTag( "contact" ) );
			fill_item( "email", getXMLTag( "email" ) );
			fill_item( "phone", getXMLTag( "phone" ) );
			fill_item( "fax", getXMLTag( "fax" ) );
		}
	}
}

function fill_item( $field, $value ) {
	document.getElementById( $field ).value = $value;
}

function checkFillSubmit( $user_id ) {
	var $url = "/Leslies/XML/contactus?uid=" + $user_id;
	if ( window.XMLHttpRequest ) {
		$req = new XMLHttpRequest();
		$req.onreadystatechange = autofill_contactus;
		$req.open( "GET", $url, true );
		$req.send( null );
	} else if ( window.ActiveXObject ) {
		$req = new ActiveXObject( "Microsoft.XMLHTTP" );
		if ( $req ) {
			$req.onreadystatechange = autofill_contactus;
			$req.open( "GET", $url, true );
			$req.send();
		}
	}
} 
