// Bookmark/Add to favorites - bookmark.js

function bookmark (bkmrk) {
	var BookmarkURL= location.href //"http://www.nacasadvice.org.uk"
	var BookmarkTitle= document.title //"North Ayrshire Citizens Advice Service"
	// If the browser is Internet Explorer
	if (document.all)
	{
        // Add to Favorites (Internet Explorer)
        window.external.AddFavorite(BookmarkURL,BookmarkTitle);
	}
	else
	{
        // Add to Bookmarks (Mozilla Firefox)
        window.sidebar.addPanel(BookmarkTitle, BookmarkURL, '');
	}
}

// Check Contact Form - checkform.js
function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // 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 + "?[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);

  // 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;
}

// defines empty fields
function isEmpty (inputStr) {
	if (inputStr == "" || inputStr == null) {
		return true;
	}
	return false;
}


// START: Check for Alphabet chars. only
function valName(){
	var docId = document.getElementById('frmContact');
	var nameExp = /^[a-zA-Z-'\s]+$/; // \s allows space. Also accepts hyphens(-) and apostrophes(')
	if(docId.fname.value.match(nameExp)){
		return true;
	}if(!docId.fname.value.match(nameExp)){
		return false;
	}
} // END: Check for Alphabet chars. only


// START: Check for a valid Email address
function validEmail(email) {
	//var invalidChars = " /:,;"
	var invalidChars = '\/\'\\ ";:?!#=()[]\{\}^|';
	var re = /\S+\.(aero|biz|com|coop|edu|gov|int|info|mil|museum|name|net|org|pro|ws|us|ca|mx|ag|ar|aw|bb|bm|br|bs|co|cr|jm|ky|pa|pe|pr|tt|ve|vg|vi|uk|ad|at|be|bg|by|ch|cz|de|dk|ee|es|eu|fi|fr|gi|gr|hr|hu|ie|il|is|it|li|lu|lv|mc|mk|mt|nl|no|pl|pt|ru|se|sk|tr|ua|au|cn|fj|hk|id|in|jp|kr|mo|my|nz|ph|pk|sg|th|tw|tv|cc|cx|fm|nu)\b/i;

	for (var i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	atPos = email.indexOf("@",1)
	if (atPos == -1) {
		return false;
	}
	if (email.indexOf("@",atPos+1) > -1) {
		return false;
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {
		return false;
	}
	if (periodPos+3 > email.length)	{
		return false;
	}
	if (!re.test(email)) {
		return false;
	}
	return true;
}
// END: Check for a valid Email address


/* START: FORM VALIDATION */
function chkForm(){
	//var doc = document.frmContact;
	var doc = document.getElementById('frmContact');
	var el = doc.elements;
	var errMsg = document.getElementById('error_box');
	errMsg.innerHTML = "";
	msgList = "";
	
	var fname = document.getElementById('fname');
	var email = document.getElementById('email');
	var postcode = document.getElementById('postcode');
	var subject = document.getElementById('subject');
	var bureau = document.getElementById('bureau');
	var acceptTc = document.getElementById('acceptTc');
	
	if (doc.fname.value == ''){
		msgList += "   - <a href=\"javascript:document.getElementById('frmContact').fname.focus();\" title=\"Click here to change this value\"><strong>" + document.getElementById('label_fname').innerHTML + "</strong></a> is required, and cannot be left empty.<br />";
		el['fname'].parentNode.className = "errorfield";
	} else if (!valName('fname')) {
		msgList += "   - <a href=\"javascript:document.getElementById('frmContact').fname.focus();document.getElementById('frmContact').fname.select();\" title=\"Click here to change this value\"><strong>" + document.getElementById('label_fname').innerHTML + "</strong></a> contains invalid characters.<br />";
		el['fname'].parentNode.className = "errorfield";
	} else{
		el['fname'].className = "required";
		el['fname'].parentNode.className = "normal";
	} 
	
	if (doc.email.value == ''){
		msgList += "   - <a href=\"javascript:document.getElementById('frmContact').email.focus();\" title=\"Click here to change this value\"><strong>" + document.getElementById('label_email').innerHTML + "</strong></a> is required, and cannot be left empty.<br />";
		el['email'].parentNode.className = "errorfield";
	} else if (!validEmail(doc.email.value)) { // for validEmail needs to be (!validEmail(doc.email.value))
		msgList += "   - <a href=\"javascript:document.getElementById('frmContact').email.focus();document.getElementById('frmContact').email.select();\" title=\"Click here to change this value\"><strong>" + document.getElementById('label_email').innerHTML + "</strong></a> is not a valid email address.<br />";
		el['email'].parentNode.className = "errorfield";
	} else{
		el['email'].className = "required";
		el['email'].parentNode.className = "normal";
	}
	
	if (doc.postcode.value == ''){
		msgList += "   - <a href=\"javascript:document.getElementById('frmContact').postcode.focus();\" title=\"Click here to change this value\"><strong>" + document.getElementById('label_postcode').innerHTML + "</strong></a> is required, and cannot be left empty.<br />";
		el['postcode'].parentNode.className = "errorfield";
		//el['postcode'].parentNode.className = "normal";
	} else if (!checkPostCode (doc.postcode.value)) {
		msgList += "   - <a href=\"javascript:document.getElementById('frmContact').postcode.focus();document.getElementById('frmContact').postcode.select();\" title=\"Click here to change this value\"><strong>" + document.getElementById('label_postcode').innerHTML + "</strong></a> is not valid.<br />";
		el['postcode'].parentNode.className = "errorfield";
	} else{
		el['postcode'].parentNode.className = "normal";
	}
	
	if (subject.selectedIndex == 0){
			msgList += "   - <a href=\"javascript:document.getElementById('frmContact').subject.focus();\" title=\"Click here to change this value\"><strong>" + document.getElementById('label_subject').innerHTML + "</strong></a> please choose a subject from the list.<br />";
			el['subject'].parentNode.className = "errorfield";
		}else if (!subject.selectedIndex == 0){
			subject.parentNode.className = "normal";
		}
	
	if (bureau.selectedIndex == 0){
			msgList += "   - <a href=\"javascript:document.getElementById('frmContact').bureau.focus();\" title=\"Click here to change this value\"><strong>" + document.getElementById('label_bureau').innerHTML + "</strong></a> please choose a bureau from the list.<br />";
			el['bureau'].parentNode.className = "errorfield";
		}else if (!bureau.selectedIndex == 0){
			bureau.parentNode.className = "normal";
		}
		
	if (doc.enquiry.value == ''){
		msgList += "   - <a href=\"javascript:document.getElementById('frmContact').enquiry.focus();\" title=\"Click here to change this value\"><strong>" + document.getElementById('label_enquiry').innerHTML + "</strong></a> is required, and cannot be left empty.<br />";
		el['enquiry'].parentNode.className = "errorfield";
	} 
	else{
		el['enquiry'].className = "required";
		el['enquiry'].parentNode.className = "normal";
	}
	
	//TEST CHECKBOX
	
	if (doc.acceptTc.checked == false){
		msgList += "   - <a href=\"javascript:document.getElementById('frmContact').acceptTc.focus();\" title=\"Click here to change this value\"><strong>" + document.getElementById('label_tc').innerHTML + "</strong></a> please tick this box to accept our terms.<br />";
		el['acceptTc'].parentNode.className = "errorfield";
	}else{
		el['acceptTc'].className = "required";
		el['acceptTc'].parentNode.className = "normal";
	}
	
	//END TEST CHECKBOX
	
	if(msgList != '') {
		errMsg.innerHTML += "<p><img src=\"alert.gif\" class=\"alert\" alt=\"There was a problem with your submission\" />&nbsp;<strong>Oops!</strong> There was a problem with your submission:</p>";
		errMsg.innerHTML += msgList;
		errMsg.innerHTML += "<p><strong>\Please make the necessary corrections, and resubmit the form.\</strong></p>";
		document.getElementById('error_box').className = "visible";
		return false;
	} else {
		return true;
	}
}
/* END: FORM VALIDATION */

// external-links.js

function arranmap(){window.open('http://maps.google.co.uk/maps?q=ka27+8dl&hl=en&sll=55.754944,-4.685547&sspn=0.007366,0.022724&vpsrc=0&hnear=Brodick+KA27+8DL,+United+Kingdom&t=m&z=16');}
function irvinemap(){window.open('http://maps.google.co.uk/maps/place?q=irvine+citizens+advice+bureau&hl=en&cid=15201799802591458513');}
function kilbirniemap(){window.open('http://maps.google.co.uk/maps?q=43+Main+Street+Kilbirnie&hl=en&sll=55.783755,-4.604815&sspn=0.471062,1.454315&vpsrc=0&hnear=43+Main+St,+Kilbirnie+KA25+7BX,+United+Kingdom&t=m&z=16');}
function largsmap(){window.open('http://maps.google.co.uk/maps/place?hl=en&cid=14711297346981563202');}
function saltcoatsmap(){window.open('http://maps.google.co.uk/maps/place?q=irvine+citizens+advice+bureau&hl=en&cid=9271200512648370420');}

// Email to a friend - mailpage.js
function mailpage()
{
mail_str = "mailto:?subject=Check out " + document.title;
mail_str += "&body=I thought you might be interested in " + document.title;
mail_str += ". You can view it at, " + location.href;
location.href = mail_str;
}

// Text Resizer - text-resizer.js

function fsize(size,unit,id){
  var vfontsize = document.getElementById(id);
  if(vfontsize){
   vfontsize.style.fontSize = size + unit;
  }
}

var textsize = 10;
function changetextsize(up){
  if(up){
   textsize = parseFloat(textsize)+2;
  }else{
   textsize =parseFloat(textsize)-2;
  }
}

// Translate website - translate.js

function langCn(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Czh-CN&hl=en&ie=UTF8'); return false;}
function langFr(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Cfr&hl=en&ie=UTF8'); return false;}
function langDe(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Cde&hl=en&ie=UTF8'); return false;}
function langHi(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Chi&hl=en&ie=UTF8'); return false;}
function langPl(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Cpl&hl=en&ie=UTF8'); return false;}
function langGd(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Cgd&hl=en&ie=UTF8'); return false;}
function langEs(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Ces&hl=en&ie=UTF8'); return false;}
function langIt(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Cit&hl=en&ie=UTF8'); return false;}
function langRu(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Cru&hl=en&ie=UTF8'); return false;}
function langJa(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Cja&hl=en&ie=UTF8'); return false;}
function langUr(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Cur&hl=en&ie=UTF8'); return false;}
function langGa(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Cga&hl=en&ie=UTF8'); return false;}
function langCy(){window.open('http://www.google.com/translate?u='+encodeURIComponent(location.href)+'&langpair=en%7Ccy&hl=en&ie=UTF8'); return false;}
