
var whitespace = " \t\n\r";
var phoneDelimiters = "()- ";
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;

function isDigit(c)
{
	return ((c >= "0") && (c <= "9"));
}

function isLetter(c)
{
	return ((c >= "a" && c <= "z") || (c >= "A" && c <= "Z"));
}

function isBlank(c)
{
	return (c == "\t" || c == "\r" || c == "\n" || c == "\f");
}

function isLetterOrDigit(c)
{
	 return (isLetter(c) || isDigit(c));
}

function isInteger(s)
{
	var i;
	if (isEmpty(s))
		return (false);
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}
	return true;
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{   var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function isEmail(s)
{
	if (isWhitespace(s)) return false;
	var i = 1;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@")) {
		i++
	}
	if ((i >= sLength) || (s.charAt(i) != "@"))
		return false;
	else 
		i += 2;
	while ((i < sLength) && (s.charAt(i) != ".")) {
		i++
	}
	if ((i >= sLength - 1) || (s.charAt(i) != "."))
		return false;
	else
		return true;
}

function reformat (s)
{   var arg;
    var sPos = 0;
    var resultString = "";
    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

function reformatUSPhone (USPhone)
{   return (reformat (USPhone, "(", 3, ") ", 3, "-", 4))
}

function verify_form(theForm)
{
	if (theForm.fldFirst.value == "") {
		alert("Please enter your First Name.");
		theForm.fldFirst.focus();
		return (false);
  	}

	if (theForm.fldLast.value == "") {
		alert("Please enter your Last Name.");
		theForm.fldLast.focus();
		return (false);
	}


//	if (theForm.fldCity.value == "") {
//		alert("Please enter your City.");
//		theForm.fldCity.focus();
//		return (false);
//	}
	if (theForm.fldState.selectedIndex == 0) {
		alert("Please select State or Non-US if required.");
		theForm.fldState.focus();
		return (false);
	}

//	if (theForm.fldZip.value == "") {
//		alert("Please enter your Zip/Postal Code.");
//		theForm.fldZip.focus();
//		return (false);
//	}

	if (theForm.fldCountry.selectedIndex == 0) {
		alert("Please select your Country.");
		theForm.fldCountry.focus();
		return (false);
	}

	if (theForm.fldPhone.value == "") {
		alert("Please enter a Phone Number.");
		theForm.fldPhone.focus();
		return (false);
	}

	if (theForm.fldEmail.value == "") {
		alert("Please enter an Email Address.");
		theForm.fldEmail.focus();
		return (false);
	}

	if (theForm.fldDistro.selectedIndex == 0) {
		alert("Please select Linux distribution.");
		theForm.fldDistro.focus();
		return (false);
	}
	
	if (!isEmail(theForm.fldEmail.value, false)) {
		alert("Please enter only letter, digit and \"@,_,.\" characters in the \"Email\" field.");
		theForm.fldEmail.focus();
		return (false);
	}

	if (theForm.fldCountry.selectedIndex != 1)
		return (true);

	if (theForm.fldState.selectedIndex < 2) {
		alert("Please specify your State in United States.");
		theForm.fldState.focus();
		return (false);
	}

	if (theForm.fldPhone.value.length < 10) {
		alert("Phone Number should be at least 10 characters.");
		theForm.fldPhone.focus();
		return (false);
	}

	var normalized = stripCharsInBag(theForm.fldPhone.value,
					phoneDelimiters);
	var inputValid = true;
	if (isEmpty(normalized || !(isInteger(normalized) && 
					normalized.length == 10))) {
		alert("Please enter only digit, whitespace and \"-)(\" characters in the \"Phone\" field.");
		theForm.fldPhone.focus();
		return (false);
	}
	theForm.fldPhone.value = reformatUSPhone(normalized);

//	var s = theForm.fldZip.value;
//	if (!(isInteger(s) && 
 //           ((s.length == digitsInZIPCode1) ||
  //           (s.length == digitsInZIPCode2)))) {
//		alert("Please enter only 5 or 9 digit U.S. ZIP Code.");
//		theForm.fldZip.focus();
//		return (false);
//	}
	return (true);
}

function verify_support(theForm) {
	if (theForm.fldFirst.value == "") {
		alert("Please enter your First Name.");
		theForm.fldFirst.focus();
		return (false);
  	}

	if (theForm.fldLast.value == "") {
		alert("Please enter your Last Name.");
		theForm.fldLast.focus();
		return (false);
	}


	if (theForm.fldEmail.value == "") {
		alert("Please enter an Email Address.");
		theForm.fldEmail.focus();
		return (false);
	}

	if (theForm.fldPhone.value == "") {
		alert("Please enter a Phone Number.");
		theForm.fldPhone.focus();
		return (false);
	}

	if (theForm.fldDistro.selectedIndex == 0) {
		alert("Please select Linux distribution.");
		theForm.fldDistro.focus();
		return (false);
	}
	
	if (!isEmail(theForm.fldEmail.value, false)) {
		alert("Please enter only letter, digit and \"@,_,.\" characters in the \"Email\" field.");
		theForm.fldEmail.focus();
		return (false);
	}

	if (theForm.fldLog.value == "") {
		alert("Please provide Error Logs.");
		theForm.fldLog.focus();
		return (false);
	}
	if (theForm.fldSystem.value == "") {
		alert("Please provide System Description.");
		theForm.fldSystem.focus();
		return (false);
	}

	return (true);
}

function verify_contact(theForm) {
	if (theForm.fldFirst.value == "") {
		alert("Please enter your First Name.");
		theForm.fldFirst.focus();
		return (false);
  	}

	if (theForm.fldLast.value == "") {
		alert("Please enter your Last Name.");
		theForm.fldLast.focus();
		return (false);
	}

	if (theForm.fldEmail.value == "") {
		alert("Please enter an Email Address.");
		theForm.fldEmail.focus();
		return (false);
	}

	if (theForm.fldPhone.value == "") {
		alert("Please enter a Phone Number.");
		theForm.fldPhone.focus();
		return (false);
	}

	if (theForm.fldState.selectedIndex == 0) {
		alert("Please specify your State or Select Non-US if required");
		theForm.fldState.focus();
		return (false);
	}

	if (!isEmail(theForm.fldEmail.value, false)) {
		alert("Please enter only letter, digit and \"@,_,.\" characters in the \"Email\" field.");
		theForm.fldEmail.focus();
		return (false);
	}

	return (true);
}

function verify_register(theForm) {
	if (!verify_form(theForm))
		return (false);

	if (theForm.fldProduct.selectedIndex == 0) {
		alert("Please select a Product.");
		theForm.fldProduct.focus();
		return (false);
	}
	if (theForm.fldVersion.selectedIndex == "") {
		alert("Please select product version.");
		theForm.fldVersion.focus();
		return (false);
  	}
	return (true);
}

function regverify(form)
{
	if (form.fldFirst.value == "") {
		alert("Please enter your First Name.");
		form.fldFirst.focus();
		return (false);
  	}

	if (form.fldLast.value == "") {
		alert("Please enter your Last Name.");
		form.fldLast.focus();
		return (false);
	}

	if (form.fldCountry.selectedIndex == 0) {
		alert("Please select your Country.");
		form.fldCountry.focus();
		return (false);
	}

	if (form.fldPhone.value == "") {
		alert("Please enter a Phone Number.");
		form.fldPhone.focus();
		return (false);
	}

	var val = '';
	for (var i = 0; i < form.profServer.length; i++) {
		if (form.profServer[i].checked) {
			val = form.profServer[i].value;
			break;
		}
	}
	if (val == '') {
		alert("Please check number of servers you have at your location.");
		return (false);
	}

	var val = '';
	for (var i = 0; i < form.profOS.length; i++) {
		if (form.profOS[i].checked)
			val += form.profOS[i].value + " ";
	}
	if (val == '') {
		alert("Please check operating systems you use at your location.");
		return (false);
	}
	form.fldPlatform.value = val;

	if (form.fldCountry.selectedIndex != 1)
		return (true);

	if (form.fldState.selectedIndex == 1) {
		alert("Please specify your State in United States.");
		form.fldState.focus();
		return (false);
	}

	if (form.fldPhone.value.length < 10) {
		alert("Phone Number should be at least 10 characters.");
		form.fldPhone.focus();
		return (false);
	}

	var normalized = stripCharsInBag(form.fldPhone.value,
					phoneDelimiters);
	var inputValid = true;
	if (isEmpty(normalized || !(isInteger(normalized) && 
					normalized.length == 10))) {
		alert("Please enter only digit, whitespace and \"-)(\" characters in the \"Phone\" field.");
		theForm.fldPhone.focus();
		return (false);
	}
	form.fldPhone.value = reformatUSPhone(normalized);


	return (true);
}

function login_verify(form)
{
	if (form.fldUser.value == "") {
		alert("Please enter user name (usually your email address).");
		form.fldUser.focus();
		return (false);
  	}

	if (form.fldPass.value == "") {
		alert("Please enter a password.");
		form.fldPass.focus();
		return (false);
	}
	if (!isEmail(form.fldUser.value, false)) {
		alert("Please enter only letter, digit and \"@,_,.\" characters in the \"Email\" field.");
		form.fldUser.focus();
		return (false);
	}

	return (true);
}

function signup_verify(form)
{
	if (form.fldUser.value == "") {
		alert("Please enter user name (usually your email address).");
		form.fldUser.focus();
		return (false);
  	}

	if (form.fldPass.value == "") {
		alert("Please enter a password.");
		form.fldPass.focus();
		return (false);
	}
	if (form.fldPass1.value == "") {
		alert("Please re-enter a password.");
		form.fldPass1.focus();
		return (false);
	}
	if (form.fldPass1.value.length < 6 || 
		form.fldPass1.value.length > 12) {
		alert("Please choose a password between 6 - 12 characters");
		form.fldPass1.focus();
		return (false);
	}
	if (form.fldPass.value != form.fldPass1.value) {
		alert("Passwords do not match.");
		form.fldPass.focus();
		return (false);
	}
	if (!isEmail(form.fldUser.value, false)) {
		alert("Please enter only letter, digit and \"@,_,.\" characters in the \"Email\" field.");
		form.fldUser.focus();
		return (false);
	}

	return (true);
}
