function createRequestObject(){
	var request_o;
	var browser=navigator.appName;
	if(browser=="Microsoft Internet Explorer"){
		request_o=new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_o=new XMLHttpRequest();
	}
	return request_o;
}

function expand(itemID){
	if(document.getElementById(itemID) && document.getElementById(itemID+'sub')){
		var curtext=document.getElementById(itemID).innerHTML;
		if(document.getElementById(itemID+'sub').style.display==''){
			document.getElementById(itemID+'sub').style.display='none';
			document.getElementById(itemID).className='rp';
		}else{
			document.getElementById(itemID+'sub').style.display='';
			document.getElementById(itemID).className='rm';
		}
	}
}

function pdfjob(jobID){
	/*alert(jobID);*/
	var pdfwin=window.open('/job/pdf.php?id='+jobID,'PDF','height=600;width=600');

}

function printjob(){
	var listinginfo=document.getElementById('listing').innerHTML;
	var printwin=window.open('printview.php','Print','height=500,width=700');
	var tmp = printwin.document;
	tmp.write('<!DOCTYPE HTML PUBLIC "-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/html4\/loose.dtd"><head><title>Job Listing - Jobs4Careers.com.au<\/title>');
	tmp.write('<style type="text\/css">body{font:9pt Arial;}td{font:9pt Arial;}<\/style>');
	tmp.write('<\/head><body><table style="width:100%;"><tr><td style="height:76px;overflow-x:hidden;"><img alt="Jobs4Careers.com.au" src="\/images\/j4c.jpg" \/><\/td><\/tr><tr><td><p>This Job Listing Can Be Found at: '+location.href+'</p>'+listinginfo+'<br \/><br \/>');
	tmp.write('<p>This Job Listing was sourced from www.jobs4careers.com.au.  Visit us today for all your career needs.<\/p>');
	tmp.write('<p>Copyright: www.jobs4careers.com.au<\/p>');
	tmp.write('<\/td><\/tr><\/table><\/body><\/html>');
	tmp.close();
	if(window.focus){printwin.focus()}
	printwin.print();
}

