var SEPARATOR = ":";
$(window).load(function(e){
    // INITIAL ASI CHECK    
    $("input[@name=asi]:eq(1)", form).attr("checked", "checked");
    $("p#asi_number_c", form).hide();

    $("input[@name=asi]:eq(0)", form).click(function(e){
        $("p#asi_number_c", form).show();
        $("input[@name=asi_number]").val("")[0].focus();
    });
    $("input[@name=asi]:eq(1)", form).click(function(e){
        $("p#asi_number_c", form).hide();
    });
    
    // limit textareas
    limitTextAreaLength(form["keywords"], 250);
    limitTextAreaLength(form["description"], 250);
    limitTextAreaLength(form["comments"], 400);


    $(form["owned_by"]).change(function(e){
        var selected = $(this).val();
        
        $("#type_c").hide();
        $("#busPhone_c").hide();
        
        if(selected == "business")
        {
            $("#busPhone_c").show();
        }
        else if(selected == "organization")
        {
            $("#type_c").show();
            $("#busPhone_c").show();
        }
    });

    // form validation
    $(form).submit(function(e){
    
        $("input", this).each(function(i){
            $(this).removeClass("invalid");
        });
        $("select", this).each(function(i){
            $(this).removeClass("invalid");
        });
        $("textarea", this).each(function(i){
            $(this).removeClass("invalid");
        });
        
        
        var field;
        
        var invalid = new Array();
        field = $(this["submitted"])[0];
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["owned_by"])[0];
        if(!$(field).val())
            invalid.push(field);

        var busPhoneRequired = false;
        if($(field).val() == "business" || $(field).val() == "organization")
        {
            var field1 = $(this["type"])[0];
            var field2 = $(this["type"])[1];
            var field3 = $(this["type"])[2];
            var field4 = $(this["type"])[3];
            
            if(!($(field1).attr("checked") ||
                 $(field2).attr("checked") ||
                 $(field3).attr("checked") ||
                 $(field4).attr("checked")
            ))
                invalid.push(field4);
            
            busPhoneRequired = true;
        }
        else if($(field).val() == "individual")
        {
            // uncheck all options
            $("input[@name=type]", this).attr("checked", "");
            $("input[@name=busPhone]", this).val("");
        }
        ////////////////////////////////////
        //var busPhoneRequired = false;
        //if($(field).val() == "business" || $(field).val() == "organization")
        //    busPhoneRequired = true;
        ////////////////////////////////////
        
        field = $(this["asi"])[0];
        var checked = $(field).attr("checked");
        field = $(this["asi_number"])[0];
        if(checked && !$(field).val())
            invalid.push(field);
        
        field = $(this["fname"])[0];
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["lname"])[0];
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["email"])[0];
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["phone"])[0];
        if(!$(field).val())
            invalid.push(field);
        
        if(busPhoneRequired)
        {
            field = $(this["busPhone"])[0];
            if(!$(field).val())
                invalid.push(field);
        }
        
        field = $(this["webname"])[0];
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["webaddress"])[0];
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["country"])[0];
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["language"])[0];
        if(!$(field).val())
            invalid.push(field);
        
        
        // add `more` fields
        for(var cbName in MoreCombos)
        {
            var extra = cbName+"_extra";
            
            // remove from previous attemp
            try {
                $(form[extra]).remove();
            }
            catch(e){}
            
            // if `None` selected, no one option is submited
            if(MoreCombos[cbName]["none"] != undefined)
                continue;
            
            var items = new Array();
            for(var item in MoreCombos[cbName])
                if(MoreCombos[cbName][item])
                    items.push(item);
            
            if(items.length > 0)
                $(this).append("<input name='"+extra+"' type='hidden' value='"+items.join(SEPARATOR)+"'");
            else
                invalid.push(form[cbName]);
        }
        
        field = $(this["keywords"])[0];
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["description"])[0];
        if(!$(field).val())
            invalid.push(field);
        
        
        
        if(invalid.length > 0)
        {
            $(invalid).each(function(i){
                $(this).addClass("invalid");
            });
            invalid[0].focus();
            return false;
        }
        return true;
    });
});
