
function IsValidEmail(val){
    var rx = new RegExp("^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$");
    var matches = rx.exec(val);                     
    return (matches != null && val == matches[0]);
}


function onSubmit(){

	var objForm = document.getElementById("mainform");
	var action = document.getElementById("aFormAction").value;
	var sendIt = true;
    var checkboxchecked=false;
    var checkboxname="";
	var radiochecked=false;
    var radioname="";
	var radioID="";
	var checkboxid="";
	for (var iCounter=0; iCounter<objForm.length; iCounter++){
	var formEl=objForm.elements[iCounter];
		
	if(formEl.type == "checkbox" && (checkboxname==formEl.name || checkboxname!=""))
	{
	    if(checkboxname=="") { checkboxname=formEl.name;}
	    if(checkboxname==formEl.name && objForm.elements[iCounter].checked){  checkboxchecked=true;}
	    checkboxid=formEl.parentNode.parentNode.id;;
    }
    else
    {
      if(checkboxname!=formEl.name && checkboxname!="")
	    {
		    
			if(checkboxchecked==false)
			{
				obj = document.getElementById("requiredText"+checkboxid);
				if(obj){   obj.style.display = "inline";    }
				else{
		                obj.style.display = "none";		                
		            }
	        }
			if(formEl.type == "checkbox")
			{checkboxname=formEl.name; }
			else
			{		checkboxname=""; }
			checkboxchecked=false;
	    }
    }
	   
	   
	if(formEl.type == "radio" && (radioname=="" || radioname==formEl.name))
	{
	  if(radioname==""){radioname=formEl.name;}	
	  if(radioname==formEl.name && objForm.elements[iCounter].checked){ radiochecked=true;}
	  radioid=formEl.parentNode.parentNode.id;
	}
	else
	{
	 if(radioname!="" && radioname!=formEl.name)
	 {
	   
	   if(radiochecked==false)
	   {
	    obj = document.getElementById("requiredText"+radioid);
	    if(obj){ obj.style.display = "inline"; }
	    else{   obj.style.display = "none";}
	   }
	   if(formEl.type == "radio")
	   {
     	   radioname=formEl.name; 
	   }	   
	   else{   radioname="";      }
	   radiochecked=false;
	}
	}
	/**/

	    // show validate message
		
	if(document.getElementById("required"+formEl.id)){
	     var obj;
	            // required field validation
	            obj = document.getElementById("requiredText"+formEl.id);
	    	    if(obj){
	    	        if(formEl.value == ""){
		                obj.style.display = "inline";
		                sendIt = false;
		                continue;
		            }
		            else{
		                obj.style.display = "none";		                
		            }
   			    }
	    	
	    	
	    		
    		    // email validation if that is the case
	    	    obj = document.getElementById('validEmail' + formEl.id);			        
    	        if(obj){
                    if(!IsValidEmail(formEl.value)){
                         obj.style.display = "inline";
                         sendIt = false;                            
                    }
                    else{
                        obj.style.display = "none";                        
                    }
        	    }
				 // extensions
            
		   }
		   if(formEl!=null && formEl.id!=null && document.getElementById('requiredFileExtension' + formEl.id)!=null)
		   {
		   obj = document.getElementById('requiredFileExtension' + formEl.id);
            if (document.getElementById(formEl.id)!=null && document.getElementById(formEl.id).value!='' && document.getElementById('ext' + formEl.id) != null) {
              //  alert(formEl.value.replace(/^.*?\.([a-zA-Z0-9]+)$/, "$1") + "  " + document.getElementById('ext' + formEl.id).value +"  "+ document.getElementById('ext' + formEl.id).value.indexOf("|" + formEl.value.replace(/^.*?\.([a-zA-Z0-9]+)$/, "$1") + "|"));
                if (obj) {
                    if (document.getElementById('ext' + formEl.id).value.indexOf("|" + formEl.value.replace(/^.*?\.([a-zA-Z0-9]+)$/, "$1") + "|")) {
                        obj.style.display = "inline";
                        sendIt = false;
                    }
                    else {
                        obj.style.display = "none";
                    }
                }
				}
				}
     }
    
    
  
	if(sendIt==true){
		objForm.method="post";
		objForm.action=action;
		if (objForm.__VIEWSTATE != null) {
		objForm.__VIEWSTATE.name ="NOVIEWSTATE";
		}
		objForm.submit();
	}

}


