function isFilled( elm ) { 
	if (  elm != null ) {
		if ( elm.val() == "" || elm.val() == null ) {
			return false									;
		}
		else {
			return true									;
		}
	}
}

function confirmDelete(garbage,url)
{
	if(confirm("Are you sure you wish to delete " + garbage + "?"))
	{
		self.location = url;
	}	
}


function confirmRestore(garbage,url)
{
	if(confirm("Are you sure you wish to restore " + garbage + "?"))
	{
		self.location = url;
	}	
}

function RequiredInput(Input,ErrorMessage)
{
	try{
		var value=Input.val();
	}catch(e){
		var value=Input.value;
	}
	if(value == '')
	{
		try{
			jAlert(ErrorMessage, 'Form alert');
			Input.focus();
		}catch(e){
			alert(ErrorMessage)
		}		
		return false;
	}
	return true;
}

function RequiredInputEditor(Input,ErrorMessage)
{
	var fck = FCKeditorAPI.GetInstance(Input); 
	var yourcontent = fck.GetHTML(); 	
	if(yourcontent==''){
		jAlert(ErrorMessage, 'Form alert');		
		fck.Focus();
	return false;
	}
	return true;
}

function RequiredSelect(Select,ErrorMessage)
{
	try{
		var value=Select.val();
	}catch(e){
		var value=Select.value;
	}	
	if(value == 0)
	{
		try{
			jAlert(ErrorMessage, 'Form alert');
			Select.focus();
		}catch(e){
			alert(ErrorMessage)
		}		
		return false;
	}
	return true;
}

function RequiredCheckboxes(Input,Message)
{
	if(Input.length)
	{
		var i;
		for( i = 0 ; i < Input.length ; i++ )
		{
			Input[i].style.background = '';
		}
		for( i = 0 ; i < Input.length ; i++ )
		{
			if(Input[i].checked)
			{
				return true;
			}
		}
		for( i = 0 ; i < Input.length ; i++ )
		{
			Input[i].style.background = 'red';
		}
	}
	else
	{
		if(Input.checked)
			{
				return true;
			}
	}
	alert(Message);
	return false;
}

function RequiredRadio(Input,Message) {
	var radioCheck = false;
	for (i = 0; i < Input.length; i++) {
		if (Input[i].checked)
			radioCheck = true; 
	}
	if (radioCheck) {
		return true;
	}
	alert(Message);
	return false;
}

function ValidateField(Input,Pattern,Message)
{
	var myRegExp = eval('/'+Pattern+'/');	
	try{
		var value=Input.val();
	}catch(e){
		var value=Input.value;
	}		
	if ( value.search(myRegExp) == -1 )
	{		
		try{
			jAlert(Message, 'Form alert');
			Input.focus();
		}catch(e){
			alert(Message)
		}	
		return false;
	}
	return true;
}

function ValidateEmail(Input,Message)
{
	return ValidateField(Input,"^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$",Message);
}

function ValidateDate(Input)
{
	var regex = /^([0-9]{1,2})[\/.]([0-9]{1,2})[\/.]([0-9]{2}|[0-9]{4})$/;
	var matches = Input.val().match(regex);
	
	if( matches != null )
	{
		var Day, Month, Year;
		day = matches[1];
		month = matches[2]-1;
		year = matches[3];
		
		var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		var daysInMonth = new Array(12);
		daysInMonth[0] = 31; // January
		daysInMonth[1] = (year % 4 == 0) ? 29 : 28; // February
		daysInMonth[2] = 31; // March
		daysInMonth[3] = 30; // April
		daysInMonth[4] = 31; // May
		daysInMonth[5] = 30; // June
		daysInMonth[6] = 31; // July
		daysInMonth[7] = 31; // August
		daysInMonth[8] = 30; // September
		daysInMonth[9] = 31; // October
		daysInMonth[10] = 30; // November
		daysInMonth[11] = 31; // December

		if ( month < 0 || month > 11 )
		{
			alert( "Please enter a valid date\n\nThe month must be between 01 and 12" );
			return false;
		}
		if ( day > daysInMonth[month] )
		{
			alert( "Please enter a valid date.\n\nThe number of days in " + months[month] + " cannot be more than " + daysInMonth[month] );
			return false;
		}
		var date = new Date(matches[3],matches[2]-1,matches[1]);
	}
	else
	{
		alert( "Please enter a valid date format (dd/mm/yyyy)" );
		return false;
	}
	return true;
}

