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("").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"]);
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["owned_by"]);
        if(!$(field).val())
            invalid.push(field);

        var busPhoneRequired = false;
		if( $(field).val() == "business" || $(field).val() == "organization" ){
			busPhoneRequired = true;
		}else{
            $("input[@name=busPhone]", this).val("");
		}
		
        if($(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);
        } else {
            $("input[@name=type]", this).attr("checked", "");
		}
        
        field = $(this["asi"]);
        var checked = $(field).attr("checked");
        field = $(this["asi_number"]);
        if(checked && !$(field).val())
            invalid.push(field);
        
        field = $(this["fname"]);
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["lname"]);
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["email"]);
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["phone"]);
        if(!$(field).val())
            invalid.push(field);
        
        if(busPhoneRequired)
        {
            field = $(this["busPhone"]);
            if(!$(field).val())
                invalid.push(field);
        }
        
        field = $(this["webname"]);
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["webaddress"]);
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["country"]);
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["language"]);
        if(!$(field).val())
            invalid.push(field);
        
        // add `more` fields
        for(var cbName in MoreCombos)
        {
            var extra = cbName+"_extra";
            
            // remove from previous attempt
			field = $(this[extra]);
			field.val("");
            
            // 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)
				field.val(items.join(SEPARATOR));
            else
                invalid.push(form[cbName]);
        }

        field = $(this["keywords"]);
        if(!$(field).val())
            invalid.push(field);
        
        field = $(this["description"]);
        if(!$(field).val())
            invalid.push(field);

        if(invalid.length > 0)
        {
            $(invalid).each(function(i){
                $(this).addClass("invalid");
            });
            invalid[0].focus();
            return false;
        }
        return true;
    });
});

