//set up global variables
var iValidateAddress = 0;
var step = 1;
var totalStep = 1;

function stepAll() {
    
    //hide the submit button
    if ($('submitButton')){
        $('submitButton').style.display = "none";
    }
    //show the loader
    if ($('loadingGraphic')){
        $('loadingGraphic').style.display = "block";
    }
    
    var okay = true;
    bSubmitted = true;
    
    iValidateAddress = 0;
    
    
	for (x=0; x<document.captureForm.elements.length; x++){
		
		try {
		    var ElementName =  document.captureForm.elements[x].name;
			var AlertElement = document.captureForm.elements[x].name + "Text";
			var AlertText = $(AlertElement).value;
		} catch(ex) {
			//alert(AlertElement)
			var AlertText = "";
		}
		
		//standard html input=checkbox
	    if(document.captureForm.elements[x].id == "agree"){
	        
			if(!checkDisClaimer(ElementName, "Please click the checkbox to continue.")){
				return bSubmitted = okay = false;
				break;
			}
			
	    }
		 
	    //for pages that use image based privacy checkbox
	    if(document.captureForm.elements[x].id == "privacy"){
            if (document.captureForm.elements[x].value == 0){
    	        failed("Please click the Checkbox to continue.");
				return bSubmitted = okay = false;
				break
			}
	    }
	    
	    if (document.captureForm.elements[x].id == "firstname"){
			if (!CheckNull($("firstname"), "First Name")) {
		        return bSubmitted = okay = false;
			}
			if (!DoRegEx1("getElementById(\"firstname\")","First Name")) {
	       		return bSubmitted = okay = false;
			}
		    if (!checkRepeat("firstname","First Name")) {
				return bSubmitted = okay = false;
		    }
			if (($("firstname").value == "Jane" || $("firstname").value == "John") &&  $("lastname").value == "Doe"){
			    failed("Please enter a valid first and last name");
			    return bSubmitted = okay = false;
			}
			if ($("firstname").value == $("lastname").value){
			    failed("Please enter a valid first and last name");
			    return bSubmitted = okay = false;
			}
	    }

		if (document.captureForm.elements[x].id == "lastname"){
			if (!CheckNull($("lastname"), "Last Name")) {
		        return bSubmitted = okay = false;
			}
			if (!DoRegEx1("getElementById(\"lastname\")","Last Name")) {
		        return bSubmitted = okay = false;
			}
			if (!checkRepeat("lastname","Last Name")) {
				return bSubmitted = okay = false;
		    }
		    if (okay == true && !(iValidateAddress & 2)){
		        iValidateAddress += 2;
		    }
		}
		if (document.captureForm.elements[x].id == "email"){
			if (!CheckNull($("email"), "E-mail")) {
		        return bSubmitted = okay = false;
			}
			
		    if (!validateEmail($("email").value)){
				failed("Please enter a valid email address.");
				try{
				    $("email").focus();
				} catch (err){
				    //
				}
		        return bSubmitted = okay = false;
		    }
			if (!DoRegEx2("getElementById(\"email\")","E-mail")) {
		        return bSubmitted = okay = false;
			}
			if($("confirmemail")){
				if (document.captureForm.email.value != document.captureForm.confirmemail.value) {
				    failed("Your Email Addresses do not match!  Please try again.");
				    document.captureForm.confirmemail.value = '';
				    document.captureForm.confirmemail.focus();
			        return bSubmitted = okay = false;
				}	
			}
		}
		if (document.captureForm.elements[x].id == "emailaddress"){
			if (!CheckNull($("emailaddress"), "E-mail")) {
		        return bSubmitted = okay = false;
			}
		    if (!validateEmail($("emailaddress").value)){
				failed("Please enter a valid Email Address.");
				$("emailaddress").focus();
		        return bSubmitted = okay = false;
		    }
			if (!DoRegEx2("getElementById(\"emailaddress\")","E-mail")) {
		        return bSubmitted = okay = false;
			}
			if($("confirmemail")){
				if (document.captureForm.emailaddress.value != document.captureForm.confirmemail.value) {
				    failed("Your Email Addresses do not match!  Please try again.");
				    document.captureForm.confirmemail.value = '';
				    document.captureForm.confirmemail.focus();
			        return bSubmitted = okay = false;
				}	
			}
		}
		if (document.captureForm.elements[x].id == "area_code" || document.captureForm.elements[x].id == "phone" ){
		    if(!validatePhone()) {
		        return bSubmitted = okay = false;
		    }
		    if (okay == true && !(iValidateAddress & 1)){
		        iValidateAddress += 1;
		    }
		}
	
		if(document.captureForm.elements[x].id == "textReq"){
		    if(document.captureForm.elements[x].name == "address"){
		        if (document.captureForm.elements[x].value == ""){
    				if(AlertText != ""){
    					failed(AlertText);
    				}else{
    					failed("Please enter a valid Address to continue.");
    				}
    				document.captureForm.elements[x].focus();
    				return bSubmitted = okay = false;
    				break
			    }
    		    if (okay == true && !(iValidateAddress & 4)){
    		        iValidateAddress += 4;
    		    }
			}else if(document.captureForm.elements[x].name == "city"){
			    if (document.captureForm.elements[x].value == ""){
    				if(AlertText != ""){
    					failed(AlertText);
    				}else{
    					failed("Please enter your City to continue.");
    				}
    				document.captureForm.elements[x].focus();
    				return bSubmitted = okay = false;
    				break
			    }
			    if (!checkRepeat("city","City")) {
    				return bSubmitted = okay = false;
    		    }
    		    if (okay == true && !(iValidateAddress & 8)){
    		        iValidateAddress += 8;
    		    }
			}else if(document.captureForm.elements[x].name == "zipcode"){
			    if (document.captureForm.elements[x].value == ""  || document.captureForm.elements[x].value.length < 4){
    				if(AlertText != ""){
    					failed(AlertText);
    				}else{
    					failed("Please enter a valid Zipcode to continue.");
    				}
    				document.captureForm.elements[x].focus();
    				return bSubmitted = okay = false;
    				break
			    }
			    if (okay == true && !(iValidateAddress & 32)){
    		        iValidateAddress += 32;
    		    }
    		}else if(document.captureForm.elements[x].name == "state"){
			    if (document.captureForm.elements[x].value == ""  || document.captureForm.elements[x].value.length < 2){
    				if(AlertText != ""){
    					failed(AlertText);
    				}else{
    					failed("Please enter a valid State to continue.");
    				}
    				document.captureForm.elements[x].focus();
    				return bSubmitted = okay = false;
    				break
			    }
			    if (okay == true && !(iValidateAddress & 16)){
    		        iValidateAddress += 16;
    		    }
			}else{
    			if (document.captureForm.elements[x].value == ""){
    				if(AlertText != ""){
    					failed(AlertText);
    				}else{
    					failed("Please enter a value for this field.");
    				}
    				document.captureForm.elements[x].focus();
    				return bSubmitted = okay = false;
    				break
    			}
		    }
		}
		
		if(document.captureForm.elements[x].id == "hiddenReq"){
			if (document.captureForm.elements[x].value == ""){
				if(AlertText != ""){
					failed(AlertText);
				}else{
					failed("Please enter a value for this field.");
				}
				return bSubmitted = okay = false;
				break
			}
		}		
		
		if(document.captureForm.elements[x].id == "selectReq"){
			varSelect = new RegExp("Select");
			if(document.captureForm.elements[x].name == "state"){
			    if (document.captureForm.elements[x].value.match(varSelect) || document.captureForm.elements[x].value == ""){
    				if(AlertText != ""){
    					failed(AlertText);
    				}else{
    					failed("Please Select a State to continue.");
    				}
    				document.captureForm.elements[x].focus();
    				return bSubmitted = okay = false;
    				break
    			}
    			if (okay == true && !(iValidateAddress & 16)){
    		        iValidateAddress += 16;
    		    }
			}else{
    			if (document.captureForm.elements[x].value.match(varSelect) || document.captureForm.elements[x].value == ""){
    				if(AlertText != ""){
    					failed(AlertText);
    				}else{
    					failed("Please select an option for this field.");
    				}
    				document.captureForm.elements[x].focus();
    				return bSubmitted = okay = false;
    				break
    			}
		    }
		}
		
		if(document.captureForm.elements[x].id == "checkReq"){
			if (document.captureForm.elements[x].checked == false){
				if(AlertText != ""){
					failed(AlertText);
				}else{
					failed("The Checkbox must be checked to continue.");
				}
				document.captureForm.elements[x].focus();
				return bSubmitted = okay = false;
    			break
			}
		}
		if(document.captureForm.elements[x].id == "YN1"){
			var xx = x++;
			if (document.captureForm.elements[x].checked == false && document.captureForm.elements[xx].checked == false){
				if(AlertText != ""){
					failed(AlertText);
				}else{
					failed("Please answer all of the Yes/No questions.");
				}
				document.captureForm.elements[x].focus();
				return bSubmitted = okay = false;
				break
			}
		}
		if(document.captureForm.elements[x].id == "ccNum"){
			var aCCSplit = document.captureForm.elements[x].value.split(" ");
			var vCCNum = aCCSplit.join("");
			var aCCSplit = vCCNum.split("-");
			var iCCNum = aCCSplit.join("");
			document.captureForm.elements[x].value = iCCNum;
			var expDate = $("ccMonth").value + "/" + $("ccYear").value;
			//alert(iCCNum);
			
			if(!checkCC(iCCNum, expDate))	{
				failed ("Please enter a valid Credit Card Name, Number and Expiration Date.");
				return bSubmitted = okay = false;
				break
			}
		}
		if(document.captureForm.elements[x].id == "checkStart"){
			if(!oneOrMore(x)){
				failed ("Please select at least one Interest.");
				return bSubmitted = okay = false;
				break
			}
		}
		if (document.captureForm.elements[x].id == "captcha_input"){
	        if(document.captureForm.elements[x].value == ""){
                failed ("Please enter your Confirmation Code.");
				return bSubmitted = okay = false;
				break
	        }
		}
	}
	//end for loop
	
	//all is good, we are going to do the ajax validation call

	if(okay == true && iValidateAddress > 0){
        validateAddress2(iValidateAddress);
    } else if (okay == true && iValidateAddress == 0){
        document.captureForm.submit();
    }
}






