/*
Util.js - For general javascript functions that are non implementation specific
*/

// New Strict regex to impove data quanlity .. dedmondson-09/2007
// Updated 11/2008 by Lewis Keen - handles postcodes such as W1G 6LA
var REGEXP_POSTCODE = new RegExp('^(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})$');
var REGEXP_EMAIL = new RegExp('^[\\w-]+(?:\\.[\\w-]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7}$');

// Altered to stricter regex : dedmondson-09/2007
function checkEmail(address) 
{
  if (!address.match(REGEXP_EMAIL)) 
  {
    return false;
  }
  return true;
}

// Altered to stricter regex : dedmondson-09/2007
function checkPostCode(pcode) 
{
  if (!pcode.toUpperCase().match(REGEXP_POSTCODE)) 
  {
    return false;
  }
  return true;
}

// 
// check an email address
//function checkEmail(address) {
//	var filter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
//	if (address==null || address=="" || !filter.test(address)) return false;
//	return true;
//}

// check a postcode
//function checkPostCode(pcode) {
//	var filter=/^[a-zA-Z][a-zA-Z0-9]{1,3}\s?\d[a-zA-Z]{2}$/;
//	if (pcode==null || pcode=="" || !filter.test(pcode)) return false;
//	return true;
//}

// Submit a form on pressing "enter"
function submitenter(myfield,evt){ 
	evt=(evt)?evt:event; 
	charCode=(evt.which)?evt.which:evt.keyCode; 
	if(charCode==13){
		myfield.form.submit();
	} 
} 


// Show the post code lookup section and the post code form input control if GB, otherwise don't
 
function showAndHidePostCodeAndLookup(form,postCodeSectionId,postCodeLabelId,inputField){
	var postCodeSectionElement = document.getElementById(postCodeSectionId); 
	var postCodeLabelElement = document.getElementById(postCodeLabelId);
	
	//First show/hide PostCode Lookup section
	if (form.country.value ==	"GB"){
		postCodeSectionElement.style.display="block"; //show element
	}
	else{
		postCodeSectionElement.style.display="none"; //hide element
	}

	//Next show/hide PostCode 
	if (form.country.value=="GB"){
		postCodeLabelElement.style.display="inline"; //show element
		inputField.style.visibility = 'visible'; //show form text field
		inputField.value = "";
	}
	else {
		postCodeLabelElement.style.display="none"; //hide element
		inputField.style.visibility = "hidden"; //hide form text field
		//If country is not GB, make the postcode field equal to the country name. This is because Maginus doesn't have a field for country name so we use postcode.
		inputField.value = form.country.options[form.country.selectedIndex].text;
	}
}

// Hide the post code lookup section
function hidePostCodeLookup(postCodeSectionId)
{
	var postCodeSectionElement = document.getElementById(postCodeSectionId);
	postCodeSectionElement.style.display="none";			
	
}

// Show the post code form input control if GB, otherwise don't
function showAndHidePostCode(form,postCodeLabelId,inputField){
	var postCodeLabelElement = document.getElementById(postCodeLabelId);
	
	//Show/hide PostCode 
	if (form.country.value=="GB"){
		postCodeLabelElement.style.display="inline"; //show element
		inputField.style.visibility = 'visible'; //show form text field
		inputField.value = "";
	}
	else {
		postCodeLabelElement.style.display="none"; //hide element
		inputField.style.visibility = "hidden"; //hide form text field
		//If country is not GB, make the postcode field equal to the country name. This is because Maginus doesn't have a field for country name so we use postcode.
		inputField.value = form.country.options[form.country.selectedIndex].text;
	}
}


//////////////////////////////////////////////////////////
// Checks whether a string contains a double byte character
// target = the string to be checked
//
// Return true if target contains a double byte char; false otherwise
//////////////////////////////////////////////////////////
function containsDoubleByte (target) {
     var str = new String(target);
     var oneByteMax = 0x007F;

     for (var i=0; i < str.length; i++){
        chr = str.charCodeAt(i);
        if (chr > oneByteMax) {return true;}
     }
     return false;
}

//////////////////////////////////////////////////////////
// This function will count the number of bytes
// represented in a UTF-8 string
//
// arg1 = the UTF-16 string
// arg2 = the maximum number of bytes allowed in your input field
// Return false is this input string is larger then arg2
// Otherwise return true...
//////////////////////////////////////////////////////////
function isValidUTF8length(UTF16String, maxlength) {
    if (utf8StringByteLength(UTF16String) > maxlength) return false;
    else return true;
}

//////////////////////////////////////////////////////////
// This function will count the number of bytes
// represented in a UTF-8 string
//
// arg1 = the UTF-16 string you want a byte count of...
// Return the integer number of bytes represented in a UTF-8 string
//////////////////////////////////////////////////////////
function utf8StringByteLength(UTF16String) {
  if (UTF16String === null) return 0;
  var str = String(UTF16String);
  var oneByteMax = 0x007F;
  var twoByteMax = 0x07FF;
  var byteSize = str.length;

  for (i = 0; i < str.length; i++) {
    chr = str.charCodeAt(i);
    if (chr > oneByteMax) byteSize = byteSize + 1;
    if (chr > twoByteMax) byteSize = byteSize + 1;
  }  
  return byteSize;
}
