
var STUDYLINK = {};

STUDYLINK.getSubject = function(code, label){

    for(i=0;i<subjectAreasList.length;i++){
    
        var subject = subjectAreasList[i];

	if(code!=null){

	    if(subject.code == code){ return subject.label; }
        }else if(label!=null){

            if(subject.label == label){ return subject.code; }
        }else{ return; }
    } 
}

STUDYLINK.isSubjectArea = function(sa){

	for(i=0; i<subjectAreasList.length; i++){

		if( subjectAreasList[i].label == sa ){
			return true;
		}
	}

	return false;
}

STUDYLINK.deleteCriteria = function(code){

    $("input[name=subject-areas]").each(function(){

        if($(this).val() == code){ 

            $(this).val(""); 
            $("#"+code).remove();
        }
    });
}

STUDYLINK.subjectAlreadyAdded = function(code){

    var alreadyAdded = false;
    $("input[name=subject-areas]").each(function(){
        if( $(this).val() == code ){
	    alreadyAdded = true;
	}
    });
    return alreadyAdded;
}

STUDYLINK.getSubjectAppendHTML = function(code){

    var html = "<span id='" + code + "'>";
    html+= STUDYLINK.getSubject(code, null);
    html+= "<a href='#' onClick='STUDYLINK.deleteCriteria($code$);'><img src='http://media.studylink.com/xl/nwr/home/btn-delete.gif' hspace='5'></a><br/></span>".replace("$code$", '"' + code + '"');
    return html;
}

STUDYLINK.error = function(message){
    $("#message").html(message);
    $("#message").show();
}

/**
 * checks value is not empty, if it is show error message
 * and returns false.
 */
STUDYLINK.isValueNotEmpty = function(elements, messages){

    for(i=0;i<elements.length; i++){
       if(elements[i] == ""){
	    STUDYLINK.error(messages[i]);
	    return false;
	}
    }
    return true;
}


STUDYLINK.submitForm = function(formOptions){
        if(! ( $("input[name=subject-areas]").length > 0 ) && ( $("#subjectKeyword").val().length < 1 ) ){
	    STUDYLINK.error("Please choose a subject area.");
	    return;
	
	} else{
            
	    	var i = 0;
	    	$("input[name=subject-areas]").each(function(){
                	if( $(this).val().length > 0 ){ i++; }
	    	});

		var sk = $("#subjectKeyword").val();

	    	if( sk == "Enter subject keyword"){ STUDYLINK.error("Please choose a subject area."); return; }
	    
	    	if( (i<1) && (sk.length < 1) ){ STUDYLINK.error("Please choose a subject area."); return; }

		//TODO if sk not a valid subject area set keyword input
	    	if( sk.length > 0 && !STUDYLINK.isSubjectArea(sk) ){ 
	    
	    		$("input[name=subject-areas]").each(function(){
				$(this).val("");
			});

	    		$("input[name=keywords]:first").val( $("#subjectKeyword").val() ); 
		
	    	}
	}

	var locations =  $("input[name=study-locations]:checked").size();

	if($("select[name=study-locations]").val() != undefined){
		if( $("select[name=study-locations]").val().length > 4 ){
			locations = 1;
		}
	}

        if (formOptions != null && formOptions != "no-study-locations"){
		if(locations < 1){ 
			STUDYLINK.error("Please choose at least one study location."); 
			return; 
		}
	}

	var levels = 0;
	$("select[name=levels-of-study] option:selected").each(function(){
		if( $(this).val().length > 1){
			levels++;
		}
	});


	if(formOptions != null && formOptions != "no-level-of-study"){
		if(levels < 1){ 
			STUDYLINK.error("Please choose a level of study."); 
			return; 
		}
	}

	$("#search").submit();
}

// Read a page's URL params and return them as an associative array.
STUDYLINK.getUrlParams = function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++){
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

STUDYLINK.getUrlErrorVars = function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++){
        hash = hashes[i].split('=');
	if(hash[0] == "error"){
	        vars.push(hash[1]);
	}
        //vars[hash[0]] = hash[1];
    }
    return vars;
}

