// Add some employment variables to the session
function add_employment(){
	// Load into variables
	var company_name 	 = escape(encodeURI(document.getElementById('employment_companyname').value));
	var job_title 		 = escape(encodeURI(document.getElementById('employment_jobtitle').value));
	var companytype 	 = escape(encodeURI(document.getElementById('employment_companytype').value));
	var county 			 = escape(encodeURI(document.getElementById('employment_county').value));
	var town 			 = escape(encodeURI(document.getElementById('employment_town').value));
	var start_day 		 = escape(encodeURI(document.getElementById('employment_start-day').value));
	var start_month 	 = escape(encodeURI(document.getElementById('employment_start-month').value));
	var start_year 		 = escape(encodeURI(document.getElementById('employment_start-year').value));
	var end_day 		 = escape(encodeURI(document.getElementById('employment_end-day').value));
	var end_month 		 = escape(encodeURI(document.getElementById('employment_end-month').value));
	var end_year 		 = escape(encodeURI(document.getElementById('employment_end-year').value));
	var responsibilities = escape(encodeURI(document.getElementById('employment_responsibilities').value));
	
	// Validation
	var start = new Date(Date.UTC(start_year,start_month,start_day,0,0,0)).getTime() / 1000;
	var end = new Date(Date.UTC(end_year, end_month, end_day,0,0,0)).getTime() / 1000;
	
	
	if(company_name.length == 0){
		alert('You must give a company name');
	} else if(job_title.length == 0){
		alert('You must give a job title');
	} else if(start_day.length == 0){
		alert('You must select a start day');
	} else if(start_month.length == 0){
		alert('You must select a start month');
	} else if(start_year.length == 0){
		alert('You must select a start year');
	} else if(end_day.length == 0){
		alert('You must select a end day');
	} else if(end_month.length == 0){
		alert('You must select a end month');
	} else if(end_year.length == 0){
		alert('You must select a end year');
	} else if(start >= end ){
		alert('The end date must be later than start date');
	} else {
	
		// AJAX call to add job
		var http = new XMLHttpRequest();
		
		var url = "/_cv_employment.php";
		var params = "company_name="+ company_name +"&job_title="+ job_title +"&companytype="+ companytype +"&county="+ county +"&town="+ town +"&start_day="+ start_day +"&start_month="+ start_month +"&start_year="+ start_year +"&end_day="+ end_day +"&end_month="+ end_month +"&end_year="+ end_year +"&responsibilities="+ responsibilities;
		http.open("GET", url+"?"+params, true);
		http.onreadystatechange = function() {//Call a function when the state changes.
			if(http.readyState == 4 && http.status == 200) {
				alert(http.responseText);
				// Reload the employment list
				employment_list();
			}
		}
		http.send(null);
		
	}
}


// Load the list
function employment_list(){
	var http = new XMLHttpRequest();
	
	var url = "/_cv_employmentlist.php";
	var params = "var=blah";
	http.open("GET", url+"?"+params, true);
	http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200) {
			document.getElementById('employment_list').innerHTML = http.responseText;
		}
	}
	http.send(null);
}


// Remove from the list
function remove_employment(array_key){
	// Make a call to the PHP page that removes the employment records
var http = new XMLHttpRequest();
	
	var url = "/_cv_employmentrmv.php";
	var params = "array_key="+ array_key;
	http.open("GET", url+"?"+params, true);
	http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200) {
			alert(http.responseText);
			// Reload the employment list
			employment_list();
		}
	}
	http.send(null);
}