function ReformatDate(Input)
{
	var regex = /^([0-9]{1,2})[\/.]([0-9]{1,2})[\/.]([0-9]{2}|[0-9]{4})$/;
	var matches = Input.val().match(regex);
	if ( matches[1].length == 1 ) { matches[1] = "0"+matches[1]; }
	if ( matches[2].length == 1 ) { matches[2] = "0"+matches[2]; }
	if ( matches[3].length == 2 ) { 
		if ( matches[3] > 67 )
		{ matches[3] = "19"+matches[3]; }
		else
		{ matches[3] = "20"+matches[3]; }
	}
	Input.value = matches[1] + "/" + matches[2] + "/" + matches[3];
}

function ValidateTime(Input)
{
	var regex = /^([0-9]{1,2})[:. ]([0-9]{1,2})$/;
	var matches = Input.val().match(regex);
	if ( matches != null )
	{
		if ( matches[1] < 0 || matches[1] > 24 )
		{
			alert( "Please enter a valid 24hr time\n\nThe hour must be between 00 and 24" );
			return false;
		}
		if ( matches[2] < 0 || matches[2] > 59 )
		{
			alert( "Please enter a valid 24hr time\n\nThe minute must be between 00 and 59" );
			return false;
		}
	}
	else
	{
		alert( "Please enter a valid 24hr time format (hh:mm)" );
		return false;

	}
	return true;
}

function ReformatTime(Input)
{
	var regex = /^([0-9]{1,2})[:. ]([0-9]{1,2})$/;
	var matches = Input.val().match(regex);
	if ( matches[1].length == 1 ) { matches[1] = "0"+matches[1]; }
	if ( matches[2].length == 1 ) { matches[2] = "0"+matches[2]; }
	
	Input.value = matches[1] + ":" + matches[2];
}

function Reserved(){
	var theForm = document.PageForm;
	var value=String(theForm.FriendlyURL.value);			
	var warning='Sorry this is a reserved word. Please select another one for your friendlyURL';
	//RESERVED WORD LIST
	var words=new Array('admin','stylesheet','login','images','upload','secure','unsecure','safari','shop','checkout','virtual','affiliatehome','print','preview','browser','ajax','assets');
	for(i=0;i<=words.length;i++){
		if (value.toLowerCase()==words[i]){alert(warning);theForm.FriendlyURL.value='';}
	}
}

function RequiredSelectNEQ(Select,ErrorMessage)
{
	if(Select.value == '')
	{
		alert(ErrorMessage);
		Select.focus();
		return false;
	}
	return true;
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


function textAreasInit(){
  var objs = document.getElementsByTagName("textarea");
  var oi = 0; //oi is object index
  var thisObj;

  for (oi=0;oi<objs.length;oi++) {
   thisObj = objs[oi];
   // note that maxlength is case sensitve
   if (thisObj.getAttribute('maxlength')){
    thisObj.onkeyup = forceMaxLength;
   }
   //thisObj.onchange = saveEntryValue;
  }
 }

 function forceMaxLength(){
  var maxlength = parseInt(this.getAttribute('maxlength'));
  if(this.value.length > maxlength){
   this.value = this.value.substring(0,maxlength);
  }
 }
 
  function addEvent(elm, evType, fn, useCapture)
 // addEvent and removeEvent
 // cross-browser event handling for IE5+,  NS6 and Mozilla
 // By Scott Andrew
 {
   if (elm.addEventListener){
  elm.addEventListener(evType, fn, useCapture);
  return true;
   } else if (elm.attachEvent){
  var r = elm.attachEvent("on"+evType, fn);
  return r;
   } else {
  alert("Handler could not be removed");
   }
 }
 
 
 function isValidPostcode(p) {
	var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
	return postcodeRegEx.test(p);
}

function hasOptions(obj){if(obj!=null && obj.options!=null){return true;}return false;}

function selectUnselectMatchingOptions(obj,regex,which,only){if(window.RegExp){if(which == "select"){var selected1=true;var selected2=false;}else if(which == "unselect"){var selected1=false;var selected2=true;}else{return;}var re = new RegExp(regex);if(!hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){if(re.test(obj.options[i].text)){obj.options[i].selected = selected1;}else{if(only == true){obj.options[i].selected = selected2;}}}}}

function selectMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"select",false);}

function selectOnlyMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"select",true);}

function unSelectMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"unselect",false);}