var job={
	curID:0,

	add:function(){
		var errorcount=0;
		var errortext='';

		var warningcount=0;
		var warningtext='';

		if(document.getElementById('reference').value==''){
			errorcount++;
			errortext=errortext+'- You must enter a job reference;'+"\n";
		}
		if(document.getElementById('professionID').options[document.getElementById('professionID').selectedIndex].value==''){
			errorcount++;
			errortext=errortext+'- You must select a profession;'+"\n";
		}
		if(document.getElementById('jobtype').options[document.getElementById('jobtype').selectedIndex].value==''){
			errorcount++;
			errortext=errortext+'- You must select a job type;'+"\n";
		}
		if(document.getElementById('jobtitle').value==''){
			errorcount++;
			errortext=errortext+'- You must enter a job title;'+"\n";
		}

		if(document.getElementById('abstract').value==''){
			warningcount++;
			warningtext=warningtext+'- There is no abstract detailing a summary of the job;'+"\n";
		}
		if(document.getElementById('description').value==''){
			warningcount++;
			warningtext=warningtext+'- There is no job description;'+"\n";
		}
		if(document.getElementById('special').value==''){
			warningcount++;
			warningtext=warningtext+'- There are no special requirements specified for this job;'+"\n";
		}
		if(document.getElementById('salaryID').options[document.getElementById('salaryID').selectedIndex].value==''){
			warningcount++;
			warningtext=warningtext+'- No salary range has been selected for this job;'+"\n";
		}
		if(document.getElementById('workType').options[document.getElementById('workType').selectedIndex].value==''){
			warningcount++;
			warningtext=warningtext+'- The work type for this job has not been selected;'+"\n";
		}

		if(errorcount>0){
			errortext='Missing information:'+"\n"+errortext;
			alert(errortext);
		}else{
			if(warningcount>0){
				warningtext='The following items were ommitted from your job listing:'+"\n"+warningtext+'These items '+
					'are not compulsory, but are recommended.  Would you like to continue adding this job listing, or '+
					'click Cancel to return to editing this job';
				var cont=confirm(warningtext);
			}else{
				var cont=true;
			}

			if(cont){
				var address=document.getElementById('number').value+' '+document.getElementById('street').value+' '+document.getElementById('streettype').value;
		    	var suburb=document.getElementById('suburb').value;
		    	var state=document.getElementById('state').value;
		    	var pcode=document.getElementById('postcode').value;
		    	address=address+', '+suburb+', '+state+','+pcode+', Australia';

		    	var http = createRequestObject();
				http.open('get','/myaccount/jobs.php?action=getLatLang&address='+escape(address));
				http.onreadystatechange=function(){
					if(http.readyState==4){
						var latlong = http.responseText;
						latlong = latlong.split(',');
						document.getElementById('lat').value=latlong[2];
						document.getElementById('long').value=latlong[3];
						document.getElementById('jobadd').submit();
					}
				}
				http.send(null);

				/*var geocoder=new google.maps.ClientGeocoder();
				geocoder.getLatLng(
					address,
					function(point){
						if(!point){
							alert(point);
							document.getElementById('jobadd').submit();
						}else{
							eval("var latlong=new Array"+point);
							document.getElementById('lat').value=latlong[0];
							document.getElementById('long').value=latlong[1];
							alert(point);
							document.getElementById('jobadd').submit();
						}
					}
				);*/
			}
		}
	},
	addatt:function(itemID,itemname){
		/*alert('addatt');*/

		var list=document.getElementById('list');
		var fileid=document.createElement('div');
		var inpid=document.createElement('input');

		fileid.setAttribute('id','upfile'+itemID);
		fileid.innerHTML='<table><tr><td><img alt="" src="/images/icons/pages/page_white.png" /></td><td>'+itemname+'</td></tr></table>';
		inpid.type='hidden';
		inpid.name='filename[]';
		inpid.value=itemID;
		list.appendChild(fileid);
		fileid.appendChild(inpid);
	},
	apply:function(jobID){
		window.location.hash='a';
		var name=document.getElementById('name').value;
		var email=document.getElementById('email').value;
		var phno=document.getElementById('phno').value;
		var message=document.getElementById('message').value;
		message=message.replace(/\n/gi,'<br />');
		message=message.replace(/\r/gi,'<br />');
		message=message.replace(/\r\n/gi,'<br />');

		if(name=='' || email=='' || phno==''){
			alert('Unable to submit your job application.  We require you to specify at least your Name, Email and Phone Number to submit a job application');
		}else{
			var attachmentID=[];
			var inputs=document.getElementsByTagName('input');
			for(var i=0;i<inputs.length;i++){
				if(inputs[i].name=='filename[]'){
					attachmentID[attachmentID.length]=inputs[i].value;
				}
			}

			var resume=document.getElementById('resume').checked;
			var cc=document.getElementById('cc').checked;

			var http=createRequestObject();
			http.open('get','apply.php?action=add&jobID='+jobID+'&name='+name+'&email='+email+'&phno='+phno+'&message='+message+'&resume='+resume+'&cc='+cc+'&attachmentArray='+attachmentID);
			http.onreadystatechange=function(){
				http.readyState==1?alert('We Are Now Submitting Your Job Application'):'';
				if(http.readyState==4){
					if(http.responseText!=''){
						var applyID=http.responseText;
						document.getElementById('applicationform').innerHTML='<h3>Application Received:</h3>'+
''+
'<p>We have forwarded your enquiry to the job lister with the information '+
'that was provided in the Job Application Form.  We wish you the best of '+
'luck in your job application!</p>'+
''+
'<p>We ask you to take some time to answer these quick questions regarding '+
'your experience using Jobs4Careers:</p>'+
'<form action="/job/feedback.php?applyID='+applyID+'" method="post">'+
'<p><b>1. How easy was it to search for jobs on Jobs4Careers?</b></p>'+
'<ul style="list-style:none;">'+
'	<li><input name="rg1" type="radio" value="2" /> Very Easy</li>'+
'	<li><input name="rg1" type="radio" value="1" /> Somewhat Easy</li>'+
'	<li><input name="rg1" type="radio" value="0" /> Difficult</li>'+
'</ul>'+
''+
'<p><b>2. How easy was it to view a job on Jobs4Careers?</b></p>'+
'<ul style="list-style:none;">'+
'	<li><input name="rg2" type="radio" value="2" /> Very Easy</li>'+
'	<li><input name="rg2" type="radio" value="1" /> Somewhat Easy</li>'+
'	<li><input name="rg2" type="radio" value="0" /> Difficult</li>'+
'</ul>'+
''+
'<p><b>3. How easy was it to apply for a job on Jobs4Careers?</b></p>'+
'<ul style="list-style:none;">'+
'	<li><input name="rg3" type="radio" value="2" /> Very Easy</li>'+
'	<li><input name="rg3" type="radio" value="1" /> Somewhat Easy</li>'+
'	<li><input name="rg3" type="radio" value="0" /> Difficult</li>'+
'</ul>'+
''+
'<p><input type="submit" name="submit" value="Submit My Feedback" /></p></form>';
					}else{

					}
				}
			}
			http.send(null);
		}
	},
	clearhistory:function(){
		var http=createRequestObject();
		http.open('get','/job/?action=clearhistory');
		http.onreadystatechange=function(){
			http.readyState==4?document.getElementById('recentlyviewed').innerHTML='<table><tr><td><img alt="Error" src="/images/icons/error.png" /></td><td class="error">Job History cleared</td></tr></table>':'';
		}
		http.send(null);
	},
	delappn:function(tableID,appnID){
		var ok=confirm('Do you wish to delete this job application?');
		if(ok){
			var http=createRequestObject();
			http.open('get','/myaccount/appn.php?action=del&appnID='+appnID);
			http.onreadystatechange=function(){
				if(http.readyState==4){
					job.removeRow(tableID,appnID);
					alert('Application Deleted');
				}
			};
			http.send(null);
		}
	},
	deleteWatch:function(watchID){
		var ok=confirm('Do you wish to delete this job watch?');
		if(ok){
			var http=createRequestObject();
			http.open('get','/myaccount/watch.php?action=del&watchID='+watchID);
			http.onreadystatechange=function(){
				http.readyState==1?'':'';
				if(http.readyState==4){
					job.removeRow('jobwatchtbl',watchID);
					alert('Job Watch Deleted');
				}
			}
			http.send(null);
		}
	},
	deljob:function(tableID,jobID){
		var ok=confirm('Do you wish to delete this job?');
		if(ok){
			var http=createRequestObject();
			http.open('get','jobs.php?action=del&jobID='+jobID);
			http.onreadystatechange=function(){
				http.readyState==1?'':'';
				if(http.readyState==4){
					job.removeRow(tableID,jobID);
					alert('Job Deleted');
				}
			}
			http.send(null);
		}
	},
	appn:{
		toggle:function(obj){
			var node=obj;
			while(node.tagName!='TABLE')
				node=node.parentNode;
			var nodes=node.getElementsByTagName('input');
			for (var i=0;i<nodes.length;i++){
					if(nodes[i].type=='checkbox')
					nodes[i].checked=(obj.checked?'checked':'');
				}
		},
		del:function(node){
			while(node.tagName!=='TBODY'){
				node=node.parentNode;
			}
			if(confirm('Are you sure you want to delete the selected applications?')){
				var tr=node.getElementsByTagName('tr');
				var appIDs=new Array();
				var params='';
				for (var i=1;i<tr.length-1;i++){
					if(tr[i].getElementsByTagName('input')[0].checked){
						appIDs.push(tr[i].id);
						params+=tr[i].getElementsByTagName('input')[0].name+'=1&';
					}
				}
				if(appIDs.length>0){
					//remove row
					for(var i=0;i<appIDs.length;i++){
						node.removeChild(document.getElementById(appIDs[i]));
					}
					var http=createRequestObject();
					http.open("POST", '/myaccount/jobs/applications.php?ajax=true', true);
					http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					http.setRequestHeader("Content-length", params.length);
					http.setRequestHeader("Connection", "close");
					http.send(params);
					//change count
					var appns=node.getElementsByTagName('tr').length-2;
					document.getElementById('appn_no').innerHTML=appns;
					if(appns==0){
						node.parentNode.parentNode.removeChild(node.parentNode);
					}
				}
			}
		}
	}
	,
	cart:{
		exclude:function(obj){
			var node=obj;
			while(node.tagName!=='TR'){
				node=node.parentNode;
			}
			var cb=node.getElementsByTagName('input');

			if(cb[0].checked){
				node.className='';
			}else{
				node.className='disable';
			}
			job.cart.update();
		},
		remove:function(obj){
			var node=obj;
			while(node.tagName!=='TR'){
				node=node.parentNode;
			}
			if(confirm('Are you sure you want to remove this item from the cart?')){
				node.parentNode.removeChild(node);
				job.cart.update();
			}
		},
		update:function(){
			var nodes=document.getElementById("pay").getElementsByTagName('tbody')[0].getElementsByTagName('tr');
			var total=0;
			for(var i=0;i<nodes.length-1;i++){
				if(nodes[i].id.toString().match(/^job:/)&&nodes[i].className==''){
					total+=50;
				}
				document.getElementById('total').innerHTML=total;
			}
		},
		refresh:function(){
			var c=document.getElementById('bal_current').innerHTML;
			var t=document.getElementById('total').innerHTML;
			var a=parseInt(document.getElementById('bal_addition').value);
			if(isNaN(a))a=0;
			document.getElementById('bal_total').innerHTML=parseInt(c)-parseInt(t)+parseInt(a);
		},
		validate:function(){
			var t=parseInt(document.getElementById('bal_total').innerHTML);
			if(t<0){
				alert('You are required to purchase additional credit before you can purchase the listings.');
				return false;
			}
			return true;
		},
		checkout:function(){
			var nodes=document.getElementById("pay").getElementsByTagName('tbody')[0].getElementsByTagName('tr');
			var items=new Array();
			for(var i=0;i<nodes.length-1;i++){
				if(nodes[i].id.toString().match(/^job:/)&&nodes[i].className==''){
					items.push(nodes[i].id.toString().split(':')[1]);
				}

			}
			if(items.length>0)
				getData('jobs','payco:'+items);
			else
				alert('There are no items in your cart to pay for.');
		},
		exit:function(){
			location.href='/myaccount/';
		},
		check:function(box){
			var obj=box;
			while(obj.tagName!="FORM")
				obj=obj.parentNode;
			var inputs=obj.getElementsByTagName('input');
			for(var i=0;i<inputs.length;i++){
				if(inputs[i].type.toLowerCase()=="checkbox"&&inputs[i]!=box){
					inputs[i].checked=box.checked;
					job.cart.exclude(inputs[i]);
				}
			}
			job.cart.update();
		}
	},
	download:function(){
		var http=createRequestObject();
		http.open('get','/myaccount/download.php?type=jobtmp');
		http.onreadystatechange=function(){
			if(http.readyState==1){
				document.getElementById('pagecontent').innerHTML='Loading ...';
			}
			if(http.readyState==4){
				document.getElementById('pagecontent').innerHTML=http.responseText;
			}
		};
		http.send(null);
	},
	init:function(){
		if(location.hash!=''){
			var act=location.hash;
			act=act.replace('#','');

			if(act=='jobdownload'){
				job.download();
			}
		}
	},
	pay:function(){
		if(this.curID!=0){
			window.location='/pay.php?jobID='+this.curID;
		}else{
			alert('Unable to process payment');
		}
	},
	removeRow:function(tableID,rowID){
		var otbl=document.getElementById(tableID);
		for(var i=0;i<otbl.rows.length;i++) {
			if(otbl.rows[i].getAttribute('id')==rowID){
				otbl.deleteRow(i);
			}
		}
	},
	save:function(jobID,userID){
		var http=createRequestObject();
		http.open('get','/job/view.php?action=save&jobID='+jobID+'&userID='+userID);
		http.onreadystatechange=function(){
			if(http.readyState==4)
				alert('Job Saved:\n\nPlease see your account to check on saved jobs');
		}
		http.send(null);
	},
	saveSearch:function(searchID){
		var http=createRequestObject();
		http.open('get','/job/index.php?action=savesearch&searchID='+searchID);
		http.onreadystatechange=function(){
			http.readyState==1?alert('Saving Your Search'):'';
			http.readyState==4?alert('Job Search Saved:\n\nPlease see your account to check on saved searches'):'';
		}
		http.send(null);
	},
	setID:function(itemID){
		this.curID=itemID;
		document.getElementById('paynow').style.display='block';

	},
	showcontactcard:function(jobID){
		if(document.getElementById('contactinfo').style.display=='list-item'){
			document.getElementById('contactinfo').style.display='none';
			document.getElementById('contactinfoBG').style.display='none';
		}else{
			curX=0;
			curY=0;
			var theElement=document.getElementById('contactdetails');
			while(theElement!=null){
				curX+=theElement.offsetLeft;
				curY+=theElement.offsetTop;
				theElement=theElement.offsetParent;
			}
			curY+=18;

			document.getElementById('contactinfo').innerHTML='<table height="200" width="200"><tr valign="middle"><td align="center"><img alt="Loading" src="/images/loading/greysnake.gif" /><br /><b>Loading ...</b><br /></td></tr></table>';
			document.getElementById('contactinfo').style.background='rgb(255,255,255)';
			document.getElementById('contactinfo').style.left=curX+"px";
			document.getElementById('contactinfo').style.top=curY+"px";
			document.getElementById('contactinfo').style.display='list-item';

			newX=curX+5;
			newY=curY+5;
			document.getElementById('contactinfoBG').innerHTML='<table height="200" width="200"><tr valign="middle"><td align="center"><img alt="Loading" src="/images/loading/greysnake.gif" /><br /><b>Loading ...</b><br /></td></tr></table>';
			document.getElementById('contactinfoBG').style.background='rgb(0,0,0)';
			document.getElementById('contactinfoBG').style.left=newX+"px";
			document.getElementById('contactinfoBG').style.top=newY+"px";
			document.getElementById('contactinfoBG').style.display='list-item';

			var http=createRequestObject();
			http.open('get','/job/apply.php?action=jobcontact&jobID='+jobID);
			http.onreadystatechange=function(){
				if(http.readyState==4){
					document.getElementById('contactinfo').innerHTML='<table><tr><td style="padding:15px;">'+http.responseText+'</td></tr></table>';
					document.getElementById('contactinfoBG').innerHTML='<table><tr><td style="padding:15px;">'+http.responseText+'</td></tr></table>';
				}
			}
			http.send(null);
		}
	},
	update:function(jobID){
		var reference=document.getElementById('reference').value;
		var professionID=document.getElementById('professionID').options[document.getElementById('professionID').selectedIndex].value;

		var jobtitle=document.getElementById('jobtitle').value;
		var abstract=document.getElementById('abstract').value;
		var description=document.getElementById('description').value;
		var special=document.getElementById('special').value;

		var number=document.getElementById('number').value;
		var street=document.getElementById('street').value;
		var streettype=document.getElementById('streettype').value;
		var suburb=document.getElementById('suburb').value;
		var state=document.getElementById('state').value;
		var postcode=document.getElementById('postcode').value;
		var country=document.getElementById('country').value;
		var appnClose=document.getElementById('appnClose').value;
		var adexpires=document.getElementById('adexpires').value;

		/*alert('started'); */

		var http=createRequestObject();
		http.open('get','jobs.php?action=update&jobID='+jobID+'&reference='+reference+'&professionID='+professionID+'&jobType=&jobTitle='+jobtitle+'&abstract='+abstract+'&description='+description+'&special='+special+'&salaryID=&workType=&number='+number+'&street='+street+'&streettype='+streettype+'&suburb='+suburb+'&state='+state+'&postcode='+postcode+'&country='+country+'&appnClose='+appnClose+'&adexpires='+adexpires);
		http.onreadystatechange=function(){
			http.readyState==1?alert('Sending information for update'):'';
			http.readyState==4?alert(http.responseText):'';
		}
		http.send(null);
	}
}

if(navigator.appVersion.indexOf("MSIE")!=-1){
	window.attachEvent("onload",job.init);
}else{
	window.addEventListener("load",job.init,false);
}
