// JavaScript Document
// vfiApp object
var vfiApp={ 	
	// applicant mysql id
	app_id:0,
	// city in which the applicant lives, used to create the metro area
	city:null,
	// has the applicant committed a felony
	crime:'notSet',
	// has the applicant agreed to the confidentiality agreementment
	con:'notSet',
	// classic vfi fade
	fade: function(id) {
		var dom = document.getElementById(id),
			level = 1;
		function step () {
			var h = level.toString(16);
			dom.style.backgroundColor = '#FFFF' + h + h;
	
		if (level < 15) {
				level += 1;
				setTimeout(step, 100);
			}
		}
		
		if(dom!=null){
			setTimeout(step, 100);
		}
	}
};

document.observe("dom:loaded", function(){
	$('canada').onclick = function(){
		parent.location='application_ca.php';
	}
	// check for a modern browser									
	vfiApp.getInternetExplorerVersion();
	//	bind the setData function below to the input and textarea elements
	$$("input").each(function(elmt){ elmt.onkeyup = function(){ $(this).setData(this); }});
	$$("textarea").each(function(elmt){ elmt.onkeyup = function(){$(this).setData(this);}});
	$$("input").each(function(elmt){ elmt.onchange = function(){ $(this).setData(this); }});
	$$("textarea").each(function(elmt){ elmt.onchange = function(){$(this).setData(this);}});
	$$("input").each(function(elmt){ elmt.onblur = function(){ $(this).setData(this); }});
	$$("textarea").each(function(elmt){ elmt.onblur = function(){$(this).setData(this);}});
	
	// bind the fade to each element
	$$("input").each(function(elmt){ elmt.onfocus = function(){(vfiApp.fade(elmt.id));} });
	$$("textarea").each(function(elmt){ elmt.onfocus = function(){(vfiApp.fade(elmt.id));} });
	
	// hide the cell phone div
	$('cellDiv').hide();
	
	// create the app_id
	vfiApp.getAppId();
	
	// set the checkbox values
	vfiApp.onload();
});


/*
 *	Utilities to record every keystroke to the database
*/

var MyUtil = {
		
	setData : function(element){
		
		element = $(element);
		
		// on this form email2 is not sent to the mysql database
		if(element.id=='email2'){
			return;
		}
		
		// do not need this
		if(element.id=='agreement'){
			return;
		}
		
		
		if(element.type=="checkbox" || element.type=="button"){
			return; // checkboxes and buttons are handled diffently
		}else{
			// otherwise update `appsTemp`
			new Ajax.Request('code/changeAppTemp.php',
			{
				method:'post',
				parameters: {app_id: vfiApp.app_id, field:element.id, data:element.value },
				onSuccess: function(transport){
					var response = transport.responseText || "no response text";
					// vfi is a valid response
					
					if(response!='vfi'){
						alert(response);
					}
			},
				onFailure: function(){ alert('Something went wrong...') }
			});	
				
		}
	}
}	

//Attach the method
Element.addMethods(MyUtil);

vfiApp.getInternetExplorerVersion=function(){
   var url='code/verData.php';
	new Ajax.Request(url,
	{
		method:'post',
		parameters:{},
		onSuccess: function(transport){
		  var response = transport.responseText || "no response text";
		  var data=[];
		  data=response.split("~");
		  var num=parseFloat(data[1]);
		  if(data[0]=='ie'){
		  	if(num<7){
				window.location='obapp.php'; //send the old browser to the old php page
			}
		  }
		 
		},
		onFailure: function(){ alert('Something went wrong...') }
	  });
}

vfiApp.getAppId = function(){

	if(vfiApp.app_id===0){
		var url='code/createAppId.php';
			new Ajax.Request(url,
			{
				method:'post',
				parameters:{},
				onSuccess: function(transport){
				  var response = transport.responseText || "no response text";
				  vfiApp.app_id=response;
				 
				},
				onFailure: function(){ alert('Something went wrong...') }
			  });
	}
}


