/*****************************************************************************
Copyright (C) 2008  Ušur HAMZADAYI
*/


function UMessageBox(VALUE , TYPE){
   if( TYPE == "YESNO" ) {
     return confirm(VALUE);
   }
   return alert(VALUE);
}

function Edit_ValidateInteger(thi)
{
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }
   
   if( event.keyCode == 45 && thi.value.indexOf('-') < 0  ) {
      thi.value = '-' + thi.value;
	  event.returnValue = false;
   }
   else 
   if( event.keyCode == 43 && thi.value.indexOf('-') >= 0  ) {	   
	  thi.value = thi.value.replace(/-/,"");
	  event.returnValue = false;
   }
   if ((event.keyCode < 48) || (event.keyCode > 57))
      event.returnValue = false;
}

function Edit_ValidateUInteger(thi)
{ 
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }

   if ((event.keyCode < 48) || (event.keyCode > 57))
      event.returnValue = false;
}

function Edit_ValidatePlakaAlpha(thi)
{ 
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }

   if(event.keyCode < 65 || event.keyCode > 90 )
   {
	  if (event.keyCode < 97 || event.keyCode > 122 ) 
         event.returnValue = false;
   }
   
}



function Edit_ValidateTelephoneFormat(thi , iBlur )
{
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }
	
   if( iBlur == 0 )	{ //Blur
      thi.value = thi.value.replace(/ /g,"");
      if( thi.value.length > 3 ) {
        thi.value = thi.value.substring(0 ,3 ) + ' ' + thi.value.substring(3);
      }
      if( thi.value.length > 6 ) {
        thi.value = thi.value.substring(0 ,6 ) + ' ' + thi.value.substring(6);
      }
   }
   else { //Focus
	  thi.value = thi.value.replace(/ /g,""); 
   }
}


function Edit_ValidateTelephone(thi)
{
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }

   if (event.keyCode != 32 && (event.keyCode < 48) || (event.keyCode > 57) )
      event.returnValue = false;
}


function Textarea_ValidateLimit( thi , len ) {
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }
	
   if( thi.value.length >= len ) {
	  if( thi.value.length > len ) {
		 thi.value = thi.value.substring(0 ,len );
	  }
      event.returnValue = false;
   }
}


function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}



function Edit_ValidateFinishFormat_MoneyFocus(thi)
{
   thi.value = thi.value.replace(/\$|\,/g,'');
   thi.select();
}

function Edit_ValidateFinishFormat_Money(thi)
{
   thi.value = formatCurrency(thi.value);
}



function Edit_ValidateReel(thi)
{
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }
	
   if( event.keyCode == 45 && thi.value.indexOf('-') < 0  ) {
      thi.value = '-' + thi.value;
	  event.returnValue = false;
   }
   else 
   if( event.keyCode == 43 && thi.value.indexOf('-') >= 0  ) {	   
	  thi.value = thi.value.replace(/-/,"");
	  event.returnValue = false;
   }
	
   if (((event.keyCode < 48) || (event.keyCode > 57)) && (event.keyCode != 46))
      event.returnValue = false;
	  
   if(event.keyCode == 46 && thi.value.indexOf('.') >=0 )
      event.returnValue = false;
}

function Edit_ValidateDate(thi)
{
  if(((event.keyCode < 48) || (event.keyCode > 57)) && (event.keyCode != 47))
     event.returnValue = false;
}


function GetFileExt(VALUE){
  var w_space = '.';
  var v_length = VALUE.length;
  var strTemp = "";
  if(v_length <= 0){
    return"";
  }
  
  var iTemp = v_length - 1;
  while(iTemp > -1){
  
     if(VALUE.charAt(iTemp) == w_space)
        break;
     
     strTemp = VALUE.substring(iTemp ,v_length );
     iTemp = iTemp - 1;
  }

  return strTemp.toLowerCase();
}

