function validZip(zipCode) {
    var digits=0;
    for (var i=0; i<zipCode.length; i++) {
	if (!(parseInt(zipCode.charAt(i)) || zipCode.charAt(i)== "0"))
	    return false;
	else
	    digits++;
    }
	return true;
}
function validUsername(username) {
    var digits=0;
    if(username.length < 4)
	return false;
    else 
	return true;
}
function validEmail(email) {
    var needed=/^.+@.+\..{2,3}$/;
    if (!needed.test(email))  
	return false; 
    else  
	return true; 
}
function valid_number(ccode,cicode,arcode,number) {
    var digits = 0;
    var new_num = "";
    var dot = ".";

    for (var i=0; i<ccode.length; i++) {
	if (parseInt(ccode.charAt(i)) || ccode.charAt(i)== "0") {
	    new_num += ccode.charAt(i);
            digits++;
        }
    }
    new_num += dot;
    for (var i=0; i<cicode.length; i++) {
        if (parseInt(cicode.charAt(i)) || cicode.charAt(i)== "0") {
            new_num += cicode.charAt(i);
            digits++;
        }
    }
    new_num += dot;
    for (var i=0; i<arcode.length; i++) {
        if (parseInt(arcode.charAt(i)) || arcode.charAt(i)== "0") {
            new_num += arcode.charAt(i);
            digits++;
        }
    }
    new_num += dot;
    for (var i=0; i<number.length; i++) {
        if (parseInt(number.charAt(i)) || number.charAt(i)== "0") {
            new_num += number.charAt(i);
            digits++;
        }
    }	
    return new_num;
}


function checkForm(f) {
    var t1, t2;
    var result=true;
    var errors = "";
    if (!validUsername(f.Username.value)) {
	errors += "Username must be atleast 4 or more.\n";
        result = false;
    }
    if (f.Password.value.length < 4 || f.Password2.value.length < 4 || f.Password.value.length > 8 || f.Password2.value.length > 8 || f.Password.value != f.Password2.value) {
	errors += "Passwords must match and be between 4 and 8 characters to be valid.\n";
	result = false;
    }
    if (!f.Company.value) {
	errors += "Please enter your company's name or N/A if not applicable.\n";
	result = false;
    }
    if (!f.Firstname.value || !f.Lastname.value) {
        errors += "Please enter both your First and Last Name.\n";
        result = false;
    }
    if (!validEmail(f.Emailaddress.value)) {
        errors += "Please enter a valid email address.\n";
        result = false;
    }
    if(!f.country_code.value) {
	errors += "Please enter a Country Code for phone number.\n";
	result=false;
    }
    if(!f.city_code.value) {
        errors += "Please enter an Area Code for phone number.\n";
        result=false;
    }
    if(!f.arcode.value || !f.pnumber.value) {
        errors += "Please enter a full phone number.\n";
        result=false;
    }
    t1 = valid_number(f.country_code.value,f.city_code.value,f.arcode.value,f.pnumber.value);
    if (t1 == 0) {
	errors += "Please enter a valid Phone Number without any charecters.\n";
	result = false;
    }
    else {
	f.Phone.value = t1;
    }
    if(!f.fcountry_code.value) {
        errors += "Please enter a Country Code for Fax number.\n";
        result=false;
    }
	if(!f.fcity_code.value) {
        errors += "Please enter an Area Code for Fax number.\n";
        result=false;
    }
    if(!f.farcode.value || !f.fnumber.value) {
        errors += "Please enter a full Fax number.\n";
        result=false;
    }
    t2 = valid_number(f.fcountry_code.value,f.fcity_code.value,f.farcode.value,f.fnumber.value);
    if (t2 == 0) {
	errors += "Please enter a valid Fax Number without any charecters.\n";
	result = false;
    }
    else {
        f.Fax.value = t2;
    }
    if (!f.Address1.value || !f.City.value || !f.Country.value) {
        errors += "Please enter your complete address information.\n";
        result = false;
    }
    if (!f.zipCode.value) {
        errors += "Please enter a valid zip code.\n";
        result = false;
    }
    if (result == false)
	alert(errors);
    if(f == document.update_owner_user)
        return true;	
    else
        return result;
}
