//=============================================================================
// SHARED FILE: Various common functions
//
// Copyright (c) 2000 Prins & Spika
// 
// $Header: /Web/SHS-CMS/ehd/mycommon.js 17    28.07.04 11:08 Ondrusm $
//=============================================================================
//
// Each function has to have following descriptions (no blank lines) which are
// used to generate documentation.
//
//
// EXAMPLE:
//
//   //-----------------------------------------------------------------
//   function MyFunction (nCount, sMessage)
//   // #@Description: Function description
//   // #@Input: item count (integer), message (string)
//   // #@Output: return code (boolean)
//   // #@Author: Author Name
//   // #@Modified: Modification date


function IsNumber( inputVal, bSelect, sMessage ) {
//-------------------
// #@Description: Return true if 'inputVal' is possible to convert to number. Normalizing 'inputVal'. If bSelect is set to true and object is not number, object will be selected.   
// #@Input:  input object, bSelect( default false )   
// #@Output: boolean 
// #@Author: Michal Barna
// #@Modified: 2.5.2000
   var bRet = false;
   var cVal;
   
   if( typeof( bMessage ) != "boolean" )
       bMessage=true;
   
   if( typeof(inputVal.value)=="string" ) {
       cVal=inputVal.value;
       cVal=cVal.replace(",",".");       
       if( !isNaN(parseFloat(cVal)) ) {
           bRet=true;
           inputVal.value=parseFloat(cVal);
       }    
   }
   else    
   if( typeof(inputVal.value)=="number" ) 
       bRet=true;
       
   if( !bRet && bSelect) {
         alert( "Musíte zadat číselnou hodnotu '" + sMessage + "' !" );
         inputVal.select();        
         inputVal.focus();

   }
   
   return bRet;
}

function ToNumber( inputVal ) {
//-------------------
// #@Description: Return numeric value of 'inputVal' or 0   
// #@Input:  numeric expression    
// #@Output: numeric value 
// #@Author: Michal Barna
// #@Modified: 3.5.2000
   var expression;
   var nRet=0, nVal;    
   
   if( typeof(inputVal)=="string" ) {
       nVal=parseFloat(inputVal);
       if( !isNaN(nVal) )
           nRet=nVal;
   }
   else    
   if( typeof(inputVal)=="number" ) 
       nRet=inputVal;
       
   return nRet;
}


function Trim( cInput ) {
//-------------------
// #@Description: Return trimed string or "" if input is not string   
// #@Input:  string expression
// #@Output: trimed string 
// #@Author: Michal Barna
// #@Modified: 2.5.2000
   var cStr="";
   var nLength,i;
   var nFirst,nLast;
   
   if( typeof(cInput)=="string" ) {
       nLength=cInput.length;
       nFirst=0;
       while( cInput.charCodeAt(nFirst)<33 && nFirst<nLength ) nFirst++;
       cStr=cInput.substr(nFirst,nLength);
       nLast=cStr.length-1;
       while( cStr.charCodeAt(nLast)<33 && nLast>0 ) nLast--; 
       cStr=cStr.substr(0,nLast+1);
   }
   
   return cStr; 
} 

function IsDate( inputVal, bSelect ) {
//-----------------------
// #@Description: Return true if inputVal.value is formated date dd.mm.yyyy, ignore empty inputVal   
// #@Input:  object expression, bSelect( default false )
// #@Output: bool 
// #@Author: Michal Barna
// #@Modified: 12.5.2000
   var aStr="";
   var bRet=true;

   if( typeof(inputVal.value)=="string" && Trim(inputVal.value).length>0 ) {
       // testing length
       aStr=inputVal.value.split(".");
       if( aStr.length!=3 ) 
           bRet=false;
       else {
           dDate= new Date(aStr[2], aStr[1]-1, aStr[0]);
           
           for( i=0;i<3;i++ ) {
                s=aStr[i];
                if( s.substr(0,1) == '0'  && s.length>1 )
                    aStr[i] = s.substr(1,1);
           }            

           if( dDate.getDate() != parseInt(aStr[0],10) ||
               dDate.getMonth()+1 != parseInt(aStr[1],10) ||
               dDate.getFullYear() != parseInt(aStr[2],10) )
                  bRet=false;
                  
           if( aStr[2].length > 4 ) bRet = false
           
       }
   }
   else
      bRet=false;
       
   if( !bRet ) {           
       alert( "Musíte zadat datum ve formátu DD.MM.YYYY !" );
       if( bSelect==true ) {        
         inputVal.select();        
         inputVal.focus();
       }          
   }
       
   return bRet; 
} 



function NotEmpty( inputVal, sMessage, bSelect ) {
//-----------------
// #@Description: Return true if 'inputVal' is not empty. If object is empty, print sMessage. If bSelect is set to true and object is empty, object will be selected.   
// #@Input:  input object, sMessage, bSelect( default false )   
// #@Output: boolean 
// #@Author: Michal Ondruska
// #@Modified: 31.5.2000
   var bRet = false;
   var cVal;
   
   if( typeof(inputVal.value)=="string" ) {
       cVal=Trim(inputVal.value);
       if( cVal.length>0 )
           bRet=true;                        
   }
       
   if( !bRet && bSelect) {
       alert( "Zadejte prosím, "+ sMessage +"." );
       inputVal.select();        
       inputVal.focus();

   }
   
   return bRet;
}