function TrimRight(VALUE){

   var w_space = String.fromCharCode(32);
   var v_length = VALUE.length;
   var strTemp = "";
   if(v_length <= 0){
      return"";
   }
   var iTemp = v_length -1;

   while(iTemp > -1){
     if(VALUE.charAt(iTemp) == w_space){
     }
     else{
        strTemp = VALUE.substring(0,iTemp + 1 );
        break;
     }
     iTemp = iTemp - 1;

   } //End While
   
   return strTemp;

} //End Function


function TrimLeft(VALUE){

   var w_space = String.fromCharCode(32);
   var v_length = VALUE.length;
   var strTemp = "";
   if(v_length <= 0){
     return"";
   }
   var iTemp = 0;

   while(iTemp < v_length){
     if(VALUE.charAt(iTemp) == w_space){
     }
     else{
        strTemp = VALUE.substring(iTemp , v_length );
        break;
     }
     iTemp = iTemp + 1 ;

   } //End While
   
   return strTemp;

} //End Function

function CheckMailEnglishSting(VALUE){

   var v_length = VALUE.length;
   var iTemp = 0;
   var OK = 0;  
   while(iTemp < v_length) {
	  OK = 0;  

	  if( VALUE.charCodeAt( iTemp ) >= 65 && VALUE.charCodeAt( iTemp ) <= 90 )
	     OK = 1;  
	  else 	 
	  if( VALUE.charCodeAt( iTemp ) >= 97 && VALUE.charCodeAt( iTemp ) <= 122 )
	     OK = 1;  
	  else	 
	  if( VALUE.charCodeAt( iTemp ) >= 48 && VALUE.charCodeAt( iTemp ) <= 57 )
	     OK = 1;  
      else
	  if( VALUE.charCodeAt( iTemp ) == 46 )
	     OK = 1;  
	  else
	  if( VALUE.charCodeAt( iTemp ) == 95 )
	     OK = 1;  

      if( OK == 0 )
	     return false;
  
      iTemp = iTemp + 1;
   }
   return true;  	
}


function CheckMail(VALUE){
   
   var w_sep = '@';

   var iAdim = 0;
   var v_length = VALUE.length;
   
   if( v_length <= 0 ) {
      return  false;
   }
   
   var iTemp = 0;
   var iDolu = 0;
   while(iTemp < v_length){
     
     if( VALUE.charAt(iTemp) == '@' && iAdim > 0 )
        return false;
        
     if( VALUE.charAt(iTemp) == w_sep){
        if( iAdim == 0 ) {
           if( iDolu <= 0 )
              return false; 
              
           w_sep = '.';
           iDolu = 0;   
           iAdim = 1;
           iTemp = iTemp + 1;
           continue; 
        }
        else
        if( iAdim == 1 ) {
           if( iDolu <= 0 )
              return false; 
           iDolu = 0;   
           iAdim = 2;
           iTemp = iTemp + 1;
           continue; 
           
        }
        else
        if( iAdim == 2 ) {
           if( iDolu <= 0 )
              return false; 
           iDolu = 0;   
           iAdim = 2;
           iTemp = iTemp + 1;
           continue; 
           
        }
        
     }
     else
     if(VALUE.charAt(iTemp) == ' ')
        return false;
     

     iDolu = iDolu + 1;    

     
     iTemp = iTemp + 1;
   }
   
   if( iAdim == 2 && iDolu > 0 )
      return true;
      
      
   return false;
}


