﻿
	//formType - if contains data, the subject is not displayed in form - otherwise its validated.
	var formType;
	var initForm = false; //content contains override for this variable to initilize the form - that was we can use form in multiple pages.

	//user may change this assuming they edit the associative array correctly - but its globally shared for all pages that include it (currently section level)
	var cboServices = { 
		"La Banque d'Affaires": { "items": [["Banque d'investissement (Corporate finance)","6"],["Marchés primaires actions (ECM)","6"]], "ss":true, "interview":Date.UTC(2007,10,11,0,0,0) },
		"La Gestion": { "items" : [["Rothschild Patrimoine","3"],["La Gestion institutionnelle","3"],["La Gestion alternative","3"],["Sélection R","3"]], "ss":false , "interview":Date.UTC(2007,10,11,0,0,0) },
		"Autres Services": { "items" : [["Toutes les positions","3"]], "ss":true, "interview":Date.UTC(2007,10,11,0,0,0) } 

	};


	//this is our page initialisation
	$( function() { 
		//alert("initForm:" + initForm);

		if( initForm )
		{
			$("#subject").val(formType); 
			if(formType==""){$("#subjectwrapper").show();} else {$("#subjectwrapper").hide(); } 
			$("#ssradio").hide(); 
			$("#filedata").hide();
			$("#formerrors").hide();
		}

  	} );



//these functions should not need editing by user unless different functionality is required

//pretend click/change to do the initial load of 2nd combo based upon 1st combo value
$( function() { 
	if( $("#services").length > 0 )
		{
		reloadCombo( $("#services")[0], cboServices, "selection");  
		}
	} );



//this will populate our slave combo from the passed in associative array - if we find no data we hide the combo
function reloadCombo(sender, data, targetname)
{
	var target = $("#"+targetname)[0];
	target.options.length = 0;
	var source;
	var servicedata;

	servicedata = data[sender.options[sender.selectedIndex].text];
	if( servicedata != undefined)
	{
		source = servicedata["items"];
	}
	var isdata = false;

	if(source != undefined)
	{ 
		isdata = true;
		if( source.length == 0 )
		{
			isdata = false;
		}
	}


	if(isdata)
	{
		
		$("label[@for='" + targetname + "']").show();
		$(target).show();
			
		
		$(source).each( function(i) { target.options[target.options.length] = new Option(this[0],this[1])  ;  } );
	} else {
		$("label[@for='" + targetname + "']").hide();
		$(target).hide();
	}

	//See if we also display 'ss'
	if( servicedata != undefined)
	{
		if( formType != "")
		{
			$("#filedata").show();

			var ss = servicedata["ss"];

			if(ss)
			{
				$("#ssradio").show();
			} else {
				$("#ssradio").hide();
			
			}

			if( $("#services")[0].selectedIndex == 1)
			{
				$("#ss").each( function(i) {this.checked = false; });
			} else {
				$("#ss")[0].checked = true;
			}
	
			var ssdate = servicedata["interview"];
			var obj = $("input[@name='ss']");
			var dt = new Date();
			$(obj).click( function() { displayssmessage(false) } );
			if( dt < ssdate )
			{
				$(obj[1]).click( function() { displayssmessage(true) } );
			}

			$("#subject").val("Candidature spontanée");
		}
		

	}
	
}


//validate our form entries upon button press
function validate()
{
	var valid = true;
	$("#formerrors").html("");
	$("#formerrors").hide();
	$(".unvalidated").each( function(i) { $(this).removeClass("unvalidated"); } );


	if( $("#name").val().replace(" ","") == "" )
	{
		valid = false;
		try {
		$("label[@id='for_name']").addClass("unvalidated"); 
		} catch(ex) {}
		$("#formerrors").append("nom: nécessité ne pas être vide<br/>");
	}

	if( $("#FromEmail").val().replace(" ","") == "" )
	{
		valid = false;
		try {
		$("label[@id='for_FromEmail']").addClass("unvalidated"); 
		} catch(ex) {}
		$("#formerrors").append("e-mail: nécessité ne pas être vide<br/>");
	}

	if( $("#message").val().replace(" ","") == "" )
	{
		valid = false;
		try {
		$("label[@id='for_message']").addClass("unvalidated"); 
		} catch(ex) {}
		$("#formerrors").append("message: nécessité ne pas être vide<br/>");
	}

	if( $("#subject").val().replace(" ","") == "" )
	{
		valid = false;
		try {
		$("label[@id='for_subject']").addClass("unvalidated"); 
		} catch(ex) {}
		$("#formerrors").append("sujet: nécessité ne pas être vide<br/>");
	}

	var ext = "";
	var fname;
	fname = $("#File").val().replace(" ","");

	if( fname.length > 0)
	{
		var pos = fname.lastIndexOf(".");
		if(pos>0)
		{
			ext = fname.substr(pos,fname.length);
		}

	}

	if( fname.length > 0)
	{
		switch (ext.toLowerCase())
		{
			case ".pdf" :{break;}
			case ".doc" :{break;}
			case ".rtf" :{break;}
			case ".txt" :{break;}
			default:
				{
					valid = false;
					try {
					$("label[@for='File']").addClass("unvalidated"); 
					} catch(ex) {}
					$("#formerrors").append("CV: [DOC,RTF,TXT,PDF]<br/>");
				}
		}

	}



	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	if( ! regex.test( $("#FromEmail").val() ) )
	{
		valid = false;
		try {
		$("label[@id='for_FromEmail']").addClass("unvalidated"); 
		} catch(ex) {}
		$("#formerrors").append("e-mail: format invalid<br/>");
	}


	if( valid)
	{

		//we now do a little message customizing
		var newdata = $("#name").val() + " (" + $("#FromEmail").val() + ")"
		$("#message_de").val( newdata );

		if( formType!="")
		{

			//customise subject
			if( $("#services")[0].selectedIndex == 1)
			{
				newdata = $("#selection")[0].options($("#selection")[0].selectedIndex).text;
				$("#subject").val( $("#subject").val() + " - " + newdata );
			}
			//customise emailto
			$("#toemail").val( $("#selection").val() );  

		}


		$("#mail")[0].submit();
	} else {
		$("#formerrors").show();
	}

	return false;

}


//When ss clicked, determine if we display a message
function displayssmessage(showit)
{
	if(showit) 
	{
		//$("#formerrors").html("Si votre candidature est retenue, le premier tour d'entretiens se déroulera les 10 et 11 novembre 2007.");
		//$("#formerrors").show();
	} else {
		$("#formerrors").html("");

	}
}



