/************************************************************************
 * $Author: elisa $
 * $Header: c:\src\webmyriad/webcms/js_utils.js,v 1.5 2010/06/14 01:35:48 elisa Exp $
 * Purpose: 	javascript utiltities, and form validators
 *
 * $Id: js_utils.js,v 1.5 2010/06/14 01:35:48 elisa Exp $
 * $Log: js_utils.js,v $
 * Revision 1.5  2010/06/14 01:35:48  elisa
 * Upgraded from FCKeditor to ckeditor
 * Committed on the Free edition of March Hare Software CVSNT Server.
 * Upgrade to CVS Suite for more features and support:
 * http://march-hare.com/cvsnt/
 *
 * Revision 1.4  2010/01/12 06:39:45  elisa
 * Changed how display:block is used so it'll work in firefox on the table rows
 * Committed on the Free edition of March Hare Software CVSNT Server.
 * Upgrade to CVS Suite for more features and support:
 * http://march-hare.com/cvsnt/
 *
 * Revision 1.3  2009/12/20 01:24:29  elisa
 * moved the config functions into MasterBean
 * Committed on the Free edition of March Hare Software CVSNT Server.
 * Upgrade to CVS Suite for more features and support:
 * http://march-hare.com/cvsnt/
 * 
 * Revision 1.2  2009/12/10 06:14:18  elisa
 * Added some generic config functions
 * Committed on the Free edition of March Hare Software CVSNT Server.
 * Upgrade to CVS Suite for more features and support:
 * http://march-hare.com/cvsnt/
 * 
 * Revision 1.1.1.1  2009/09/19 09:35:35  elisa
 * Imported sources
 * Committed on the Free edition of March Hare Software CVSNT Server.
 * Upgrade to CVS Suite for more features and support:
 *  http://march-hare.com/cvsnt/
 * 
*************************************************************************/
function right(e) {
	var msg = "Sorry, you don't have permission to right-click.";
	if (navigator.appName == 'Netscape' && e.which == 3) {
		alert(msg);
		return false;
	}
	if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2) {
		alert(msg);
		return false;
	}
	else return true;
}


function trap() 
  {
  /*
  if(document.images)
    {
    for(i=0;i<document.images.length;i++)
      {
      document.images[i].onmousedown = right;
      document.images[i].onmouseup = right;
      }
    }
*/
 }

function trim(str){
   return str.replace(/^\s*|\s*$/g,"");
}

function addslashes(str) {
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}
function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}
/*
function get_ed_text(editor_name){
	var oEditor = FCKeditorAPI.GetInstance(editor_name) ;
	var str=oEditor.EditorDocument.body.innerHTML;
	if (str) {
		str=str.replace(/<P>&nbsp;<\/P>/i,"");
		str=str.replace(/<P><\/P>/i,"");
		return str.replace(/<P><br><\/P>/i,"");
		//return str;
	}
	else return '';
}		
*/
function entered(field){
  if (trim(document.getElementById(field).value)==""){
		if (document.getElementById(field).type !="hidden"){
			document.getElementById(field).focus();
		}
		return false;
   }else{
		return true;
   }
}

function isInteger(field){   
    var reInteger = /^\d+$/;
    return reInteger.test(trim(document.getElementById(field).value));
}

function valid_email(field){   
	var email=trim(document.getElementById(field).value);
	var reEmail =/(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
	if (email!=""){
		return reEmail.test(email);
	}
	return true;
}

function valid_price(field){
  if (!isInteger(field)){
  //alert('2ndhalf'+document.getElementById(field).value.substr(document.getElementById(field).value.length-2));
  //alert('1sthalf'+document.getElementById(field).value.substr(0,document.getElementById(field).value.length-3));
  //alert('midlle'+document.getElementById(field).value.substr((document.getElementById(field).value.length-3),1));
    var reInteger = /^\d+$/;
    if (! reInteger.test(document.getElementById(field).value.substr(document.getElementById(field).value.length-2))){
		//alert('last two digits of '+ field+' are incorrect');
		return false;
	}
   if (! reInteger.test(document.getElementById(field).value.substr(0,document.getElementById(field).value.length-3))){
		//alert('first part of '+ field+' is incorrect');
		return false;
	}
	if (document.getElementById(field).value.substr((document.getElementById(field).value.length-3),1)!='.'){
		//alert('incorrect decimal place in '+field);
		return false;
	}
	//if (isInteger(document.getElementById(field).value.substr(document.getElementById(field).value.length-2))){
	//	alert("okay");
	//}
  }
	return true;
}

function valid_postcode (Field){  
	var postcode=trim(document.getElementById(Field).value);  
	if(isInteger(postcode) && postcode.length == 4){  
		return true;
	}else{
		return false;
	}
}

function showHelp(divName){
 //hide all help divs
 //show the required help div
}

function flipVisibility(name){
	if (document.getElementById(name).style.display=='none'){
		document.getElementById(name).style.display=''; //used to say block but block doesnt work in firefox
	}else{
		document.getElementById(name).style.display='none';
	}
}

function confirmDelete(className,id){
	res=confirm("Are you sure you want to archive this record?");
	if (res){
		xajax_delete(className,id);
	}
	return false;
}

function confirmPermanentDelete(className,id){
	$res=confirm("Are you sure you want to PERMANENTLY delete this entry from the system?");
	if ($res){
		xajax_permanentDelete(className,id);
	}
	return false;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  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];}}
}

function MM_findObj(n, d) { //v3.0
  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); return x;
}

function MM_swapImage() { //v3.0
  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];}
}
