/**
 * Common JavaScript Fucntions
 * Derived from Core J2EE best practices
 * 
 * 
 * @package 	CSI Application Version 1.0
 * @author: 	Richard Flathmann (richardf@commspecial.com)
 * @author: 	Derek Miranda (derekm@commspecial.com)
 * @copyright: 	2007 Communications Specialties, Inc.
 * 
 * 
 */
 
/**
 *
 * This function will remove text from a
 * passed form element only if the current
 * content does not equal the default
 * content.
 *
 */
 
function ClearTextBox( TextBox, ifEquals ) 
{
	if( ifEquals )
	{
		if( TextBox.value == ifEquals )
		{
			TextBox.value = '';
		}
		
		return;
	}
	
	TextBox.value = '';
}

/**
 *
 * This function restores a text box's
 * value back to retoreTo if the user
 * didn't enter anything
 *
 */
function restoreTextBox( TextBox, restoreTo )
{
	if( TextBox.value == '' )
	{
		TextBox.value = restoreTo;
	}
}

/**
 *
 * Used to verify an email address
 *
 */
function validateEmailSubscribe( box )
{
	with (box)
	{	
		var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    	var regex = new RegExp(emailReg);
     	
     	if( regex.test(value) == false)
     	{
     		alert('Valid Email Address Required');
     		return false;
     	}
     	else
     	{
     		return true;
     	}
	}
}

/**
 *
 * Validates the search box in the header
 *
 */
function validateSearchBox( box )
{
	if( box.value == 'Search' || box.value == '' )
	{
		alert('Please enter a search term.');
		return false;
	}
	else
	{
		return true;
	}
}

/**
 * Message to confirm before deleting records
 */
function confirmDelete()
{
	msg = "Are you sure that you want to delete this record?";
	return confirm(msg);
}

/**
 * Message to confirm before an action
 *
 * @param msg string, custom message
 */
function confirmDialog( msg )
{
	return confirm(msg);
}

/**
 * Used by the PHP class fileUploadProgress
 * to open up the window displaying the file
 * upload progress meter.
 * 
 * @param path The Path to the display Contentss
 */
function openUploadWindow( path )
{
	var uploadWindow = window.open( path,'Upload_Meter','width=370,height=115');
}

/**
 * Function to open a browser window
 */
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/**
 * Function to call common Java Functions
 */
function MM_callJS(jsStr) { 
  return eval(jsStr)
}

/**
 * Function to redirect to a URL
 */
function MM_goToURL() {
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

/**
 * Function to find an object in the DOM
 */
function MM_findObj(n, d) { //v4.01
  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);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/**
 * Shows/Hides a specefied layer (div)
 */
function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

/**
 * Evaluates and executes a Java Statement
 */
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

/**
 * Java based form validation
 */
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }

