
// Open a popup in the middle of screen
function nc_popup(url, pWidth, pHeight) 
{  
 var leftPos = (screen.width - pWidth)/2, topPos = (screen.availHeight - pHeight)/2; 

 window.open(url,'popupWindow', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + pWidth + ', height=' + pHeight + ', top=' + topPos + ', left=' + leftPos);
}


// Limit the number of characters in a field (textarea, text: onkeypress)
function nc_limitfield(object, limit) 
{  
 if (object) 
 {  
  var length = object.value.length; 
  if ( length >= limit) 
  { 
   return false; 
  } 
 } 
 return true; 
}

// Only authorize number key
function nc_isnumberfield(field)
{
 if (isNaN(field.value))
  { 
   var newnumber = "";
   number = field.value; 
   	  
   for (i=0; i<number.length; i++) if (!isNaN(number.charAt(i))) newnumber += number.charAt(i); 
   field.value = newnumber;   
  }  
}

// Give the default value
function nc_formatfield(field)
{ 
 field.value = field.value.replace(/ /g, ""); // remove all spaces
 
 if (!$.trim(field.value)) 
 {
  if (field.defaultValue) field.value = field.defaultValue;
  else field.value=0;
 }
 
}

// Search a key in array and return index
function nc_array_search(pkey, ptbl)
{	
 var indexoftab = 0;
 
 for(elt in ptbl){  
			 	    if (ptbl[elt]==pkey) {return indexoftab;}
					indexoftab++;
				 }
 indexoftab =-1;
 return indexoftab;
}

// Verify an email adresse
function nc_filter(mystring, type)
{
 switch (type) 
 { 
  case 'email': 
     var place = mystring.indexOf('@',1);
     var point = mystring.indexOf('.',place+1);
     if (!((place > -1) && (mystring.length >2) && (point > 1))) return false;
     else return true;
  break; 

  case 'url': 
     if (mystring.search(/^([http]+[/:/]+[\///])+(.+)?[/\./]+[a-z]{2,4}$/) == -1) return false;
     else return true;
  break; 

  default: 
   return true;
  break; 
 }
}


// Init a field on blur
function initfield(field)
{
 if (field.value == '') 
 {
  field.style.fontStyle = 'italic';
  field.value = field.defaultValue;
 }
}

// Clear a field on focus
function clearfield(field)
{
 if (field.defaultValue == field.value) 
 {
  field.value = '';
  field.style.fontStyle = 'normal';
 } 
}

// Ajax routines
function makeRequest(purl, pid, ptype, pdata)
{
 data = pdata || ''; 
 ptype = ptype || 'GET';

 $.ajax
 (
  {   
   url: purl, 
   type: ptype,
   data: pdata,
   success: function(answer) 
   {   
	$('#'+pid+'_loading').hide();   
    $('#'+pid).html(answer);
   },
   beforeSend: function(jqXHR, settings)
   {
	$('#'+pid+'_loading').show();
   }   
  } 
 );   
}
