<!--

/**  Function: null openWin(windowURL, windowName, windowFeatures)
*    ---------------------------------------------------------------- 
*    Purpose:           opens a new window and sets its properties
*    Arguments:         windowURL			- str, the url
*						windowName			- str, the window name
*						windowFeatures		- str, the features of the window
*    Returns/Assigns:   none
*/

function openWin( windowURL, windowName, windowFeatures ){
	window.open( windowURL, windowName, windowFeatures );
}


/**  Function: null toggleDisplay(ele)
*    ---------------------------------------------------------------- 
*    Purpose:           toggle the display / hide property of an element
*    Arguments:         ele			- str, the name of the element
*    Returns/Assigns:   none
*/

function toggleDisplay(ele){
	var el = document.getElementById(ele);
	if(el.style.display == 'none'){
		el.style.display = '';
	}else{
		el.style.display = 'none';
	}
}


/**  Function: null clearInput(ele, val)
*    ---------------------------------------------------------------- 
*    Purpose:           clear an input
*    Arguments:         ele			- str, the name of the element to clear
*						val			- str, the default value that should be cleared
*    Returns/Assigns:   none
*/

function clearInput(ele, val){
	if(ele.value == val){
		ele.value='';
	}
}

/**  Function: null unlockKeys(obj, keys)
*    ---------------------------------------------------------------- 
*    Purpose:           unlock keys (for front end user) to avoid entering any other keys to field (plus unlocked keys: 'enter', backspace, 
*                       left and right arrow (to move pointer))
*    Arguments:         event		- event
*						keys		- keys which wont be blocked
*    Returns/Assigns:   none
*/

function unlockKeys(event, keys){                                          
	var excludedKeys = new Array(0, 8, 13, 37, 39);
	
	if (window.event){                      
		keyCode=event.keyCode;
	}else if(event.which){ 
		keyCode=event.which;      
	}
	
	if ((keyCode == 13) || (keyCode == 8) || (keyCode == 0)){   
		return true;
	}
	key = String.fromCharCode(keyCode);
	if (keys.indexOf(key) == -1){
		return false;
	}else{
		return true;
	}
}


/**  Function: null help(helpName)
*    ---------------------------------------------------------------- 
*    Purpose:           open the help window
*    Arguments:         helpName		- str, the help reference to display
*    Returns/Assigns:   opens a window
*/

function help(helpName){
	openWin('helpPop.php?help=' + helpName, 'help', 'width=400, height=400, scrollbars, status');
}


/**  Function: null printResults()
*    ---------------------------------------------------------------- 
*    Purpose:           open a window to print the enquiry results
*    Arguments:         none
*    Returns/Assigns:   opens a window
*/

function printResults(ref){
	openWin('serviceResult.php?print=true&ref=' + ref, 'print', 'width=700, height=550, toolbar, scrollbars, status');
}


/**  Function: null checkLogin()
*    ---------------------------------------------------------------- 
*    Purpose:           validate the login form
*    Arguments:         none
*    Returns/Assigns:   true on success, false and a message alert on failure
*/

function checkLogin(){
	var mesg = '';
	var username = document.login.username.value;
	var password = document.login.password.value;
	var charReg = /[^a-zA-Z0-9]/;

	if(username.length < 7 || username.length > 16){
		mesg += "Your username should be between 7 and 16 characters.\n";
	}

	if(password.length < 7 || password.length > 16){
		mesg += "Your password should be between 7 and 16 characters.\n";
	}

	if(charReg.test(password)){
		mesg += "There are non-alphanumeric characters in your password.\nPlease remove them.";
	}

	if(mesg){
		alert(mesg);
		return false;
	}else{
		return true;
	}
}


/**  Function: null checkEmail(email)
*    ---------------------------------------------------------------- 
*    Purpose:           validate an email address
*    Arguments:         email		- str, the email address to check
*    Returns/Assigns:   true on success, false on failure
*/

function checkEmail(email){
	var emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/; // valid
	if (!emailReg1.test(email) && emailReg2.test(email)) {
		return true; // valid
	}else{
		return false; // "Please enter a valid email address.\n";
	}
}


/**  Function: null userDetails(email)
*    ---------------------------------------------------------------- 
*    Purpose:           validate an email address
*    Arguments:         email		- str, the email address to check
*    Returns/Assigns:   true on success, false and a message on failure
*/

