var resultPanel = '';

function openResultPanel( opt )
{
	 var pWidth = (opt.width != null && opt.width!='' )?opt.width:300;
	 var pTitle = (opt.title != null && opt.title!='' )?opt.title:'Loading, Please wait...';
	 var pMsg  = (opt.msg != null && opt.msg!='' )?opt.msg:'<img src="cms/jquery/loading.gif">';
	 if( $('#resultPanel').length == 0)
	 {
	 	  $(document.body).append('<div title="'+pTitle+'" id="resultPanel">'+pMsg+'</div>');
	 	  $('#resultPanel').dialog({
	 	  	modal:true,
	 	  	width:pWidth,
	 	  	draggable:false,
	 	  	minHeight:10,
	 	  	resizable:false
	 	  });
	 	   var dialogParent = $('#resultPanel').parent();
	 	  $('.ui-dialog-titlebar-close', dialogParent).css('display', 'none');
	 }
	 else
	 {
	 	  $('#resultPanel').html(pMsg);
	 		$('#ui-dialog-title-resultPanel').html(pTitle);
	 		$('#resultPanel').dialog('open');
	 		
	 }
	
}


var numberOnly = function( ev ) {
	try
	{
		var charcode = ( ev.charCode != null)?ev.charCode:ev.keyCode;
		
	  var character = String.fromCharCode(charcode);
	 
	  if ((charcode==null) || (charcode==0) || (charcode==8) || 
	      (charcode==9) || (charcode==13) || (charcode==27) )
	    return true;
	  if (("1234567890").indexOf(character) > -1)
	    return true;
	}
	catch(E)
	{
			return true;
	}
  return false;
}

var decimalOnly = function( ev ) {
	
	try
	{
		var charcode = ( ev.charCode != null)?ev.charCode:ev.keyCode;
		
	  var character = String.fromCharCode(charcode);
	 
	  if ((charcode==null) || (charcode==0) || (charcode==8) || 
      (charcode==9) || (charcode==13) || (charcode==27) )
    	return true;
  	if (("1234567890.").indexOf(character) > -1)
    	return true;
	}
	catch(E)
	{
		return true;
	}
  return false;
	
}

var alphaNumericOnly = function( ev ) {
	try
	{
		var charcode = ( ev.charCode != null)?ev.charCode:ev.keyCode;
		
	  var character = String.fromCharCode(charcode);

	  if ((charcode==null) || (charcode==0) || (charcode==8) || 
	      (charcode==9) || (charcode==13) || (charcode==27) )
	    return true;
	  if (("0123456789.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_").indexOf(character) > -1)
	    return true;
	}
	catch(E)
	{
		return true;
	}
  return false;
}

var customCharSet = function( ev, characterSet ) {
	try
	{
		var charcode = ( ev.charCode != null)?ev.charCode:ev.keyCode;
		
	  var character = String.fromCharCode(charcode);
	 
	  
	 	// control keys
	  if ((charcode==null) || (charcode==0) || (charcode==8) || 
	      (charcode==9) || (charcode==13) || (charcode==27) )
	    return true;
	  else if (characterSet.indexOf(character) > -1)
	    return true;
	}
	catch(E)
	{
		return true;
	}
  return false;
}

var isEmail = function(str) {

		var at="@"
		var dot="."

		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}

var dump = function(ob, print_method ){
			var str = '';
			for( item in ob ) {
				str += item + ':' + ob[item] + '\n';
			}
			
			if( print_method == 'write') {
				var newwin = window.open('about:blank');
				newwin.document.write(str);
			}
			else {
				alert(str);
			}
}

var in_array = function( val, arr ){
	if( !arr instanceof Array ) {
		return false;
	}
	
	for( var i=0; i<arr.length; i++ ){
		if( val == arr[i] ) {
			return true;
		}
	}
	
	return false;
}

var dateDiff = function(unit, from, to) {
	var one_unit = 1;
	if( unit == 'day' ) {
		one_unit=1000*60*60*24;
	}
	//Calculate difference btw the two dates, and convert to days
	return Math.ceil((to.getTime()-from.getTime())/(one_unit));
}

var clone = function (obj) {

        if(obj == null || typeof(obj) != 'object') return obj;



        if(obj.constructor == Array) {

                var temp = [];

                for(var i = 0; i < obj.length; i++) {

                        if(typeof(obj[i]) == 'object')   temp.push(clone(obj[i]));

                        else temp.push(obj[i]);

                }

                return temp;

        }



        var temp = {};

        for(var key in obj) temp[key] = clone(obj[key]);

        return temp;

}

function isDate(dateStr) {
	
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
		var matchArray = dateStr.match(datePat); // is the format ok?
		
		
		if (matchArray == null) {
			return false;
		}
	
		month =parseFloat(matchArray[3]); // p@rse date into variables
		day = parseFloat(matchArray[1]);
		year = parseFloat(matchArray[5]);
		
		if (month < 1 || month > 12) { // check month range
			return false;
		}
	
		if (day < 1 || day > 31) {
			return false;
		}
	
		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
			return false;
		}
		
		if (month == 2) { // check for february 29th
		   var isleap = (year % 4 == 0);
		   if (day>29||(day==29&&!isleap)) {
			return false;
		  }
	    }
	return true; // date is valid
}

function isnumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         { 
         blnResult = false;
         }
	  
      }
   return blnResult;
   }

function formatcurrency(field)
{	
   if(field.value!='')
   {
      if(!isnumeric(field.value))
      {    alert("please enter this field with numeric value in xxxx.xx format.");
           field.value ='';       
      }
      else if(parseFloat(field.value)==0)
      {
          alert("The value of this field cannot be zero.");
          field.value ='';
      }
      else
      {		
          var amt = parseFloat(field.value);
          var temp = amt.toFixed(2);
          if( temp.indexOf('.') == -1 ) {
          	temp += '.00';
          }
          field.value = temp;
      }
      
   }
}