//Written by Terry Grimm to validate the the teaser form
//Feb 13, 2009

    function ValidateForm(searchtype)    
    {
        
        var fnerror;
        var lnerror;
        var ageError;
		var dobError;
		var cityError;
		var dlError;
		var dobError;
        var validform = true;
        
        //alert("ValidateForm start " + searchtype);
        if(searchtype != 'rpl')
        {
            $('#' + searchtype + '_firstname').val($.trim($('#' + searchtype + '_firstname').val()));
            $('#' + searchtype + '_lastname').val($.trim($('#' + searchtype + '_lastname').val()));

            //alert($('#' + searchtype + '_firstname').val());
//            document.getElementById(searchtype + '_firstname').value =  document.getElementById(searchtype + '_firstname').value.Trim();
//            document.getElementById(searchtype + '_lastname').value =  document.getElementById(searchtype + '_lastname').value.Trim();
        }
        //alert("ValidateForm names " + searchtype);
        
        if(searchtype != 'fl' && searchtype != 'dr' && searchtype != 'rpl')
        {
            //alert("ValidateForm age " + searchtype);
            $('#' + searchtype + '_age').val($.trim($('#' + searchtype + '_age').val()));
//            document.getElementById(searchtype + '_age').value =  document.getElementById(searchtype + '_age').value.Trim();
        }
        
		if(searchtype=='fl')
		{
		    $('#' + searchtype + '_city').val($.trim($('#' + searchtype + '_city').val()));
//		    document.getElementById(searchtype + '_city').value =  document.getElementById(searchtype + '_city').value.Trim();
		    //alert("ValidateForm city " + searchtype);
		}
		
		if(searchtype=='crim' || searchtype=='so' || searchtype=='dr')	
		{
		    //alert("ValidateForm dob" + searchtype);
		    $('#' + searchtype + '_dob_1').val($.trim($('#' + searchtype + '_dob_1').val()));
		    $('#' + searchtype + '_dob_2').val($.trim($('#' + searchtype + '_dob_2').val()));		
		    $('#' + searchtype + '_dob_3').val($.trim($('#' + searchtype + '_dob_3').val()));
//		    document.getElementById(searchtype + '_dob_1').value =  document.getElementById(searchtype + '_dob_1').value.Trim();
//		    document.getElementById(searchtype + '_dob_2').value =  document.getElementById(searchtype + '_dob_2').value.Trim();		
//		    document.getElementById(searchtype + '_dob_3').value =  document.getElementById(searchtype + '_dob_3').value.Trim();
		}

		//alert("ValidateForm phone " + searchtype + " " +  document.getElementById(searchtype + '_phone_number').value);

		if(searchtype=='rpl')	
		{
		    $('#' + searchtype + '_phone_number').val($.trim($('#' + searchtype + '_phone_number').val()));
//		    document.getElementById(searchtype + '_phone_number').value =  document.getElementById(searchtype + '_phone_number').value.Trim();
		}
				
		if(searchtype == 'dr')		
		{
		    //alert("ValidateForm dr" + searchtype);
		    $('#' + searchtype + '_dl_number').val($.trim($('#' + searchtype + '_dl_number').val()));
		}
		
		
        
		switch(searchtype)
        {
            case "ps":
//                fnError = document.getElementById(searchtype + '_fnError');
//                lnError = document.getElementById(searchtype + '_lnError');                
//                ageError = document.getElementById(searchtype + '_ageError');                
//                stateError = document.getElementById(searchtype + '_stateError');
                //alert("Switch before Validate People");
                if(!ValidatePeople(searchtype))
                {
                    validform = false;   
                }
                if(!ValidateAge(searchtype))
                {
                    validform = false;   
                }
                //alert("after ValidateAge " + searchtype);
                if(!ValidateState(searchtype))
                {
                    validform = false;   
                }            
            break;
            case "bc":
//                fnError = document.getElementById(searchtype + '_fnError');
//                lnError = document.getElementById(searchtype + '_lnError');                
//                ageError = document.getElementById(searchtype + '_ageError');                
//                stateError = document.getElementById(searchtype + '_stateError');                
                if(!ValidatePeople(searchtype))
                {
                    validform = false;   
                }
                if(!ValidateAge(searchtype))
                {
                    validform = false;   
                }
                if(!ValidateState(searchtype))
                {
                    validform = false;   
                }
            break;
            case "dr":
//                fnError = document.getElementById(searchtype + '_fnError');
//                lnError = document.getElementById(searchtype + '_lnError');                             
//                stateError = document.getElementById(searchtype + '_stateError');
//                dlError = document.getElementById(searchtype + '_dlnError');
//                dobError = document.getElementById(searchtype + '_dobError');

                if(!ValidatePeople(searchtype))
                {
                    validform = false;   
                }
                //alert("Before ValidateDOB");
                if(!ValidateDOB(searchtype))
                {
                    validform = false;    
                }
                //alert("Before ValidiateDL");
                if(!ValidateDLNumber(searchtype))
                {
                    //alert("If ValidateDLNumber");
                    validform = false;  
                }
                //alert("After ValidiateDL");
                if(!ValidateState(searchtype))
                {
                    validform = false;   
                }
                //alert("dr end = " + validform);
            break;
            case "crim":
//                fnError = document.getElementById(searchtype + '_fnError');
//                lnError = document.getElementById(searchtype + '_lnError');                
//                ageError = document.getElementById(searchtype + '_ageError'); 
//                dobError = document.getElementById(searchtype + '_dobError');
               
                if(!ValidatePeople(searchtype))
                {
                    validform = false;   
                }
                if(!ValidateDOBorAge(searchtype))
                {
                    validform = false;    
                }
                else
                {
                    if(!ValidateDOB(searchtype))
                    {
                        //alert("crimif ValidateDOB " + validform); 
                        if(!ValidateAge(searchtype))
                        {
                            validform = false;
                            //alert("crimif valid age " + validform);   
                        }    
                    }
                }                   

            break;
            case "so":
//                fnError = document.getElementById(searchtype + '_fnError');
//                lnError = document.getElementById(searchtype + '_lnError');                
//                ageError = document.getElementById(searchtype + '_ageError');                
//                dobError = document.getElementById(searchtype + '_dobError');
                
                if(!ValidatePeople(searchtype))
                {
                    validform = false;   
                }
                if(!ValidateDOBorAge(searchtype))
                {
                    validform = false;    
                }
                else
                {
                    if(!ValidateDOB(searchtype))
                    {
                        //alert("crimif ValidateDOB " + validform); 
                        if(!ValidateAge(searchtype))
                        {
                            validform = false;
                            //alert("crimif valid age " + validform);   
                        }    
                    }
                }                               
            break;    
            case "fl":
//                fnError = document.getElementById(searchtype + '_fnError');
//                lnError = document.getElementById(searchtype + '_lnError');                
//                cityError = document.getElementById(searchtype + '_cityError');                
//                stateError = document.getElementById(searchtype + '_stateError');
//                //alert("FL Before validate People ");               
                if(!ValidatePeople(searchtype))
                {
                    validform = false;   
                }   
                //alert("FL after validate People ");               
                if(!ValidateCity(searchtype))
                {
                    validform = false;   
                } 
                if(!ValidateState(searchtype))
                {
                    validform = false;   
                } 
                //alert("case phone validform " + validform);
            break;  
            case "rpl":
                //alert("rpl Before validate phone ");               
                if(!ValidatePhone(searchtype))
                {
                    validform = false;   
                }   
                //alert("rpl after validate phone " + validform);
            break;          
        }
        
        return validform;
    }
 
    function ValidatePeople(searchtype)
    {
        var validpeople = true;
        var firstname = $('#' + searchtype + '_firstname').val(); 
        var lastname = $('#' + searchtype + '_lastname').val(); 
        //alert("ValidatePeople function start " + firstname);

        //if(firstname.length < 1  || !isAlpha(firstname))
        if(!isAlpha(firstname))
        {
        //alert("ValidatePeople function  start if firstname "); 
            $('#' + searchtype + '_fnErrormessage').html('first name is required');
            validpeople = false;  
            //alert("ValidatePeople function if firstname "); 
        }
        else
        {
            $('#' + searchtype + '_fnErrormessage').html('');
        }
        
        if(lastname.length < 1 || !isAlpha(lastname))
        {
            $('#' + searchtype + '_lnErrormessage').html('last name is required');
            validpeople = false;   
        }
        else
        {
            $('#' + searchtype + '_lnErrormessage').html('');
        }        
        
        //alert ("valid people " + validpeople);
        return validpeople;
    }

    function ValidateAge(searchtype)
    {
        //alert("ValidateAge Function start");
        var validage = true;
        var age = $('#' + searchtype + '_age').val(); 

        $('#' + searchtype + '_ageErrormessage').html('');

        if(isNaN(age))
        {
            //alert("validateage");
            $('#' + searchtype + '_ageErrormessage').html('Age must be a valid number');
            validage = false;   
        }
        else
        {
            if(searchtype == 'dr' || searchtype == 'crim' || searchtype == 'so')
            {
                $('#' + searchtype + '_dobErrormessage').html('');
                $('#' + searchtype + '_dob_1').val('');
                $('#' + searchtype + '_dob_2').val('');       
                $('#' + searchtype + '_dob_3').val('');  
            }
        }     

        return validage;
    }

    function ValidateState(searchtype)
    {
        var validstate = true;
        var state = $('#' + searchtype + '_state_select').val(); 
        //alert(state);

        if(state == 0)
        {
            $('#' + searchtype + '_stateErrormessage').html('required');
            validstate = false;   
        }
        else
        {
            $('#' + searchtype + '_stateErrormessage').html('');            
        }
        
        //alert ("valid state " + validstate);

        return validstate;
    }

    function ValidatePhone(searchtype)
    {
        //alert("VaidatePhone Begin");
        var validphone = true;
        var _phone = $('#' + searchtype + '_phone_number'); 
        var phone;
        var isOp=window.opera?1:0;
        
        //alert("isOp =" + isOp);
        if(_phone.val().length<1) 
        {
            phone="_"; 
                   
        }
        else
        {
            //alert(_phone.val());
            if(isOp!=1)
            {
                //alert('isOp != 1 ' + _phone.val());
                phone = detectPN(_phone);
                //alert('phone = ' + phone);
            }
            else
            {
                //alert('isOp == 1 ' + _phone.val());
                phone = _phone.val();    
            }
        }
            
        //alert(phone + " " + isNaN(phone) + " " + phone.length);
        var htmlmessage='';
        if(phone.length != 10 || isNaN(phone))
        {
            //alert(phone.length + " " + isNaN(phone));
            htmlmessage  ='valid phone number is required.';
            
            if(isOp==1)
            {
                htmlmessage += '<br /><br />Opera users should enter 10 numeric characters (no spaces, hyphens or parenthesis).';    
            }
            validphone = false;   
        }
        else
        {
            htmlmessage = '';            
        }
        

        //alert ("valid phone " + validphone);
        $('#' + searchtype + '_phoneErrormessage').html(htmlmessage);

        return validphone;
    }

	function ValidateCity(searchtype)
    {
        var validcity = true;
        var city = $('#' + searchtype + '_city').val(); 
        //alert("ValidateCity " + (city.length > 0 && !isAlpha(city)));

        if(city.length > 0 && !isAlpha(city))
        {
            //alert("ValidateCity " + !isAlpha(city))
            $('#' + searchtype + '_cityErrormessage').html('city must be alphabetic');
            validcity = false;   
        }
        else
        {
            $('#' + searchtype + '_cityErrormessage').html('');
        }
           
        //alert ("valid city " + validcity);
                        
        return validcity;
    }
    
     function ValidateDOB(searchtype)
    {
        var validdob = true;
        var dob1 = $('#' + searchtype + '_dob_1').val(); 
        var dob2 = $('#' + searchtype + '_dob_2').val(); 
        var dob3 = $('#' + searchtype + '_dob_3').val(); 
        
        $('#' + searchtype + '_dobErrormessage').html('');
            
        if(!isDate(dob1,dob2,dob3))
        {
            validdob = false;
            $('#' + searchtype + '_dobErrormessage').html('DOB is not a valid birth date');    
    
        }
        else
        {
            if(searchtype!='dr') $('#' + searchtype + '_age').val('');
        }
        
        if(validdob)
        {
            var d = new Date();
            var curr_date = d.getDate();
            var curr_month = d.getMonth() + 1;
            var curr_year = d.getFullYear();
            
            //alert(curr_month+ "/" +curr_date + "/" + curr_year);
            if(dob3 < 1900 || dob3 > curr_year)
            {
                validdob = false;
                $('#' + searchtype + '_dobErrormessage').html('DOB year must be between 1900 and '  +  curr_year);    
            }
                //alert(dob1 + curr_month + ' ' + dob2 + curr_date + ' ' + dob3 + curr_year);
            
            if(dob3 == curr_year && (dob2 > curr_date || dob1 > curr_month))
            {
                validdob = false;
                $('#' + searchtype + '_dobErrormessage').html('A future DOB is not allowed');    
            }
        }
                
        return validdob;
    } 
	
     function ValidateDOBorAge(searchtype)
    {
        var validDOBorAge = true;
        var dob1 = $('#' + searchtype + '_dob_1').val();
        var dob2 = $('#' + searchtype + '_dob_2').val();
        var dob3 = $('#' + searchtype + '_dob_3').val();
        var age = $('#' + searchtype + '_age').val();
        
        if(age.length<1) age="_"; 

//        alert("ValidateDOBorAge " + dob1+"/"+dob2+"/"+ dob3 + " age " + age);        
//        alert("ValidateDOBorAge " + isDate(dob1,dob2,dob3) + " age " + isNaN(age));        
        $('#' + searchtype + '_dobErrormessage').html('');
        
        if(!isDate(dob1,dob2,dob3))
        {
            if(!isNaN(age))
            {
                validDOBorAge = true;
                $('#' + searchtype + '_dobErrormessage').html(''); 
            } 
            else
            {
                validDOBorAge = false;
                $('#' + searchtype + '_dobErrormessage').html('DOB or Age must be entered'); 
            }     
    
        }
 
        
        //alert("validDOBorAge " + validDOBorAge);
        return validDOBorAge;
    } 	
    
	function ValidateDLNumber(searchtype)
    {
        //alert("ValidateDL " + searchtype);
        var validDLNumber = true;
        var dl = $('#' + searchtype + '_dl_number').val(); 
        var state = $('#' + searchtype + '_state_select').val(); 
       
        //alert("ValidateDLNumber start = " + dl + " " + !isAlphanum(dl) + " length = " + dl.length + " state = " + state);

        if(dl.length < 1 || !isAlphanum(dl))
        {
            //alert("ValidateDLNumber fail");
            $('#' + searchtype + '_dlnErrormessage').html('DL is required');
            validDLNumber = false;   
        }
        else
        {
            //$('#' + searchtype + '_dlnErrormessage').html('');
            var errorMessage='';
            errorMessage = ValidateDriverLicenseByState(state,dl);
            //alert(errorMessage);
            $('#' + searchtype + '_dlnErrormessage').html(errorMessage);
            if(errorMessage.length>1)
            {
                validDLNumber = false;
            }
        }
           
        //alert ("valid dln " + validDLNumber);
                        
        return validDLNumber;
    }
  
 //helper function to create the form
    function getNewSubmitForm()
    {
        //alert("getNewSubmitForm");
        var submitForm = document.createElement("FORM");
        document.body.appendChild(submitForm);
        submitForm.method = "POST";
        return submitForm;
    }       