function userDetails(email){
	if(checkEmail(email)){
		return true;
	}else{
		alert("Please enter a valid email address.\n");
		return false;
	}
}


/**  Function: null checkRegister(add)
*    ---------------------------------------------------------------- 
*    Purpose:           validate a registration or a profile amendment
*    Arguments:         add		- str, if this a new registration
*    Returns/Assigns:   true on success, false and a message on failure
*/

function checkRegister(add){

	var mesg = '';
	var charReg = /[^a-zA-Z0-9]/;
	var title = document.register.title;
	var firstname = document.register.firstname.value;
	var lastname = document.register.lastname.value;
	var company = document.register.company.value;
	var address1 = document.register.address1.value;
	var town = document.register.town.value;
	var county = document.register.data_countyid;
	var postcode = document.register.postcode.value;
	var email = document.register.email.value;
	var tel = document.register.tel.value;
	var username = document.register.username.value;
	var password = document.register.password.value;
	var terms = document.register.terms;


		
	if(title[0].selected == true){
		mesg += "Please select a title.\n";
	}

	if(firstname.length == 0){
		mesg += "Please enter your first name.\n";
	}

	if(lastname.length == 0){
		mesg += "Please enter your last name.\n";
	}

	if(company.length == 0){
		mesg += "Please enter your company name.\n";
	}

	if(address1.length == 0){
		mesg += "Please enter your address.\n";
	}

	if(town.length == 0){
		mesg += "Please enter your town.\n";
	}

	if(county[0].selected == true){
		mesg += "Please select your county.\n";
	}

	if(postcode.length == 0){
		mesg += "Please enter your postcode.\n";
	}

	if (!checkEmail(email)) {
		mesg += "Please enter a valid email address.\n";
	}

	if(add){
		if(email != document.register.confirm_email.value){
			mesg += "You have not re-typed your email correctly. Please try again.\n";
		}
	}

	if(tel.length == 0){
		mesg += "Please enter your telephone number.\n";
	}

	if(username.length == 0){
		mesg += "Please enter a username.\n";
		
	}else if(username.length < 7 || username.length > 16){
		mesg += "Your username should be between 7 and 16 characters.\n";
		
	}else if(charReg.test(username)){
		mesg += "There are non-alphanumeric characters in your username.\nPlease remove them.";	
	}

	if(add){
		if(password.length == 0){
			mesg += "Please enter a password.\n";
			
		}else if(password.length < 7 || password.length > 16){
			mesg += "Your password should be between 7 and 16 characters.\n";
			
		}else if(charReg.test(password)){
			mesg += "There are non-alphanumeric characters in your password.\nPlease remove them.";
			
		}else{
			if(password != document.register.confirm_password.value){
				mesg += "You have not re-typed your password correctly. Please try again.\n";
			}
		}
	}

	if(add){
		if(terms.checked != true){
			mesg += "You must agree to the terms of use to continue.\n";
		}
	}

	if(mesg){
		alert(mesg);
		return false;
	}else{
		return true;
	}
}

// remove all special characters 
function filterNum(str) {
      re = /\£|\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;         
      return str.replace(re, "");
}

/**  Function: null checkSearch()
*    ---------------------------------------------------------------- 
*    Purpose:           validate the search form
*    Arguments:         none
*    Returns/Assigns:   true on success, false and a message alert on failure
*/

