/*HANDS OFF*/

/*PRELOAD IMAGES */
//v3.0
function MM_swapImgRestore() { 
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
//v3.0
function MM_preloadImages() { 
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//v4.01
function MM_findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
//v3.0
function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

MM_preloadImages();

/*BOOKMARK*/

function bookmarksite(title,url){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

<!--
// show item on hover - for text or pictures - used in MAP HIGHLIGHTS
function ShowItem(id,Source) {
	if (Source=="1"){
		if (document.layers) document.layers[''+id+''].visibility = "show"
		else if (document.all) document.all[''+id+''].style.visibility = "visible"
		else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
	}
	else
	if (Source=="0"){
		if (document.layers) document.layers[''+id+''].visibility = "hide"
		else if (document.all) document.all[''+id+''].style.visibility = "hidden"
		else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
	}
}
//-->


// POSTCODE CHECK
function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkpmnrstuvwxy]";                         // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "{1}" + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}

// PHONE VALIDATION

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s) {   
	var i;
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function trim(s) {   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++) {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}

function stripCharsInBag(s, bag) {   
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

/*CALL BACK*/
function submitEntry(form) {
	if(form.firstname.value == '') {
		alert('Please fill in your first name.');
		form.firstname.focus();
		return false;
	}	
	if(form.lastname.value == '') {
		alert('Please fill in your last name.');
		form.lastname.focus();
		return false;
	}	
	if(form.contactemail.value == '') {
		alert('Please fill in your email.');
		form.contactemail.focus();
		return false;
	}	
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.contactemail.value))) {
		alert('Email address is invalid.\nPlease check and change.');
		form.contactemail.value="";
		form.contactemail.focus();
		return false;
	}
	if ((form.contactnumber.value==null)||(form.contactnumber.value=="")){
		alert("Please fill in a contact number.");
		form.contactnumber.focus()
		return false
	}
	if (checkInternationalPhone(form.contactnumber.value)==false){
		alert("Please give a valid number.");
		form.contactnumber.value="";
		form.contactnumber.focus();
		return false;
	}
	if(form.facebookname.value == '') {
		alert('Please provide your Facebook Username - we will be checking!');
		form.facebookname.focus();
		return false;
	}	
	if(form.address.value == '') {
		alert('Please fill in your address:\nhouse name or number and street name');
		form.address.focus();
		return false;
	}	
	if(form.postcode.value == '') {
		alert('Please fill in your postcode.');
		form.postcode.focus();
		return false;
	} 
	var myPostCode = form.postcode.value;
  	if (checkPostCode (myPostCode)) {
		form.postcode.value = checkPostCode (myPostCode)
		//alert ("Postcode good and accepted.");
	}
	else {
		alert ("Postcode invalid.\nPlease check and change.");
		form.postcode.value="";
		form.postcode.focus();
		return false;
  	}
	if(form.dob.value == '') {
		alert('Please fill in your Date of Birth in the format dd/mm/yyyy.');
		form.dob.focus();
		return false;
	}
	var chosentheme = false;
	for (i = 0;  i < form.chosentheme.length;  i++) {
		if (form.chosentheme[i].checked)
		chosentheme = true;
	}
	if (!chosentheme) {
		alert("Please select the theme you chose");
		return false;
	}	
	if(form.artworkname.value == '') {
		alert('Please tell us the name of your piece.');
		form.artworkname.focus();
		return false;
	}	
	if(form.aboutartwork.value == '') {
		alert('Please tell us about your piece - a description.');
		form.aboutartwork.focus();
		return false;
	}	
	if (form.giveconsent.checked == false){
		alert("Do you agree to our terms?\nPlease give your consent by ticking the box.");
		return false;
	}	
    return true;
}

function clearGroupNum() {
	document.myform.victimTypeNumber.value="";
}
function clearGroupOther() {
	document.myform.victimTypeOther.value="";
}

