// Partner tracking
function track(p) {
	alert(p);
}
// Use the Prototype blind functions and some class setting to create a curtain up and down
// effect got elements.  Assume the element is display:none;
function blind(idtoblind) {

	if($(idtoblind).hasClassName('blinddown')) {

		$(idtoblind).addClassName('blindup');
		$(idtoblind).removeClassName('blinddown');

		Effect.BlindUp(idtoblind, {duration: 1.0});

	} else {

		$(idtoblind).addClassName('blinddown');
		$(idtoblind).removeClassName('blindup');

		Effect.BlindDown(idtoblind, {duration: 1.0});
	}

	return false;

}


// Add a job to a basket
// Requires a JOB_ID
// Uses AJAX, on success it will then update the table
function jobBasket(action, job_id) { 
	
	if(job_id == "") { 
		return false;
	}

	var url = getURLPrefix() + "www.drivingjobs.co.uk/ajax/job.basket.php?job_id="+job_id+"&action="+action;

	new Ajax.Request(
		url, 
		{
			method: 'get',
			onSuccess: function(transport) {
				var notice = $('add_to_basket');
				if (transport.responseText == "2") {
		        	notice.update('<a href="#"><img src="/images/icons/tick.png"> Removed From Basket</a>');
					notice.highlight();
				} else if (transport.responseText == "1") {
		        	notice.update('<a href="#"><img src="/images/icons/tick.png"> Saved to Basket</a>');
					notice.highlight();
				} else {
					notice.update('Error Saving Basket');
					notice.highlight();
				}
			}
		}
	);

	return false;
}

function getURLPrefix() {
	
	var loc = new String(window.parent.document.location);
	
	if (loc.indexOf("https://")!= -1)
		prefix = "https://";
	else
		prefix = "http://";
		
	return prefix;
}



// Get all the towns for a given region
function getTypeForRegionClearTowns(id, select_id, town_select_id) {
	$(town_select_id).options.length = 1;
	AddSelectOption($(town_select_id), "No towns Available", "", 0);
	getTypeForRegion(id, select_id, 'county');
}
function getTypeForRegion(id, select_id, type) { 
	
	if(id == undefined || id == "") { 
		return false;
	}
	
	if(select_id == undefined || select_id == "") { 
		return false;
	}
	
	if(type == undefined || type == "") { 
		return false;
	}

	var url = "/ajax/regions.get.php?region_id="+$(id).value+"&type="+type;
	
	// Reset the options for the select
	$(select_id).options.length = 1;
	
	new Ajax.Request(
		url, 
		{
			method: 'get',
			onSuccess: function(transport) {
				if(transport.responseText.match(/Error/)) {
		        	AddSelectOption($(select_id), "No " + type + " Available", "", 0);
				} else {
					var t = transport.responseText.split("||");
					if(t.length > 0) {
						for(var n=0;n<t.length;n++) {
							var options = t[n].split(",");
							AddSelectOption($(select_id), options[1], options[0],0)
						}
					}  else {
						AddSelectOption($(select_id), "No " + type + " Available", "", 0);
					}
				}
			}
		}
	);

	return false;
}

// Get all the towns for a given region
function getTypeForCounty(id, select_id, type) { 
	
	if(id == undefined || id == "") { 
		return false;
	}
	
	if(select_id == undefined || select_id == "") { 
		return false;
	}
	
	if(type == undefined || type == "") { 
		return false;
	}

	var url = "/ajax/counties.get.php?county_id="+$(id).value+"&type="+type;
	
	// Reset the options for the select
	$(select_id).options.length = 1;
	
	new Ajax.Request(
		url, 
		{
			method: 'get',
			onSuccess: function(transport) {
				if(transport.responseText.match(/Error/)) {
		        	AddSelectOption($(select_id), "No " + type + " Available", "", 0);
				} else {
					var t = transport.responseText.split("||");
					if(t.length > 0) {
						for(var n=0;n<t.length;n++) {
							var options = t[n].split(",");
							AddSelectOption($(select_id), options[1], options[0],0)
						}
					}  else {
						AddSelectOption($(select_id), "No " + type + " Available", "", 0);
					}
				}
			}
		}
	);

	return false;
}

function getTypeForCountyNoSSL(id, select_id, type) { 
	
	if(id == undefined || id == "") { 
		return false;
	}
	
	if(select_id == undefined || select_id == "") { 
		return false;
	}
	
	if(type == undefined || type == "") { 
		return false;
	}

	var url = "/ajax/counties.get.php?county_id="+$(id).value+"&type="+type;
	
	// Reset the options for the select
	$(select_id).options.length = 1;
	
	new Ajax.Request(
		url, 
		{
			method: 'get',
			onSuccess: function(transport) {
				if(transport.responseText.match(/Error/)) {
		        	AddSelectOption($(select_id), "No " + type + " Available", "", 0);
				} else {
					var t = transport.responseText.split("||");
					if(t.length > 0) {
						for(var n=0;n<t.length;n++) {
							var options = t[n].split(",");
							AddSelectOption($(select_id), options[1], options[0],0)
						}
					}  else {
						AddSelectOption($(select_id), "No " + type + " Available", "", 0);
					}
				}
			}
		}
	);

	return false;
}


