//********************************************************************
//*-------------------------------------------------------------------
//* Licensed Materials - Property of IBM
//*
//* WebSphere Commerce
//*
//* (c) Copyright International Business Machines Corporation. 2003
//*     All rights reserved.
//*
//* US Government Users Restricted Rights - Use, duplication or
//* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//*
//*-------------------------------------------------------------------
//*

//////////////////////////////////////////////////////////
// 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;
}

//////////////////////////////////////////////////////////
// A simple function to validate an email address
// It does not allow double byte characters
// strEmail = the email address string to be validated
//
// Return true if the email address is valid; false otherwise
//////////////////////////////////////////////////////////
function isValidEmail(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;
}



//////////////////////////////////////////////////////////
// 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;
}


// check a UK 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);
	
	var countryBox = document.getElementById('country');
	
	if(countryBox != null)
	{
		//First show/hide PostCode Lookup section
		if (countryBox.value ==	"GB"){
			postCodeSectionElement.style.display="block"; //show element
		}
		else{
			postCodeSectionElement.style.display="none"; //hide element
		}
	
		//Next show/hide PostCode 
		if (countryBox.value =="GB"){
			postCodeLabelElement.style.display="inline"; //show element
			inputField.style.display="inline"; //show element
			inputField.style.visibility = 'visible'; //show form text field
			inputField.value = "";
		}
		else {
			postCodeLabelElement.style.display="none"; //hide element
			inputField.style.display="none"; //show 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 = countryBox.options[countryBox.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');
	var postCodeElement      = document.getElementById('zipCode');
	
	//Show/hide PostCode 
	if (form.country.value=="GB"){
		postCodeLabelElement.style.display ="inline"; //show element
		postCodeElement.style.display      = "inline";
		postCodeLabelElement.style.visibility = 'visible'; //show form text field
		postCodeElement.style.visibility         = 'visible'; //show form text field
		inputField.value = "";
	}
	else
	{
		postCodeLabelElement.style.display ="none"; //show element
		postCodeElement.style.display      = "none";
		postCodeLabelElement.style.visibility = 'hidden'; //show form text field
		postCodeElement.style.visibility         = 'hidden'; //show 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.
		form.zipCode.value = form.country.options[form.country.selectedIndex].text;
	}
}

////////////////////////////////////////////////////////////////////////	
// Function to prepare the mail preferences update form for submission//	
////////////////////////////////////////////////////////////////////////	

var addPreferenceStr = '_null_a_null';
var updatePreferenceStr = '_null_r_null';
var deletePreferenceStr = '_null_d_null';

function createAddMailPreference(frm, preferenceInput) {
	createMailPreference(frm, preferenceInput.name, 'a', preferenceInput.value);
}

function createUpdateMailPreference(frm, preferenceInput) {
	createMailPreference(frm, preferenceInput.name, 'r', preferenceInput.value);
}

function createDeleteMailPreference(frm, preferenceInput) {
	createMailPreference(frm, preferenceInput.name, 'd', preferenceInput.value);
}

function createMailPreference(frm, preferenceName, action, preferenceVal) {
	frm.appendChild( createPreferenceElement(preferenceName, action, preferenceVal) );
}

function createPreferenceElement(preferenceName, action, preferenceVal) {
	var inputElement = document.createElement("input");
	inputElement.type = "HIDDEN";
	inputElement.name = getElementName(preferenceName, action);
	inputElement.value = preferenceVal;
	
	return inputElement;
}

function getElementName(preferenceName, action) {
	var elementName = preferenceName;
	
	if( action == 'a' ) {
		elementName += addPreferenceStr;
	}
	else if( action == 'd' ) {
		elementName += deletePreferenceStr;
	}
	else {
		elementName += updatePreferenceStr;
	}
	
	return elementName;
}

function showInNewWindow(contentPageURL,width,height)
{
	window.open (contentPageURL,'name','width='+width+',height='+height+',resizable=no,scrollbars=yes');
}

function calculateCharactersLeft(textAreaId,messageFieldId,characterLimit)
{
	var textAreaInput = document.getElementById(textAreaId);
	var charactersLeft = document.getElementById(messageFieldId);
	if(textAreaInput != null && charactersLeft != null)
	{
		if(textAreaInput.value.length > characterLimit)
		{
			textAreaInput.value = textAreaInput.value.slice(0,characterLimit);
		}
		charactersLeft.innerHTML = "(" + (characterLimit - textAreaInput.value.length) + " characters remaining)";
	}
}