function checkSearch(){
	var mesg = '';
	var charReg = /[^a-zA-Z0-9]/;
	
	// location
	var easting = document.search.easting.value;
	var northing = document.search.northing.value;
	var latitude1 = document.search.latitude1.value;
	var latitude2 = document.search.latitude2.value;
	var latitude3 = document.search.latitude3.value;
	var longitude1 = document.search.longitude1.value;
	var longitude2 = document.search.longitude2.value;
	var longitude3 = document.search.longitude3.value;
	var landranger = document.search.landranger.value;
	var postcode = document.search.postcode.value;
	var street = document.search.street.value;
	var locations = new Array(easting, northing, latitude1, latitude2, latitude3, longitude1, longitude2, longitude3, landranger, postcode, street);
	var location_error = false;
	var any_location = false;
	var this_location = 0;
	for(i=0; i<locations.length; i++){
		if(locations[i] != ''){
			any_location = true;
		}
	}
	if(!any_location){
		mesg += 'No location selected!\n';
		location_error = true;
	}

	if(easting || northing){
		this_location++;

		if((easting && !northing) || (northing && !easting)){
			mesg += 'Please supply both X and Y OS grid reference coordinates.\n';
			location_error = true;
		}

		if(isNaN(easting) || easting.length != 6){
			mesg += 'Your X OS grid reference coordinate should be a 6 digit number.\n';
			location_error = true;
		}

		if(isNaN(northing) || northing.length != 6){
			mesg += 'Your Y OS grid reference coordinate should be a 6 digit number.\n';
			location_error = true;
		}
	}

	if(latitude1 || latitude2 || latitude3 || longitude1 || longitude2 || longitude3){
		this_location++;
		for(i=2; i<6; i++){
			if(locations[i] == '' || isNaN(locations[i])){
				mesg += 'Please enter full latitude and longitude coordinates using only digits.\n';
				location_error = true;
				break;
			}
		}
	}

	if(landranger){
		this_location++;
		if(charReg.test(landranger) || (landranger.length < 8 || landranger.length > 8)){
			mesg += 'Your landranger reference should be made up of 8 characters/digits and no spaces.\n';
			location_error = true;
		}
	}

	if(postcode){
		this_location++;
		if(postcode.length < 3){
			mesg += 'Your postcode should be made up of at least 3 characters.\n';
			location_error = true;
		}
	}

	if(street){
		this_location++;
	}

	if(this_location > 1){
		mesg = 'Please only enter one type of location information.\n';
		location_error = true;
	}

	// start date
	var day = document.search.date_start_d;
	var month = document.search.date_start_m;
	var year = document.search.date_start_y;

	if((day[0].selected == true) || (month[0].selected == true) || (year[0].selected == true)){
		mesg += 'Please enter a full start date for your work.\n';
	}

	// work type
	var num_wc = document.search.work_category.length;
	var category_error = true;
	var type_error = true;

	for(i=0; i<num_wc; i++){
		if(document.search.work_category[i].checked == true){
			category_error = false;
			var num_wt = eval("document.search.work_type_" + i + ".length");
			for(j=0; j<num_wt; j++){
				if(j != 0){
					if(eval("document.search.work_type_" + i + "[" + j + "].selected") == true){
						type_error = false;
					}
				}
			}
		}
	}

	if(category_error || type_error){
		mesg += 'Please select a category of work and a description within it.\n';
	}

	// work coverage
	var area = document.search.area.value;
	if (area == '' || isNaN(area) || area == 0 || area > 5000){
		mesg += "The distance/area your work will cover must be a number of meters from 1 to 5000.\nDo not include any letters.\n";
	}
	
	// job code / reference	
	var ref = document.search.ref.value;
	var refWithoutSCh = filterNum(ref); 
	ref = ref.replace(/^\s+|\s+$/g, '');
	var myRegxp = /(.){3,100}/;
	
	if(!ref){
		mesg += 'Please enter Your job code / reference (3 - 100 characters).\n';	
	}else{
		if(!refWithoutSCh){
			mesg += 'Your job code / reference is invalid (contains only special characters).\nPlease enter at least 3 alphanumeric characters (max 100).\n';	
		}else{			
			if(myRegxp.test(ref) == false){				
				mesg += 'Your job code / reference is invalid. It is too short.\nPlease enter at least 3 characters (max 100).\n';	
			}else{
				if(ref.length > 100){
					mesg += 'Your job code / reference must not be longer than 100 characters.\nAt the moment it has '+ ref.length +' characters.';
				}
			}			
		}					
	}
	document.search.ref.value = ref; //trimed ref
	
	var onbehalf = document.search.onbehalf.value;
	if(onbehalf == 1 && !ref){
		mesg += 'You must add a third party user reference.';
	}

	if(mesg){
		alert(mesg);
		return false;
	}else{
		return true;
	}
}


/**  Function: null turn(obj, on)
*    ---------------------------------------------------------------- 
*    Purpose:           sets 'required' image depending on validation results
*    Arguments:         obj		- obj, image that is being manipulated
*						on		- bln, whether to turn required 'on' or not
*    Returns/Assigns:   none
*/

function turn(obj, on){
	if(on){
		obj.src="images/required_now.gif";
	}else{
		obj.src="images/required.gif";
	}
}


// -->