//Get licenses for driver type
function getLicensesForDriverType(id, select_id) { 

	if(id == undefined || id == "") return false;
	if(select_id == undefined || select_id == "") return false;

	var url = "/ajax/dt-licenses.get.php?driver_type_id="+$(id).value;	
	doAjax(url, select_id);
	
	return false;
}
function getSectorsForDriverType(id, select_id) { 
	
	if(id == undefined || id == "") return false;
	if(select_id == undefined || select_id == "") return false;

	var url = "/ajax/dt-sectors.get.php?driver_type_id="+$(id).value;
	doAjax(url, select_id);
	
	return false;
}

// Helper functions
function doAjax(url, select_id) {
	$(select_id).options.length = 1;
	new Ajax.Request(
		url, 
		{
			method: 'get',
			onSuccess: function(transport) {
				populateSelect(transport, select_id);
			}
		}
	);
}
function populateSelect(transport, select_id) {
	if(transport.responseText.match(/Error/)) {
    	AddSelectOption($(select_id), "No " + type + " Available", "", 0);
	} else {
		var t = transport.responseText.split("||");
		if(t.length > 0) {
			for(var n=0;n<t.length;n++) {
				var options = t[n].split(",");
				AddSelectOption($(select_id), options[1], options[0],0);
			}
		}  else {
			AddSelectOption($(select_id), "No " + type + " Available", "", 0);
		}
	}
}

// Get sectors for driver type



function AddSelectOption(selectObj, text, value, isSelected) 
{
    if (selectObj != null && selectObj.options != null)
    {
        selectObj.options[selectObj.options.length] = new Option(text, value, false, isSelected);
    }
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

// JOTW Photo Upload
var Photo = {
	"photo": null,
	"type": null,
	"contId": 'jotwPhoto',

	init: function(type, id, date) {
		this.type = type;
		this.id = id;
		this.date = date;
	},
	
	upload: function() {
	
		// Check photo selected
		var file = this.getFile();
		if (file.value == '') {
			return;
		}
		// Upload photo
		this.setForm('photo');
		this.formSubmit();
		this.setForm('job');
	},
	
	uploadOK: function(file) {
		this.setPhoto(file);
		this.showPhoto();
	},
	
	uploadFailed: function() {
		alert('There was a problem uploading your photo, please try again');
	},
	
	// Sets photo to current file in variable and also hidden field
	setPhoto: function(file) {
		this.photo = file;
		if (document.getElementById(this.contId + 'Hidden')) {
			document.getElementById(this.contId + 'Hidden').value = file;
		}
	},
	
	// Shows current uploaded photo
	showPhoto: function() {
		switch (this.type) {
			case 'jotw':
				path = '/images/jotw/';
				break;
			case 'fe':
				path = '/images/featured/';
				break;
			default:
				path = '/upload/';
		}
		if (this.postForm) {
			$(this.contId + 'Image').src = path+ this.photo;
			document.getElementById(this.contId).innerHTML = "<input type='button' value='Change Photo' onclick='Photo.remove();' />";
		} else {
			document.getElementById(this.contId).innerHTML = "<img src='" + path + this.photo + "' />&nbsp;<input type='button' value='Remove' onclick='Photo.remove();' />";
		}
	},
	
	// AJAX call to delete photo on server
	remove: function() {
		if (this.type == null || confirm("Are you sure you want to delete this photo?")) {
			this.removeAjax();
		}
	},
	removeAjax: function() {
		url = '/post.photo.remove.php';
		if (this.type != null) {
			parameters = { 				
				"type": this.type,
				"id": this.id,
				"date": this.date
			};
		} else {
			parameters = {
				"file": this.photo
			};
		}
		new Ajax.Request(url, {
			method: 'post',
			parameters: parameters,
			onSuccess: function (transport) {
				if (transport.responseText == '1') {
					Photo.resetPhotoForm();
				}
			},
			onFailure: function (transport) {
				alert('Problem removing photo, please try again');
			}
		});
	},
	// Resets upload form
	resetPhotoForm: function() {
		this.setPhoto(null);
		if (this.type != null) {
			var html = "No photo";
		} else {
			var html = "<input type='file' name='uploadFile' id='uploadFile' accept='png,gif,jpg,jpeg' /><input type='button' value='Upload' onclick='Photo.upload();' />";
			if (this.defaultPhoto) {
				$(this.contId + 'Image').src = this.defaultPhoto;
			}
		}
		document.getElementById(this.contId).innerHTML = html;
	},
	
	// Sets doc form for uploading a photo or for creating job 
	setForm: function(type) {
		var form = document.getElementById('postForm');
		switch(type) {
			case 'photo': 
				form.action = '/post.upload.php';
				form.target = 'uploadFrame';
				form.onSubmit = 'return false';				
				break;
			case 'job':
				form.action = '';
				form.target = '';
				form.onSubmit = '';
				break;
		}
	},
	formSubmit: function() {
		var form = document.getElementById('postForm');
		form.submit();
	},
	
	getFile: function() {
		return document.getElementById('uploadFile'); 
	}
}