function sortSelect(obj){var o = new Array();if(!hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;}if(o.length==0){return;}o = o.sort(

function(a,b){if((a.text+"") <(b.text+"")){return -1;}if((a.text+"") >(b.text+"")){return 1;}return 0;});for(var i=0;i<o.length;i++){obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);}}

function selectAllOptions(obj){if(!hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){obj.options[i].selected = true;}}

function moveSelectedOptions(from,to){if(arguments.length>3){var regex = arguments[3];if(regex != ""){unSelectMatchingOptions(from,regex);}}if(!hasOptions(from)){return;}for(var i=0;i<from.options.length;i++){var o = from.options[i];if(o.selected){if(!hasOptions(to)){var index = 0;}else{var index=to.options.length;}to.options[index] = new Option( o.text, o.value, false, false);}}for(var i=(from.options.length-1);i>=0;i--){var o = from.options[i];if(o.selected){from.options[i] = null;}}if((arguments.length<3) ||(arguments[2]==true)){sortSelect(from);sortSelect(to);}from.selectedIndex = -1;to.selectedIndex = -1;}

function copySelectedOptions(from,to){var options = new Object();if(hasOptions(to)){for(var i=0;i<to.options.length;i++){options[to.options[i].value] = to.options[i].text;}}if(!hasOptions(from)){return;}for(var i=0;i<from.options.length;i++){var o = from.options[i];if(o.selected){if(options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text){if(!hasOptions(to)){var index = 0;}else{var index=to.options.length;}to.options[index] = new Option( o.text, o.value, false, false);}}}if((arguments.length<3) ||(arguments[2]==true)){sortSelect(to);}from.selectedIndex = -1;to.selectedIndex = -1;}

function moveAllOptions(from,to){selectAllOptions(from);if(arguments.length==2){moveSelectedOptions(from,to);}else if(arguments.length==3){moveSelectedOptions(from,to,arguments[2]);}else if(arguments.length==4){moveSelectedOptions(from,to,arguments[2],arguments[3]);}}

function copyAllOptions(from,to){selectAllOptions(from);if(arguments.length==2){copySelectedOptions(from,to);}else if(arguments.length==3){copySelectedOptions(from,to,arguments[2]);}}

function swapOptions(obj,i,j){var o = obj.options;var i_selected = o[i].selected;var j_selected = o[j].selected;var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);o[i] = temp2;o[j] = temp;o[i].selected = j_selected;o[j].selected = i_selected;}

function moveOptionUp(obj){if(!hasOptions(obj)){return;}for(i=0;i<obj.options.length;i++){if(obj.options[i].selected){if(i != 0 && !obj.options[i-1].selected){swapOptions(obj,i,i-1);obj.options[i-1].selected = true;}}}}

function moveOptionDown(obj){if(!hasOptions(obj)){return;}for(i=obj.options.length-1;i>=0;i--){if(obj.options[i].selected){if(i !=(obj.options.length-1) && ! obj.options[i+1].selected){swapOptions(obj,i,i+1);obj.options[i+1].selected = true;}}}}

function removeSelectedOptions(from){if(!hasOptions(from)){return;}for(var i=(from.options.length-1);i>=0;i--){var o=from.options[i];if(o.selected){from.options[i] = null;}}from.selectedIndex = -1;}

function removeAllOptions(from){if(!hasOptions(from)){return;}for(var i=(from.options.length-1);i>=0;i--){from.options[i] = null;}from.selectedIndex = -1;}

function addOption(obj,text,value,selected){if(obj!=null && obj.options!=null){obj.options[obj.options.length] = new Option(text, value, false, selected);}}



