    var oktocontinue = true;
    var strmess = "";
    var emess = "";

$(document).ready(function(){

    $("#sel_industry").change(function(){
       var val_industry = $("#sel_industry").val();
       if (val_industry == "Other") {
           $("#company_details_other").show();
           $("#txt_co_details_answer").val('');
       } else {
           $("#company_details_other").hide();
           $("#txt_co_details_answer").val(val_industry);  
       } 
    });
    
    $("#txt_co_details").change(function(){
       var val_industry = $("#sel_industry").val();
       if (val_industry == "Other") $("#txt_co_details_answer").val($("#txt_co_details").val());  
    });
    
    $("#txa_proj_description").keyup(function(){ 
        var wdcount = CountWords (this);
        $("#wdct_description").val(wdcount);
    });
	
	$("#txa_resources").keyup(function(){ 
        var wdcount = CountWords (this);
        $("#wdct_resources").val(wdcount);
    });
    
    $("#txa_bus_problem").keyup(function(){
          var wdcount = CountWords (this);
          $("#wdct_bus_problem").val(wdcount);
    });
    
    $("#txa_exemplary_technology").keyup(function(){
          var wdcount = CountWords (this);
          $("#wdct_exemplary_technology").val(wdcount);
    });
    
    $("#txa_bus_benefits").keyup(function(){
          var wdcount = CountWords (this);
          $("#wdct_bus_benefits").val(wdcount);
    });
    
    $("#txa_product_solved").keyup(function(){
          var wdcount = CountWords (this);
          $("#wdct_product_solved").val(wdcount);
    });
    
    $("#txa_other_contrib").keyup(function(){
          var wdcount = CountWords (this);
          $("#wdct_other_contrib").val(wdcount);
    });
    
    function CountWords (this_field, show_word_count, show_char_count) {
        var char_count = this_field.value.length;
        var fullStr = this_field.value + " ";
        var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
        var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
        var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
        var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
        var splitString = cleanedStr.split(" ");
        var word_count = splitString.length -1;
        if (fullStr.length <2) {
            word_count = 0;
        }
        return word_count;
    }
    
    $("#btn_submit").click(function(){
        oktocontinue = true;
        validatetxt($("#txt_name"));  			// co_name
        validatetxt($("#txt_headquarters"));  	// hqheadquarters
        validatetxt($("#txt_url")); 			// url
        validatetxt($("#txt_projleader"));		// projleader 
        validatetxt($("#txt_projtitle"));		// projtitle
        validatetxt($("#txt_email"));			// email
        validatetxt($("#txt_phone"));			// phone
        validatetxt($("#txt_mail_address"));	// mail_address
		validatetxt($("#txt_city"));			// mail_address
		validatetxt($("#txt_state"));			// mail_address
		validatetxt($("#txt_zip"));				// mail_address
		validatetxt($("#txa_resources"));		// resources
        validatetxt($("#txa_bus_problem"));		// bus_problem
        validatetxt($("#txa_products_deployed1")); 	// products_deployed_1
		validatetxt($("#txa_product_solved"));	// product_solved
        validatetxt($("#txa_exemplary_technology"));	// examplary_technology
        validatetxt($("#txa_bus_benefits"));	// bus_benefits
        validatetxt($("#txt_cost"));			// cost
        validatetxt($("#txt_return_1_year"));	// return_1_year
        validatetxt($("#txt_return_3_year")); 	// return_2_year
        validatetxt($("#txt_criteria")); 		// criteria
        validatetxt($("#txa_other_contrib")); 	// other_contrib
        //alert("End of text / textarea");
        
		if($("#expected_month").val() == "null" || $("#expected_year").val() == "null") {
        	validatetxt($("#sel_month"));
        	validatetxt($("#sel_year"));
		} else {
			$("#sel_month").css('backgroundColor','#FFFFFF');;
        	$("#sel_year").css('backgroundColor','#FFFFFF');
		}
		
		if($("#sel_month").val() == "null" || $("#sel_year").val() == "null") { 
			//alert('test'); 
			validatetxt($("#expected_month"));
			validatetxt($("#expected_year"));
		} else {
			$("#expected_month").css('backgroundColor','#FFFFFF');;
        	$("#expected_year").css('backgroundColor','#FFFFFF');
		}
        
        validatetxt($("#sel_num_company_employees"));
        validatetxt($("#sel_gross_revenue"));
        //alert("End of selects");
        
        // select with other text box 
        var strx = $("#txt_co_details_answer").val();
        if (strx == "") {
            oktocontinue = false;
            $("#sel_industry").css('backgroundColor','#FEE0F0');
        } else {
            $("#sel_industry").css('backgroundColor','#FFFFFF');  
        }
                          
        var bolradio = valButton(mysurvey.team_member);
        if(bolradio == null) {
            oktocontinue = false;
            $("#team_member").css('backgroundColor','#FEE0F0');
        } else {
            $("#team_member").css('backgroundColor','#FFFFFF');  
        }
        //alert("End of radios");
		
		var a = parseInt($("#wdct_resources").val(), 10);
        if(a > 50) {
            oktocontinue = false;
            strmess = '\nWord count of some fields must be decreased.';
            if($("#wdct_description").val() > 1) $("#txa_resources").attr('backgroundColor','#FEE0F0');  
        }
        
		var b = parseInt($("#wdct_product_solved").val(), 10);
        if(b > 250) {
            oktocontinue = false;
            strmess = '\nWord count of some fields must be decreased.';
            $("#txa_product_problem").attr('backgroundColor','#FEE0F0');
        }
        
		var c = parseInt($("#wdct_exemplary_technology").val(), 10);
        if(c > 250) {
            oktocontinue = false;
            strmess = '\nWord count of some fields must be decreased.';
            $("#txa_exemplary_technology").attr('backgroundColor','#FEE0F0'); 
        }
        
		var d = parseInt($("#wdct_product_solved").val(), 10);
        if(d > 250) {
            oktocontinue = false;
            strmess = '\nWord count of some fields must be decreased.';
            $("#txa_product_solved").attr('backgroundColor','#FEE0F0'); 
        }
        
		var e = parseInt($("#wdct_bus_benefits").val(), 10);
        if(e > 500) {
            oktocontinue = false;
            strmess = '\nWord count of some fields must be decreased.';
            $("#txa_bus_benefits").attr('backgroundColor','#FEE0F0'); 
        }
        
		var f = parseInt($("#wdct_other_contrib").val(), 10);
        if(f > 500) {
            oktocontinue = false;
            strmess = '\nWord count of some fields must be decreased.';
            $("#txa_other_contrib").attr('backgroundColor','#FEE0F0'); 
        }
        
        //validate emails
        var bolfirst = echeck($("#txt_email").val());
        if (!bolfirst) {
            oktocontinue = false;
            emess = "\nIncorrect email address were found.";
            $("#txt_email").css('backgroundColor','#FEE0F0'); 
        }
        
        //validate emails
        var secemail = $("#txt_submitter_email").val();
        if (secemail != "") {
            var bolsecond = echeck(secemail);
            if (!bolsecond) {
                oktocontinue = false;
                emess = "\nIncorrect email address were found.";
                $("#txt_submitter_email").css('backgroundColor','#FEE0F0'); 
            }
        }
        
        if(!oktocontinue) {
            var stralert = 'Highlighted fields are required and must be completed to submit nomination.';
            if(strmess != "") stralert += strmess;
            if(emess != "") stralert += emess;
            alert(stralert);
            return false;                   
        } else {
            addnull($("#txa_products_deployed2"));
            addnull($("#txa_products_deployed3"));
            addnull($("#txt_submitter_name"));
            addnull($("#txt_submitter_title"));
            addnull($("#txt_submitter_email"));
            addnull($("#txt_submitter_phone"));
        }
		//return false;
    });
    
    function addnull(ctr) {
        var tval = ctr.val();
        tval = trim(tval, " ");
        if(tval.length == 0) {
            ctr.val("null");
        }
    }
    
    function validatetxt(ctr) {
        var tval = ctr.val();
        tval = trim(tval, " ");
        if(tval.length == 0 || tval =="null") {
            oktocontinue = false;
            ctr.val('');
            ctr.css('backgroundColor','#FEE0F0');
        } else {
           ctr.css('backgroundColor','#FFFFFF');  
        } 
    }
    
    function trim(str, chars) {  
        return ltrim(rtrim(str, chars), chars);  
    }  

    function ltrim(str, chars) {  
        chars = chars || "\\s";  
        return str.replace(new RegExp("^[" + chars + "]+", "g"), "");  
    }  

    function rtrim(str, chars) {  
        chars = chars || "\\s";  
        return str.replace(new RegExp("[" + chars + "]+$", "g"), "");  
    }
    
    function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
        else return null;
    }
                  
    function echeck(str) {
        var at="@"
        var dot="."
        var lat=str.indexOf(at)
        var lstr=str.length
        var ldot=str.indexOf(dot)
        if (str.indexOf(at)==-1) return false;
        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
        if (str.indexOf(at,(lat+1))!=-1) return false;
        if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
        if (str.indexOf(dot,(lat+2))==-1) return false;
        if (str.indexOf(" ")!=-1) return false;
        return true                    
    }           
    
});