function sendReportSimple(form) {
	// STATUS
	if (form.yourStatus[5].checked && form.yourStatusOther.value=='') {
		alert("Please specify \'other\' to describe your status in this incident.");
		form.yourStatusOther.focus();
		return false;
	}
	// TYPE
	if (form.incidentType[6].checked && form.incidentTypeOther.value=='') {
		alert("Please specify \'other\' category to describe the motivation or type of incident.");
		form.incidentTypeOther.focus();
		return false;
	}
	// PAST OR NOT
	var radioPastIncident = false;
	for (i=0; i<form.pastIncident.length; i++) {
		if (form.pastIncident[i].checked)
		radioPastIncident = true;
	}
	// IF PAST MUST SAY NUMBER
	if (form.firstTimer[2].checked && !radioPastIncident) {
		alert("Please specify the number of previous incidents. Select \'unknown\' if you are unsure.");
		return false;
	}
	// LOCATION
	// checkbox
	for (i=0; i<form.incidentLocation.length; i++) {
		if (form.incidentLocation[25].checked && form.incidentLocationOther.value=='') {
			alert("Please specify \'other\' category to describe the location type of the incidents.");
			form.incidentLocationOther.focus();
			return false;
		}
	}
	// WHAT HAPPENED
	for (i=0; i<form.incidentHappening.length; i++) {
		if (form.incidentHappening[15].checked && form.incidentHappeningService.value=='') {
			alert("Please specify \'other\' category to describe the location or service where access was refused.");
			form.incidentHappeningService.focus();
			return false;
		}
	}
	// IF TYPE IS GROUP GIVE NUMBER - ONFOCUS CLEAR SCRIPT SEE FORM
	if (form.victimType[3].checked && form.victimTypeNumber.value=='') {
		alert("Please specify the number of persons in the group or state \'unknown\' if you are unsure.");
		form.victimTypeNumber.focus();
		return false;
	}
	// IF OTHER TYPE STATE
	if (form.victimType[4].checked && form.victimTypeOther.value=='') {
		alert("Please specify \'other\' to describe the target/type of victim(s) of the incident.");
		form.victimTypeOther.focus();
		return false;
	}
   	return true;
}

function sendReportSimple2(form) {
	// LOCATION Q5 AND Q6
	var checkincidentLocation = false;
	for (i=0; i<form.incidentLocation.length; i++) {
		if (form.incidentLocation[i].checked)
		checkincidentLocation = true;
	}
	
	if (!checkincidentLocation) {
		alert("Question 5 - is required.\nPlease select an option or options to best describe the location of the incident(s)\nFor your information to be of use, we do need you to answer location related questions.");
		return false;
	}
	
	if (form.incidentLocation[25].checked && form.incidentLocationOther.value=='') {
		alert("Question 5 - for this you selected \'other\'.\nPlease accompany this with a name, place or 3-4 words to briefly describe this.");
		form.incidentLocationOther.focus();
		return false;
	}
	
	if(form.incidentGeoAddress.value == '' && form.incidentGeoPostcode.value == '' && form.incidentGeoDescription.value == '') {
		alert('Question 6 - requires at least one option.\nBoth address and postcode seems to be unknown.\nPlease then describe the incident area in as much detail as you can using landmarks, unique features etc.');
		form.incidentGeoDescription.focus();
		return false;
	}
	if(form.recaptcha_response_field.value == '') {
		alert('Please type the two words in the \'reCAPTCHA\' verification box beside the buttons');
		form.recaptcha_response_field.focus();
		return false;
	}	
   	return true;
}

/*CONTACT*/
function sendEnquiry(form) {
	if(form.boxEnquiry.value == '') {
		alert('Enquiry/comment is required.\nPlease fill in the enquiry/comments box.');
		form.boxEnquiry.focus();
		return false;
	}
	if((form.boxEmail.value != '') && !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.boxEmail.value))) {
		alert('Email address is invalid.\nPlease check and change.');
		form.boxEmail.value="";
		form.boxEmail.focus();
		return false;
	}
	if(form.recaptcha_response_field.value == '') {
		alert('Please type the two words in the \'reCAPTCHA\' verification box beside the buttons');
		form.recaptcha_response_field.focus();
		return false;
	}
    return true;
}