function OT_transferLeft() { moveSelectedOptions(this.right,this.left,this.autoSort,this.staticOptionRegex); this.update(); }
function OT_transferRight() { moveSelectedOptions(this.left,this.right,this.autoSort,this.staticOptionRegex); this.update(); }
function OT_transferAllLeft() { moveAllOptions(this.right,this.left,this.autoSort,this.staticOptionRegex); this.update(); }
function OT_transferAllRight() { moveAllOptions(this.left,this.right,this.autoSort,this.staticOptionRegex); this.update(); }
function OT_saveRemovedLeftOptions(f) { this.removedLeftField = f; }
function OT_saveRemovedRightOptions(f) { this.removedRightField = f; }
function OT_saveAddedLeftOptions(f) { this.addedLeftField = f; }
function OT_saveAddedRightOptions(f) { this.addedRightField = f; }
function OT_saveNewLeftOptions(f) { this.newLeftField = f; }
function OT_saveNewRightOptions(f) { this.newRightField = f; }
function OT_update() {
	var removedLeft = new Object();
	var removedRight = new Object();
	var addedLeft = new Object();
	var addedRight = new Object();
	var newLeft = new Object();
	var newRight = new Object();
	for (var i=0;i<this.left.options.length;i++) {
		var o=this.left.options[i];
		newLeft[o.value]=1;
		if (typeof(this.originalLeftValues[o.value])=="undefined") {
			addedLeft[o.value]=1;
			removedRight[o.value]=1;
			}
		}
	for (var i=0;i<this.right.options.length;i++) {
		var o=this.right.options[i];
		newRight[o.value]=1;
		if (typeof(this.originalRightValues[o.value])=="undefined") {
			addedRight[o.value]=1;
			removedLeft[o.value]=1;
			}
		}
	if (this.removedLeftField!=null) { this.removedLeftField.value = OT_join(removedLeft,this.delimiter); }
	if (this.removedRightField!=null) { this.removedRightField.value = OT_join(removedRight,this.delimiter); }
	if (this.addedLeftField!=null) { this.addedLeftField.value = OT_join(addedLeft,this.delimiter); }
	if (this.addedRightField!=null) { this.addedRightField.value = OT_join(addedRight,this.delimiter); }
	if (this.newLeftField!=null) { this.newLeftField.value = OT_join(newLeft,this.delimiter); }
	if (this.newRightField!=null) { this.newRightField.value = OT_join(newRight,this.delimiter); }
	}
function OT_join(o,delimiter) {
	var val; var str="";
	for(val in o){
		if (str.length>0) { str=str+delimiter; }
		str=str+val;
		}
	return str;
	}
function OT_setDelimiter(val) { this.delimiter=val; }
function OT_setAutoSort(val) { this.autoSort=val; }
function OT_setStaticOptionRegex(val) { this.staticOptionRegex=val; }
function OT_init(theform) {
	this.form = theform;
	if(!theform[this.left]){alert("OptionTransfer init(): Left select list does not exist in form!");return false;}
	if(!theform[this.right]){alert("OptionTransfer init(): Right select list does not exist in form!");return false;}
	this.left=theform[this.left];
	this.right=theform[this.right];
	for(var i=0;i<this.left.options.length;i++) {
		this.originalLeftValues[this.left.options[i].value]=1;
		}
	for(var i=0;i<this.right.options.length;i++) {
		this.originalRightValues[this.right.options[i].value]=1;
		}
	if(this.removedLeftField!=null) { this.removedLeftField=theform[this.removedLeftField]; }
	if(this.removedRightField!=null) { this.removedRightField=theform[this.removedRightField]; }
	if(this.addedLeftField!=null) { this.addedLeftField=theform[this.addedLeftField]; }
	if(this.addedRightField!=null) { this.addedRightField=theform[this.addedRightField]; }
	if(this.newLeftField!=null) { this.newLeftField=theform[this.newLeftField]; }
	if(this.newRightField!=null) { this.newRightField=theform[this.newRightField]; }
	this.update();
	}

function OptionTransfer(l,r) {
	this.form = null;
	this.left=l;
	this.right=r;
	this.autoSort=true;
	this.delimiter=",";
	this.staticOptionRegex = "";
	this.originalLeftValues = new Object();
	this.originalRightValues = new Object();
	this.removedLeftField = null;
	this.removedRightField = null;
	this.addedLeftField = null;
	this.addedRightField = null;
	this.newLeftField = null;
	this.newRightField = null;
	this.transferLeft=OT_transferLeft;
	this.transferRight=OT_transferRight;
	this.transferAllLeft=OT_transferAllLeft;
	this.transferAllRight=OT_transferAllRight;
	this.saveRemovedLeftOptions=OT_saveRemovedLeftOptions;
	this.saveRemovedRightOptions=OT_saveRemovedRightOptions;
	this.saveAddedLeftOptions=OT_saveAddedLeftOptions;
	this.saveAddedRightOptions=OT_saveAddedRightOptions;
	this.saveNewLeftOptions=OT_saveNewLeftOptions;
	this.saveNewRightOptions=OT_saveNewRightOptions;
	this.setDelimiter=OT_setDelimiter;
	this.setAutoSort=OT_setAutoSort;
	this.setStaticOptionRegex=OT_setStaticOptionRegex;
	this.init=OT_init;
	this.update=OT_update;
	}