function CheckDate( VALUE ){

   var w_seperator = String.fromCharCode(47);

   
   var v_length = VALUE.length;
   if(v_length <= 0){
     return false;
   }
   
   var strGun = "";
   var strAy = "";
   var strYil = "";
   
   var iTemp = 0 ;
   var iPart = 0 ;
   var iTotaSeperator = 0 ;
   while( iTemp < v_length ) {
        
     if( VALUE.charAt(iTemp) == w_seperator ) {
        iTotaSeperator++;
        iPart++;
        iTemp++;
        continue;    
     }   
        
     if( iPart == 0 ) {
        strGun += VALUE.charAt(iTemp);
     }

     
     if( iPart == 1 ) {
        strAy += VALUE.charAt(iTemp);
     }
     
     if( iPart == 2 ) {
        strYil += VALUE.charAt(iTemp);
     }
     iTemp++;
   }
   
   

   if( strGun.length < 1 || strGun.length > 2 )
      return false;
      
   if( strAy.length < 1 || strAy.length > 2 )
      return false;
      
   if( strYil.length != 4 )
      return false;

   if( iTotaSeperator != 2 )
      return false;


   iTemp= 0;
   v_length = strGun.length;
   while( iTemp < v_length ) {
      if( strGun.charAt(iTemp) < '0' || strGun.charAt(iTemp) > '9' )  
         return false;    
      iTemp++;   
   }

   iTemp= 0;
   v_length = strAy.length;
   while( iTemp < v_length ) {
      if( strAy.charAt(iTemp) < '0' || strAy.charAt(iTemp) > '9' )  
         return false;    
      iTemp++;      
   }

   iTemp= 0;
   v_length = strYil.length;
   while( iTemp < v_length ) {
      if( strYil.charAt(iTemp) < '0' || strYil.charAt(iTemp) > '9' )  
         return false;    
      iTemp++;      
   }

   
   var iGun = parseInt( strGun , 10 );
   var iAy  = parseInt( strAy , 10 ); 
   var iYil = parseInt( strYil , 10 );
   
   if( iYil < 1900 || iYil > 2100 )
      return false;   
 
   if( iAy < 1 || iAy > 12 )
      return false;   

   var iMaxGun = 31;
   switch( iAy ) {
      case 2:
        iMaxGun = 28;      
        if( (iYil % 4 ) == 0 )
           iMaxGun = 29;
           
      break;

      case 4:  
      case 6:
      case 9:
      case 11:
        iMaxGun = 30
      break;
   
   }
      
   if( iGun < 1 || iGun > iMaxGun )
      return false;   


   return true;


} //End Function


function Verify_TextField( idCtrl, msg ) {

   var str = document.getElementById(idCtrl).value;
   str = TrimRight( str );

   if( str.length <= 0 ) {
      UMessageBox( msg );      
      document.getElementById(idCtrl).focus();
      return false;
   }
   return true;
}

function Verify_PasswordField( idCtrl, msg ) {
   str = document.getElementById(idCtrl).value;
   str = TrimRight( str );
   if( str.length < 5 ) {
      UMessageBox( msg );      
      document.getElementById(idCtrl).focus();
      return false;
   }
   return true;
}

function Verify_CheckField( idCtrl, msg ) {
   if( document.getElementById(idCtrl).checked ) {
      return true;
   }
   else {
      UMessageBox( msg );      
      document.getElementById(idCtrl).focus();
      return false;   
   }
   
   return true;
}


function GetValue( idCtrl ) {
   return document.getElementById(idCtrl).value;
}


function Verify_MailField( idCtrl , msg ) {
   str = document.getElementById(idCtrl).value;
   str = TrimRight( str );
   if( str.length <= 0 ) {
      UMessageBox( msg );      
      document.getElementById(idCtrl).focus();
      return false;
   }
   else
   if( !CheckMail( str ) ) {
      UMessageBox( msg );      
      document.getElementById(idCtrl).focus();
      return false;
   }
        
   return true;
}


function Verify_DateField( idCtrl , msg ) {
   str = document.getElementById(idCtrl).value;
   str = TrimRight( str );
   if( str.length <= 0 ) {
      UMessageBox( msg );      
      document.getElementById(idCtrl).focus();
      return false;
   }
   else
   if( !CheckDate( str ) ) {
      UMessageBox( msg );      
      document.getElementById(idCtrl).focus();
      return false;
   }
        
   return true;
}