function IsUserName ( inputVal, bSelect ){
//===============
  var exclude=/[ě©©ą®ľĄµ«»¦¶¬ĽŠšŽžĽľŤťŚśŹź©čřýášžíéĚŠČŘŽÝÁľÍÉ ]{1}/;
  var check=/[\w\-]+\./;
  var checkend=/\.[a-zA-Z]{2,3}$/;
  var userad=inputVal.value;

  inputVal.value=userad;
  
	if(userad.search(exclude) != -1){
		 alert("Špatný formát uľivatelského jména, pozor na mezery a vynechte háčky a čárky.");
     if( bSelect ) { 
         inputVal.select();        
         inputVal.focus();
     }    
     return false;
	}
  return true;
}


function IsEmail( inputVal, bSelect ) {
//===============
  var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
  var check=/@[\w\-]+\./;
  var checkend=/\.[a-zA-Z]{2,3}$/;
  var emailad=inputVal.value.trim();

  inputVal.value=emailad;
  
	if(((emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend) == -1)){
		 alert("Špatný formát emailové adresy!");
     if( bSelect ) { 
         inputVal.select();        
         inputVal.focus();
     }    
     return false;
	}
  return true;
}

function openform(param, wname ) {
//******************
    var lwidth=780;
    var lheight=screen.availHeight;
    var win,openPar
    
    if ( !wname ) wname = ""
    openPar='menubar=no,location=no,personalbar=no,titlebar=no,status=no,';
    openPar+='toolbar=no,directories=no,scrollbars=yes,resizable=yes,';
    openPar+='top=0,left='+(screen.availWidth-lwidth)/2+',';
    openPar+='width='+lwidth+',height='+lheight;
    win=window.open( param, wname, openPar );
    if ( self.name != wname ) {
        win.close();
        win=window.open( param, wname, openPar );
    }
//    win.focus();
    
}

/*
Notes:
'exclude' checks 5 conditions:
a) characters that should not be in the address
b) characters that should not be at the start
c) & d) characters that shouldn't be together
e) there's not more than one '@'
'check' checks there's at least one '@', later followed by at least one '.'
'checkend' checks the address ends with a period followed by 2 or 3 alpha characters
N.B. Javascript 1.2 only works with version 4 browsers and higher.
*/


// Add a function called trim as a method of the prototype 
// object of the String constructor.
String.prototype.trim = function()
{
    // Use a regular expression to replace leading and trailing 
    // spaces with the empty string
    return this.replace(/(^\s*)|(\s*$)/g, "");
}


function OtevriOknoFull( param, wname ) {
    var openPar, win
    if ( !wname ) wname = "newwindow"    
    openPar='menubar=no,location=no,personalbar=no,titlebar=no,status=no,';
    openPar+='toolbar=no,directories=no,scrollbars=yes,resizable=yes,';
    openPar+='top=0,left=0,';
    openPar+='width='+(screen.availWidth-8)+',height='+(screen.availHeight-25);
    win=window.open( param, wname, openPar );
    if ( self.name != wname ) {
        win.close();
        win=window.open( param, wname, openPar );
    }
   //win.focus();
    
}

function OpenFormular( param, wname ) {
//******************
    var lwidth=780;
    var lheight=screen.availHeight-25;
    var openPar, win
    
    if ( !wname ) wname = ""
    openPar='menubar=no,location=no,personalbar=no,titlebar=no,status=no,';
    openPar+='toolbar=no,directories=no,scrollbars=yes,resizable=yes,';
    openPar+='top=0,left='+(screen.availWidth-lwidth)/2+',';
    openPar+='width='+lwidth+',height='+lheight;
    
    win=window.open( param, wname, openPar );
    if ( self.name != wname ) {
        win.close();
        win=window.open( param, wname, openPar );
    }
//    win.focus();
    
}

function OtevriOkno( param, wname ) {
//******************
    var lwidth=780;
    var lheight=450;
    var openPar, win
    
    if ( !wname ) wname = "view"        
    openPar='menubar=no,location=no,personalbar=no,titlebar=no,status=no,';
    openPar+='toolbar=no,directories=no,scrollbars=yes,resizable=yes,';
    openPar+='top='+(screen.availHeight-lheight)/2+',left='+(screen.availWidth-lwidth)/2+',';
    openPar+='width='+lwidth+',height='+lheight;
    win=window.open( param, wname, openPar );
    if ( self.name != wname ) {
        win.close();
        win=window.open( param, wname, openPar );
    }
//    win.focus();
    
}

function OtevriOkno2( param, wname, width, height ) {
//******************
    var lwidth=width;
    var lheight=height;
    var openPar, win
    
    if ( !wname ) wname = "view"        
    openPar='menubar=no,location=no,personalbar=no,titlebar=no,status=no,';
    openPar+='toolbar=no,directories=no,scrollbars=yes,resizable=yes,';
    openPar+='top='+(screen.availHeight-lheight)/2+',left='+(screen.availWidth-lwidth)/2+',';
    openPar+='width='+lwidth+',height='+lheight;
    win=window.open( param, wname, openPar );
    if ( self.name != wname ) {
        win.close();
        win=window.open( param, wname, openPar );
    }
//    win.focus();
    
}
