window.onload = function(){

	DreamJob.initObjects();
	//new Ajax.Autocompleter('job','autoCompleteMenu','ajax/suggestions.php',{tokens: ','});
	var x = navigator;
	if(x.appName != "Microsoft Internet Explorer" && x.appVersion.substr(0,1) != "4"){
		document.getElementById("pass").type="text";
	}
	DreamJob.checkRefresh();

}

var __valid = [];
__valid['email'] = false;
__valid['pass'] = false;
__valid['job'] = false;
__valid['location'] = false;
__valid['distance'] = "value";
__valid['salary'] = '15';

var DreamJob = {
	_token: ",",
	_autoCompleteList: false,
	_autoCompleteKey: false,

	//required
	_email: false,
	_pass: false,
	_job: false,
	_location: false,
	//_distance_only: false,
	//_distance_within: false,
	_distance_within_value: false,
	//_salary_any: false,
	_salary: false,
	
	_find: false,
	_rss: false,
	_out: false,
	
	//optional
	_graduate: false,
	_perks: false,
	_industry: false,
	_company: false,
	
	
	initObjects: function(){
		
		this._autoCompleteList = document.getElementById("autoCompleteMenu");
		DreamJob._autoCompleteList.onmouseover = function(){DreamJob._out = "open"; DreamJob._autoCompleteList.style.display = "block";}
		DreamJob._autoCompleteList.onmouseout = function(){DreamJob._out = "close"; setTimeout("DreamJob.hideCompleteList()",2000)}
	
		//init objects	
		this._email = document.getElementById("email");
		this._pass = document.getElementById("pass");
		this._job = document.getElementById("job");
		this._location = document.getElementById("location");
		//this._distance_only = document.getElementById("dist_only");
		//this._distance_within = document.getElementById("dist_within");
		this._distance_within_value = document.getElementById("dist_within_value");
		//this._salary_any = document.getElementById("salary_any");
		this._salary = document.getElementById("salary");
		this._industry = document.getElementById("industry");
		this._company = document.getElementById("company");
		
		this._graduate = document.getElementById("graduate");
		this._perks = document.getElementById("perks");
		
		this._find = document.getElementById("find");
		this._rss = document.getElementById("rss");
		
		//assigning methods to certain objects
		this._email.onclick = function(){DreamJob.removeTxt('E-mail',this); DreamJob._out = "close"; DreamJob.hideCompleteList();};
		this._email.onblur = function(){DreamJob.addTxt('E-mail',this)};	
		this._pass.onfocus = function(){DreamJob._pass.type="password"; DreamJob.removeTxt('Password ',this); DreamJob._out = "close"; DreamJob.hideCompleteList();};
		this._pass.onblur = function(){DreamJob.addTxt('Password ',this)};	
		
		this._job.onfocus = function(){DreamJob.removeTxt('Dream Job Title',this)};
		this._job.onblur = function(){DreamJob.addTxt('Dream Job Title',this)};	
		this._job.onkeyup = function(){DreamJob.showAutoCompleteList()};	
		
		this._location.onfocus = function(){DreamJob.removeTxt('Dream Location',this); DreamJob._out = "close"; DreamJob.hideCompleteList();};
		this._location.onblur = function(){DreamJob.addTxt('Dream Location',this)};
		this._industry.onfocus = function(){DreamJob._out = "close"; DreamJob.hideCompleteList();};
		this._company.onfocus = function(){DreamJob.removeTxt('My Dream Company',this); DreamJob._out = "close"; DreamJob.hideCompleteList();};
		this._company.onblur = function(){DreamJob.addTxt('My Dream Company',this)};
		
		//this._distance_only.onclick = function(){DreamJob.doDistanceOnly(); DreamJob._out = "close"; DreamJob.hideCompleteList();}
		//this._distance_within.onclick = function(){DreamJob.doDistanceValue(); DreamJob._out = "close"; DreamJob.hideCompleteList();}
		
		//this._salary_any.onclick = function(){DreamJob.doSalary(); DreamJob._out = "close"; DreamJob.hideCompleteList();}
		
		this._find.onclick = function(){DreamJob.submitDetails();};
		
	},
	
	
	checkRefresh: function(){
	
		if( document.refreshForm.visited.value == "" )
		{
			document.refreshForm.visited.value = "1";

		}
		else
		{
			window.location.href = window.location.href;
		}
	},

	
	
	removeTxt: function(txt,obj){
	
		if(obj.value == txt){__valid[obj.id] = false; obj.value = ""; obj.style.color = "#33414f"; obj.style.fontWeight = "bold"; obj.style.border = "1px solid #33414f";}
		
	},
	
	addTxt: function(txt,obj){
	
		if(obj.value == "")
		{
			
			if(obj.id!='pass')
			{
				obj.value = txt; obj.style.color = "#A0ADB6"; 
				obj.style.fontWeight = "normal"; 
				obj.style.border = "1px solid #cccccc"; 
				__valid[obj.id] = false; 
			}
			else
			{
				var x = navigator;
				if(x.appName != "Microsoft Internet Explorer")
				{
					DreamJob._pass.type="text";
					DreamJob._pass.value = txt;
					obj.value = txt; obj.style.color = "#A0ADB6"; 
					obj.style.fontWeight = "normal"; 
					obj.style.border = "1px solid #cccccc"; 
					__valid[obj.id] = false; 
				}
				else
				{
					obj.value = txt; obj.style.color = "#A0ADB6"; 
					obj.style.fontWeight = "normal"; 
					obj.style.border = "1px solid #cccccc"; 
					__valid[obj.id] = false; 
				}
			}
		
		}
		else{if(obj.id == "email"){DreamJob.doEmailValidation(obj);} else {obj.style.border = "1px solid green"; __valid[obj.id] = true;}};
		
	},
	
	doEmailValidation: function(obj){
	
		var filter = /^.+@.+\..{2,3}$/;
		if(filter.test(obj.value)){obj.style.border = "1px solid green"; __valid[obj.id] = true;}else{obj.style.border = "1px solid red"; __valid[obj.id] = false;}
	
	},
	
	doDistanceOnly: function(){
	
		if(this._distance_only.checked){this._distance_within.checked = false; this._distance_within_value.disabled = true; __valid['distance'] = "only";}
		else{this._distance_within_value.disabled = false; this._distance_within.checked = true; __valid['distance'] = "value";}
		DreamJob._out = "close";
		this.hideCompleteList();
	},
	
	doDistanceValue: function(){
	
		if(this._distance_within.checked){this._distance_only.checked = false; this._distance_within_value.disabled = false; __valid['distance'] = "value";}
		else{this._distance_within_value.disabled = true; this._distance_only.checked = true; __valid['distance'] = "only";}
		DreamJob._out = "close";
		this.hideCompleteList();
	},
	
	doSalary: function(){
	
		if(this._salary_any.checked){this._salary.disabled = true; __valid["salary"] = "any";}
		else{this._salary.disabled = false; __valid["salary"] = "amount";}
		DreamJob._out = "close";
		this.hideCompleteList();
	},
	
	submitDetails: function(){
	
		DreamJob._out = "close";
		this.hideCompleteList();
		
		if(__valid["email"] && __valid['pass'] && __valid['job'] && __valid['location'] && __valid['distance'] && __valid['salary']){
		
			var email = encodeURIComponent(this._email.value);
			var pass = encodeURIComponent(this._pass.value);
			var job = encodeURIComponent(this._job.value);
			var location = encodeURIComponent(this._location.value);
			var distance;
			var salary;
			
			if(__valid['salary'] == 'any'){salary = 'any';}
			else{salary = this._salary.value;}
			
			if(__valid['distance'] == 'value'){distance = this._distance_within_value.value;}
			else{distance = "only";}
			
			
			var request = "email="+email+"&pass="+pass+"&keywords="+job+"&location="+location+"&distance="+distance+"&salary="+salary+DreamJob.checkOptionalFields();
			
			//alert(request);
			
			var url = 'ajax/store.php';
			var response_id = 'content';
			var loading = "<img src=\"include/submiting.gif\" alt=\"loading\" />";
			var loading_id = "loading";
			
			//document.getElementById("loading").textAlign = "center";
			
			SimpleAjax.postRequest_(url, request, response_id, loading, loading_id);
			
		}else{
			alert("Please complete all fields.");
		}
	
	},
		
	checkOptionalFields: function(){

		var out;
		
		if(this._graduate.checked){out = "&graduate_jobs=1"}else{out = "&graduate_jobs=0";}
		if(this._perks.value != "null"){out += "&perks="+this._perks.value;}
		if(this._industry.value != "My Dream Industry"){out += "&industry="+encodeURIComponent(this._industry.value);}
		if(this._company.value != "My Dream Company"){out += "&comp="+encodeURIComponent(this._company.value);}
		
		return out;
	
	},
	
	showAutoCompleteList: function(){
	
		if(DreamJob._job.value.length > 3 && DreamJob._job.value.indexOf(",")==-1){
			DreamJob._autoCompleteList.style.display = "block";
			DreamJob._autoCompleteKey = DreamJob._job.value;
			SimpleAjax.postRequest("ajax/KeywordsSuggest.php", "keywords="+DreamJob._autoCompleteKey, "ajaxAutoComplete", "<center><img src=\"include/loading.gif\" alt=\"loading...\" style=\"margin-bottom: 5px;\" /></center>", "ajaxAutoComplete");
		}else{
			var key = DreamJob._job.value.substr(DreamJob._job.value.lastIndexOf(",")+1);
			if(key.length > 3){
				DreamJob._autoCompleteList.style.display = "block";
				DreamJob._autoCompleteKey = key;
				SimpleAjax.postRequest("ajax/KeywordsSuggest.php", "keywords="+DreamJob._autoCompleteKey, "ajaxAutoComplete", "<center><img src=\"include/loading.gif\" alt=\"loading...\" style=\"margin-bottom: 5px;\" /></center>", "ajaxAutoComplete");
			}
		}
	
	},
	
	doComplete: function(elem){
	
			if(elem.checked){
			
				if(this._job.value == this._autoCompleteKey && this._job.value.indexOf(this._token)==-1){this._job.value = elem.value+this._token; this._autoCompleteKey = false;}
				else if(this._job.value.indexOf(this._token)>0){this._job.value = this._job.value.replace(this._autoCompleteKey, ""); this._autoCompleteKey = false; this._job.value += elem.value+this._token;}
				else{this._job.value += elem.value+this._token; }
			
			}else{
			
				this._job.value = this._job.value.replace(elem.value+this._token, "");
			
			}
	
	},
	
	
	hideCompleteList: function(){
	
		if(DreamJob._out == "close"){
			DreamJob._autoCompleteList.style.display = "none";
			if(DreamJob._job.value == ""){DreamJob.addTxt('Job Title',this._job);}
		}
	
	},
	
	
	toogle: function(elemId)
	{
		var elem = document.getElementById(elemId);
		
		if(elem.style.display == 'block')
		{
			elem.style.display = 'none';
		}
		else
		{
			elem.style.display = 'block';
		}
	},
	
	sendEmail: function()
	{
		var email = document.getElementById('friend_email');
		var cont = document.getElementById('tell_your_friend');
		
		var filter = /^.+@.+\..{2,3}$/;
		if(filter.test(email.value))
		{
			SimpleAjax.postRequest("ajax/invite.php", "email="+email.value, "tell_your_friend", "sending message...", "tell_your_friend");
		}
		else
		{
			alert('Oops.  We need a valid email address.');
		}
	}


}
