var submittedValue;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function showElements(theForm) {   
    str = "Form Elements of form " + theForm.name + ": \n "   
    for (i = 0; i < theForm.length; i++)       
        str += theForm.elements[i].name + "\n"   
        alert(str)
}

function setSubmittedValue(what){
    submittedValue = what;
}
function ValidateForm(){ 

	var dt=document.formFinal.CUSTNUM;
    var dt1=document.formFinal.CUSTNAME;
    var dt2=document.formFinal.NAME;
    var dt3=document.formFinal.PHONE; 
    var dt4=document.formFinal.USERNAME;  
    var dt5=document.formFinal.PASSWORD;
    var dt6=document.formFinal.PASSWORD2;  
    
    if (submittedValue == "<< Prev"){
        return true;
    }
    

    if (Trim(dt.value) == ""){
		alert("Invalid Customer Number.");
        dt.focus();
		return false;
	} 

    if (!isInteger(Trim(dt.value))){
		alert("Invalid Customer Number.");
        dt.focus();
		return false;
	} 
                      
    if (Trim(dt1.value) == ""){
		alert("Invalid Company Name.");
        dt1.focus();
		return false;
	} 

    if (Trim(dt2.value) == ""){
		alert("Invalid Name.");
        dt2.focus();
		return false;
	} 

    if (Trim(dt3.value) == ""){
		alert("Invalid Phone Number.");
        dt3.focus();
		return false;
	} 

    if (Trim(dt4.value) == ""){
		alert("Invalid Email Address.");
        dt4.focus();
		return false;
	}                 

    if (!dt4.value == "") {
		if(!validateEmail(dt4.value)) {
            window.alert("Invalid Email Address");
			dt4.focus();
			return false;
		}
		if (dt4.value.length > 70) {
			window.alert("Invalid Email Address");
			dt4.focus();
			return false;
		}
	}
     
    if (Trim(dt5.value) == ""){
		alert("Password is invalid.");
        dt5.focus();
		return false;
	} 
  
    if (Trim(dt5.value) != Trim(dt6.value)){
		alert("Password does not match verification password.");
        dt5.focus();
		return false;
	} 
     
   // document.formFinal.submit();
    return true;
 }

function validateEmail(emailString)
{

/* The characters don't belong in a valid email address */
invalidChars = " /:,;";

emailUpper = emailString.toUpperCase();

/* You must enter something */
	if (emailString == "")
	{
	window.alert("You must enter your email!");
	return false;
	}

/* There must be something BEFORE the at sign */ 
	if (emailString.indexOf("@", 0) == 0)
	{
	window.alert("No username in email address!");
	return false;
	}

/* There must be an at sign at, or after, the second character */
	if (emailString.indexOf("@", 1) == -1)
	{
	window.alert("No @ sign in email address!");
	return false;
	}

/* There must be a period somewhere */
	if (emailString.indexOf(".", 0) == -1)
	{
	window.alert("No period in email address!");
	return false;
	}

/* Check for invalid characters */
	for (i=0; i<invalidChars.length; i++)
	{
		if (emailString.indexOf(invalidChars.charAt(i), 0) > -1)
		{
		window.alert("Bad character(s) in email address!", invalidChars.charAt(i), i);
		return false;
		}
	} 

/* Check for free email accounts */

	if (emailUpper.indexOf("@YAHOO") > 0)
	{
		window.alert("Free email accounts are not accepted.");
		return false;
	}
	
	if (emailUpper.indexOf("@MAIL.YAHOO") > 0)
	{
		window.alert("Free email accounts are not accepted.");
		return false;
	}
	
	if (emailUpper.indexOf("@REDIFF") > 0)
	{
		window.alert("Free email accounts are not accepted.");
		return false;
	}
	
	
	if (emailUpper.indexOf("@HOTMAIL") > 0)
	{
		window.alert("Free email accounts are not accepted.");
		return false;
	}
		
	if (emailUpper.indexOf("@MAIL.COM") > 0)
	{
		window.alert("Free email accounts are not accepted.");
		return false;
	}
	
	if (emailUpper.indexOf("@FASTMAIL.FM") > 0)
	{
		window.alert("Free email accounts are not accepted.");
		return false;
	}
		
/* We made it! The email looks good! */ 
return true;
}

function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function