//helper function to add elements to the form
    function createNewFormElement(inputForm, elementName, elementValue)
    {
        //alert("createNewFormElement " + elementName + " = " + elementValue);
        var newElement;
        if(navigator.appName=="Microsoft internet Explorer") 
        {
            newElement = document.createElement("<input name='" + elementName + "' type='hidden' />");
            //alert(newElement);
            inputForm.appendChild(newElement);
            newElement.value = elementValue;        
        }
        else
        {
           // alert("createNewFormElement else " + elementName + " = " + elementValue);

            newElement = document.createElement('input');
            newElement.setAttribute('name', elementName);           
            newElement.setAttribute('type', 'hidden');
            inputForm.appendChild(newElement);
            newElement.value = elementValue;
            //alert(newElement.name + " = " + newElement.value);
        }                                                  
        
        return newElement;
    } 
            
 //made by minidxer
 //For more visit http://ntt.cc
 // Trim() , Ltrim() , RTrim()
 // trim blank space at the string
 String.prototype.Trim = function()
 {
 return this.replace(/(^\s*)|(\s*$)/g, "");
 }
  
 // trim blank space at the beginning
 String.prototype.LTrim = function()
 {
 return this.replace(/(^\s*)/g, "");
 }
  
 // trim blank space at the end
 String.prototype.RTrim = function()
 {
 return this.replace(/(\s*$)/g, "");
 }
  