// JavaScript Document
function generate(x){

	var x = document.getElementsByTagName(x);
	var myString = new String();

	for(i = 0; i < x.length; i++){
			
		if(x[i].getAttribute('type') == 'checkbox'){
						
			if(x[i].checked){
					
				myString += '*  * ' + x[i].getAttribute('name') + ' * Yes ';				
						
			}else{
					
				myString += '*  * ' + x[i].getAttribute('name') + ' * No ';
					
			}
			
			
			
		}else{
			
			if(x[i].value == ''){
					
				myString += '*  * ' + x[i].getAttribute('name') + ' * ! ';				
						
			}else{
					
				myString += '*  * ' + x[i].getAttribute('name') + ' * ' + x[i].value;
					
			}
			
		}
	}
	
	return myString;

}


function getRequest(){
	
	var ajaxRequest;
	
	try{

		ajaxRequest = new XMLHttpRequest();
		
	} catch (e){
		
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}	

	return ajaxRequest;
	
}


function processMail(data){

	var ajaxRequest = getRequest();
		
		ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4){

			alert("Thank you for choosing Fout inc. \n We will contact you as soon as possible.");
			window.location = './';
						
		}
	}

	var queryString = "?time=" + new Date().getTime() + "&" + data.substr(0, data.indexOf(' ')) + "=" + data.substr(data.indexOf(' ') + 1, data.length);
	ajaxRequest.open("GET", "backEnd/email.php" + queryString, true);
	ajaxRequest.send(null);
		
}


function processResume(name, filePath){

	var ajaxRequest = getRequest();
		
		ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4){

			myData = ajaxRequest.responseText;
						
		}
	}

	var queryString = "?time=" + new Date().getTime() + "&name=" + name + "&filePath=" + filePath;
	ajaxRequest.open("POST", "backEnd/emailFile.php" + queryString, true);
	ajaxRequest.send(null);
	
	alert(queryString);
	
}

function checkForBlank(id){

	var check = new Boolean();
		check = false;
	
		if(	document.getElementById(id).value == ''){
		
			check = true;
		
		}
	
	return check;

}

function getValue(id){
		
	return document.getElementById(id).value;
	
}

function handleResume(){
	
	
	var fileCheck = new Boolean();
	var nameCheck = new Boolean();

	fileCheck = checkForBlank('dataFile');
	nameCheck = checkForBlank('name');
		
	if(fileCheck || nameCheck){
		
		alert("All fields must be filled to submit the form.");
		
	}else{
						
		processResume(getValue('name'), getValue('dataFile'));
		
	}
	
}