	//GLOBALVARS
	var countries; 
	var schedules;
    var formValues = null;
    
  $(document).ready(function() {
            initApplicationForm();
  });
  

  function initApplicationForm()	{
	
	// INITIALIZE COUNTRY DESTINATION VALUES
	countries = new Array();
	schedules = new Array();
	countries["NULL"] = $("#destinationValuesCTA").val();
	var lines = $("#destinationValues").val().split("\n");
	
	for (var i=0;i<lines.length;i++)	{
		var data = lines[i].split("|");
		if (data.length == 2)	{
			data[0] = jQuery.trim(data[0]);
			countries[data[0]]=data[0]; //inserting the country
			var scheds = data[1].split(",");
			schedules[data[0]] = new Array();
			for (var j=0;j<scheds.length;j++)	{
				scheds[j] = jQuery.trim(scheds[j]);
				schedules[data[0]][scheds[j]] = scheds[j];
			}
		
		}//data has two columns
	
	}// end foreach line
	
	$("#destination").removeOption(/./);
	$("#destination").addOption(countries, false);
	update_schedulelist();
	update_schoolyear();
	update_titles();
	update_interviewLocation();
	update_DiscvoveryThru();
	update_bMonth();
	update_byear();
	update_credMethods();
	update_repeated_class_vals()
	update_ppp_scholarship_vals()
	var actionURI = window.location.pathname + "/?action=sendapplication";
	document.sendapplication.action = actionURI.replace(/\/\//g, "/");
	fill_entries();
	remove_unnecessary_fields();
	
  }// end function initApplicationForm
	
  function remove_unnecessary_fields() {
	
	$(".formlabel:contains('NOTAVAILABLE')").each(function(i) {

	    if(($(this).parent("tr:contains('NOTAVAILABLE')").length*2) >= $(this).parent("tr").children("td").length){
	        $(this).parent("tr").css("display","none");
	    } else    {
	        $(this).html("");
	        $(this).next("td").html("");        
	    }
	});
  
  }

  function update_schedulelist()	{

	if ($("#destination").val()!= "NULL")	{

		$("#duration").removeOption(/./);
		$("#duration").addOption(schedules[$("#destination").val()], false);

	} else	{

		$("#duration").removeOption(/./);
		$("#duration").addOption("NULL",$("#durationCTA").val());

	}

  }

  function update_schoolyear()	{
  
	var d = new Date();
	var curr_year = d.getFullYear();
	$("#schoolyear").removeOption(/./);
	$("#schoolyear").addOption(curr_year + " - " + (curr_year + 1),curr_year + " - " + (curr_year + 1),true);
	$("#schoolyear").addOption((curr_year + 1) + " - " + (curr_year + 2),(curr_year + 1) + " - " + (curr_year + 2),false);
	
  }
  
  function update_titles()	{
  
	var tmp_titles = $("#teachTitleValues").val().split(",");
	var titles = new Array();
	
	for(var i=0;i<tmp_titles.length;i++)	{
		var title = jQuery.trim(tmp_titles[i]);
		if (title.length > 0)
			titles[title] = title;
	}	
	//$("#teachTitle").removeOption(/./);
	$("#teachTitle").addOption(titles, false);
  }

 function update_repeated_class_vals()	{
	$("#class_repeated_yes_label").html($("#yes").val());
	$("#class_repeated_no_label").html($("#no").val());
  }

 function update_ppp_scholarship_vals()	{
	var values = new Array();
	$("#ppp_scholarship_yes_label").html($("#yes").val());
	$("#ppp_scholarship_no_label").html($("#no").val());
  }
  
  function update_interviewLocation()	{
  
	if ($("#interviewLocationValues").val().length > 0)	{	
		var tmp_locations = $("#interviewLocationValues").val().split(",");
		var locations = new Array();
	
		for(var i=0;i<tmp_locations.length;i++)	{
			var location = jQuery.trim(tmp_locations[i]);
			if (location.length > 0)
				locations[location] = location;
		}	
		$("#interviewLocation").removeOption(/./);
		$("#interviewLocation").addOption(locations, false);
  	} else	{
		$("#interviewLocation").hide();
	}
  
	}
  
  function update_DiscvoveryThru()	{
  
	var tmp_discs = $("#discvoveryThruValues").val().split(",");
	var discs = new Array();
	
	for(var i=0;i<tmp_discs.length;i++)	{
		var disc = jQuery.trim(tmp_discs[i]);
		if (disc.length > 0)
			discs[disc] = disc;
	}	
	$("#discvoveryThru").removeOption(/./);
	$("#discvoveryThru").addOption(discs, false);
  }  
  
  function update_bMonth()	{
  
	var tmp_bmons = $("#bmonthValues").val().split(",");
	var bmons = new Array();
	
	for(var i=0;i<tmp_bmons.length;i++)	{
		var bmon = jQuery.trim(tmp_bmons[i]);
		if (bmon.length > 0)
			bmons[i+1] = bmon;
	}	
	$("#bmonth").removeOption(/./);
	$("#bmonth").addOption(bmons, false);
  }
  
  function update_byear()	{
  
	var d = new Date();
	var curr_year = d.getFullYear();
	$("#byear").removeOption(/./);
	for(var i=18;i>12;i--)	{
		$("#byear").addOption((curr_year - i),(curr_year - i),false);
	}
	
  }
  
  function update_credMethods()	{
  
	var tmp_creds = $("#credentialDeliveryValues").val().split(",");
	var creds = new Array();
	
	for(var i=0;i<tmp_creds.length;i++)	{
		var cred = jQuery.trim(tmp_creds[i]);
		if (cred.length > 0)
			creds[cred] = cred;
	}	
	$("#credentialDelivery").removeOption(/./);
	$("#credentialDelivery").addOption(creds, false);
  }
  
  function validate_entries()	{
	
	
	var valid=true;
	var mandatory_fields = new Array("destination","duration","schoolyear","lastname","firstname","str_no",
									 "zip","city","phone","bday","bmonth","byear","email","nation","parent",
									 "school","school_zip","school_city","teach_title","teach_lastname","classgrade",
									 "eng_grade","avrg_grade","credentialDelivery","selfdescription");
	// first check parents apporval
	if(document.sendapplication.parent_ok.checked == false)	{
		alert($("#errormsg").val());
		document.sendapplication.parent_ok.focus();
		return false;
	}//check radios
	else if(document.sendapplication.gender[0].checked == false && document.sendapplication.gender[1].checked == false)	{
		alert($("#errormsg").val());
		//document.sendapplication[gender[0]].focus();
		return false;
	}
	else	{
		// check rest
		for(var i=0;i<mandatory_fields.length;i++)	{
			
			if(document.sendapplication[mandatory_fields[i]].value == "" || document.sendapplication[mandatory_fields[i]].value == "NULL")	{
				alert($("#errormsg").val());
				document.sendapplication[mandatory_fields[i]].focus();
				return false;
			}
			
		}
	}
	
	return true;
	
  }

  function fill_entries(){
      if(formValues){
          jQuery.each(formValues,
            function(){
                var currSelect = this;

                //Highlight invalid required fields
                markInvalid(currSelect);

                //Fill in form fields
                
                switch(currSelect.type){
                    case "text":
                        $("[@name="+currSelect.name+"]").attr("value",currSelect.value);
                        break;
                    case "select":                        
                        $("[@name="+currSelect.name+"] option").each(
                            function(i){
                                if(this.value == currSelect.value){
                                    this.selected = true;
                                }                                
                            }
                        );
                        if(currSelect.name == "destination"){
                            update_schedulelist();
                        }
                        break;
                    case "checkbox":
                        if(currSelect.value != ""){
                            $("[@name="+currSelect.name+"]").attr("checked","checked");
                        }
                        break;
                    case "radio":
                        $("[@name="+currSelect.name+"]").each(
                            function(i){
                                if(this.value == currSelect.value){
                                    this.checked = true;
                                }
                            });
                        break;
                    case "textarea":
                        $("[@name="+currSelect.name+"]").attr("value",currSelect.value);
                        break;
                }
            }
         );
      }
  }

function markInvalid(formField){
    var invalidColor = "#f4c0c0";
    if(formField.valid === false){
        switch(formField.type){
            case "text":
                $("[@name="+formField.name+"]").css("background-color",invalidColor);
                break;
            case "select":
                $("[@name="+formField.name+"]").css("border","4px solid "+invalidColor);
                break;
            case "checkbox":
                $("[@name="+formField.name+"]:first").parent().css("background-color",invalidColor);
                break;
            case "radio":
                if(formField.name == "class_repeated"){
                    $("[@name="+formField.name+"]:first").parent().parent().css("background-color",invalidColor);
                }else{
                    $("[@name="+formField.name+"]").parent().css("background-color",invalidColor);
                }
                break;
            case "textarea":
                $("[@name="+formField.name+"]").css("background-color",invalidColor);
                break;     
        }
    }
}
function validate_entries2()	{
    return true;
}