vfiApp.onload=function(){
	
	// PO Box boxes
	$('poYes').observe('click', function(e){
		$('poNo').checked=false;
		vfiApp.getCity();
	});

	$('poNo').observe('click', function(e){
		$('poYes').checked=false;
		vfiApp.getCity();
	});

	
	
	// felony checkboxes
	$('crimeYes').observe('click', function(e){
		vfiApp.crime='Yes';
		$('crimeNo').checked=false;
	});
	$('crimeNo').observe('click', function(e){
		vfiApp.crime='No';
		$('crimeYes').checked=false;
	});
	
	
	// confidentiality boxes
	$('conYes').observe('click', function(e){
		vfiApp.con='Yes';
		$('conNo').checked=false;
	});
	$('conNo').observe('click', function(e){
		vfiApp.con='No';
		$('conYes').checked=false;
	});
	
	// cell phone boxes
	$('phoneYes').observe('click', function(e){
		$('phoneNo').checked=false;
		$('cellDiv').show();
		$('cell').activate();
	});

	$('phoneNo').observe('click', function(e){
		$('phoneYes').checked=false;
		$('cellDiv').hide();
	});

}




vfiApp.getCity=function(){

	var el=$('zipcode');

	if(el.value.length==5){
		var url='code/getCity.php';
			new Ajax.Request(url,
			{
				method:'post',
				parameters:{zipcode:el.value, app_id:vfiApp.app_id},
				onSuccess: function(transport){
				  var response = transport.responseText || "no response text";
				  $('cityDiv').update(response);
				  var data=[];
				  data=response.split(",");
				  var x="(For example, you can list zip codes or the, '"+data[0]+"  metro area' or by local Area Codes.";
				  x+="<strong> Please also let us know your availablity, how many days per week and how many hours per week.</strong>)";
				  $('metro').update(x);
				 
				},
				onFailure: function(){ alert('Something went wrong...') }
			  });
	
	}

}

vfiApp.emailCheck = function(){
	var one=$('email').value;
	var two=$('email2').value;
	
	if(one===two){
	}else{
		alert('Emails do not match!');
		$('email').activate();
	}
	
	
}

vfiApp.errorCheck = function(){
	
		
	if(!$('first_name').present()){
		alert('Please enter your first name');
		$('first_name').activate();
		return false;
	}
	
	if(!$('last_name').present()){
		alert('Please enter your last name');
		$('last_name').activate();
		return false;
	}
	
	if(!$('address').present()){
		alert('Please enter your address');
		$('address').activate();
		return false;
	}
	
	if(!$('zipcode').present()){
		alert('Please enter your zip code');
		$('zipcode').activate();
		return false;
	}
	
	if($('poYes').checked==false && $('poNo').checked==false){
		alert('Is your Zip Code For a PO Box? [5]');
		return false;
	}
	
	
	if(!$('phone').present()){
		alert('Please enter your home phone');
		$('phone').activate();
		return false;
	}
	
	if($('phoneYes').checked==false && $('phoneNo').checked==false){
		alert('Do you have a cell phone?');
		return false;
	}
	
	if($('cellDiv').visible()){
		if(!$('cell').present()){
			alert('Please enter your cell phone number');
			$('cell').activate();
			return false;
		}
	}
	
	if(!$('email').present()){
		alert('Please enter your email');
		$('email').activate();
		return false;
	}
	
	if(!$('email2').present()){
		alert('Please reenter your email');
		$('email2').activate();
		return false;
	}

	if(!$('previous').present()){
		alert('Please list any experiences that may be pertinent');
		$('previous').activate();
		return false;
	}
	
	if(!$('comments').present()){
		alert('Please tell us the areas in which you can perform inspections');
		$('comments').activate();
		return false;
	}
	
	if($('crimeYes').checked==false && $('crimeNo').checked==false){
		alert('Have you ever been convicted of a felony?');
		return false;
	}	

	if($('conYes').checked==false && $('conNo').checked==false){
		alert('Please check if you accept the terms\nof the Confidentiality Agreement');
		return false;
	}
	
	if($('conNo').checked==true){
		var m="Thank you for considering\ncontracting with VFI ";
		m+="but, acceptance of the\nConfidentality Agreement";
		m+="is required for consideration.\nYour application will not be submitted.";
		alert(m);
		return false;
	}else{
		vfiApp.getClosest();
	}
}

vfiApp.getClosest = function(){
	var zip=$('zipcode').value;
	var url='code/closest.php';
	new Ajax.Request(url,
	{
		method:'post',
		parameters:{app_id:vfiApp.app_id,zip:zip},
		onSuccess: function(transport){
		  var response = transport.responseText || "no response text";
		  if(response!='vfi'){
		  	alert(response);
		  }else{
		  	vfiApp.finishUp();
		  }
		 
		},
		onFailure: function(){ alert('Something went wrong...') }
	  });

}

vfiApp.finishUp = function(){
	
	parent.location='finishUp.php?app_id='+vfiApp.app_id;

}