<script language="JavaScript" type="text/JavaScript">
 
/* ================================= */
/*     ENTER GO TO NEXT FIELD        */
/* ================================= */ 

var items = document.getElementsByTagName("*");
var myItems=[];var nn=-1;
var myItemType=[];

 for (var i = 0; i < items.length; ++i) {
 	 if(items[i].tagName == 'TEXTAREA' || items[i].tagName == 'INPUT') {	 
        nn=nn+1;
 	    myItems[nn]= items[i].name;
		myItemType[nn]= items[i].tagName;
  }
 }
 
 document.onkeydown = processKey;
 
function processKey(e) {
if (null == e) { e = window.event; }


 
if (e.keyCode==13 && document.activeElement.name.indexOf("FindWhat")>-1){
	  // alert("Edo Eimaste!!!");
	 e.keyCode=0; var a=document.activeElement.name; var d0 = a.split('_'); 
	 mySubmit(d0[0]+'_'); 
	 return;
}
 
if (e.keyCode == 13) { 
 
         e.keyCode=0;
      var myFocused=document.activeElement;
		for (var i = 0; i < myItems.length; ++i) {
			 if(myFocused.name==myItems[i]){
				 e.keyCode=0;
				 document.getElementById(myItems[i+1]).focus();
				 if (myItemType[i+1] != 'TEXTAREA'){
					document.getElementById(myItems[i+1]).select();
				 }else if (myItemType[i+1]== 'TEXTAREA'){
					//e.keyCode=0;e.keyCode=9;	
						  	  e.keyCode=0;
						var length = document.getElementById(myItems[i+1]).value.length;  
						document.getElementById(myItems[i+1]).setSelectionRange(length, length);
						// e.keyCode=8;e.keyCode=8;
				 }else {
					//	document.getElementById(myItems[i+1]).focus();  
				  	  e.keyCode=0;
						var length = document.getElementById(myItems[i+1]).value.length;  
						document.getElementById(myItems[i+1]).setSelectionRange(length, length); 
				 }
				  break;
				 }
		    }
	} else if(e.keyCode==38) { e.keyCode =0;
     var myFocused=document.activeElement;
		for (var i = 0; i < myItems.length; ++i) {
			 if(myFocused.name==myItems[i] ){document.getElementById(myItems[i-1]).focus();
			 document.getElementById(myItems[i-1]).select(); break;}
		    }			
	} else if(e.keyCode==27) {confirmExit();} 
}
 
/* ================================= */
/*              REC SAVE             */
/* ================================= */ 

function submit(cmd,IDc){
   
var RequiredField="<?php echo $RequiredField;?>";
var rq_value="";

if(RequiredField!==''){
	  //  alert(cmd+" " +IDc+" , " + "<?php echo $RequiredField;?>");
	    rq_value=document.getElementById("<?php echo $RequiredField;?>").value;
	if(cmd=='sv' &&  rq_value==''){
		 document.getElementById("<?php echo $RequiredField;?>").style.borderColor = "red";
		 return;
	}
}
   if(cmd=='sv'){
		 if( rq_value!==''){
			 document.<?php echo $MyFormName;?>.action='_rs.php';
			 document.getElementById("<?php echo $MyFormName;?>").submit(); 
		 }
	}else if(cmd=='new'){
	document.cookie = '<?php echo $IDcField;?>=' + IDc;
	document.<?php echo $MyFormName;?>.action='_ad.php';
	document.getElementById("<?php echo $MyFormName;?>").submit(); 
	}else if(cmd=='dl'){
		if(IDc!==0){		
		  document.<?php echo $MyFormName;?>.action='_dl.php';
		  document.getElementById("<?php echo $MyFormName;?>").submit(); 
		}		  
	}
 }
 

function PrintDoc() {
	var toPrint = document.getElementById('printarea');
	var popupWin = window.open('', '_blank', 'width=650,height=450,location=no, resizable=yes,left=200px');
	popupWin.document.open();
	popupWin.document.write('<html><title>::Preview::</title><link rel="stylesheet" type="text/css" href="print.css" /></head><body onload="window.print()">')
	popupWin.document.write(toPrint.innerHTML);
	popupWin.document.write('</html>');
	popupWin.document.close();
 }

function OpenFormADD(IDc){
 document.cookie = '<?php echo $MyIDFieldName; ?>=' + IDc;
 var w=850;var h=600; 
 var oPenFrm = "<?php echo $MyADDFormName; ?>.php" ; // ?IDc="+IDc;
 var NewWindow = window.open( oPenFrm, "_self"  ); 
    NewWindow.moveTo(0,0);
    NewWindow.resizeTo(screen.width,screen.height);   
   NewWindow.focus();
}

function OpenFormADD_onWin___11111(IDc){
 document.cookie = '<?php echo $MyIDFieldName; ?>=' + IDc;
 var w='<?php echo $w; ?>';var h='<?php echo $h; ?>'; 
 var oPenFrm="<?php echo $MyADDFormName; ?>.php" ;  
 
    var left = (screen.width/2)-(w/2);
    var top = (screen.height-h)/2-50;
 
  var Pos1 = "top="+top+", Left="+left+", width="+w+", height="+h+" ";
  //var Pos2 = "top="+top+", Left="+left+", width=800, height=0 ";
  var NewWindow="";

    NewWindow = window.open( oPenFrm, "sadasdasd",  Pos1+ ",resizable=no, Linkbar=0, addressbar=no, directories=no, toolbar=no,location=no,status=no, menubar=no, scrollbars=no, modal=yes, copyhistory=no "); 
  // NewWindow = window.open( oPenFrm, null,  Pos2+ ",resizable=yes, Linkbar=0, addressbar=no, directories=no, titlebar=no,toolbar=no,location=no,status=no, menubar=no, scrollbars=yes, modal=yes, copyhistory=no"); 
  
// NewWindow.blur();
 NewWindow.focus();
}


function getOffset(el) {
  var rect = el.getBoundingClientRect();
  return {
    left: rect.left + window.scrollX,
    top: rect.top + window.scrollY,
	height: rect.height  
  };
}
	
/* ================================= */
/*     CBO AND CHK FUNCTIONS         */
/* ================================= */ 

function cboGetIndex(cboName,IDcName) {
	var e = document.getElementById(cboName);  
	var Idc1 = e.options[e.selectedIndex].value; 
	document.getElementById(IDcName).value = Idc1;
}

function comboChange(e,fldName,IDName){
  document.getElementById(fldName).value = e.options[e.selectedIndex].text;
  document.getElementById(IDName).value = e.options[e.selectedIndex].value;
  var myID=document.getElementById(IDName).value
  if (IDName=='IDPelati'){ pel_change(myID); }
}

function chkUpd(element){
	var chk=document.getElementById(element).value;
	if(chk==-1){chk=0;}else{chk=-1;}
	document.getElementById(element).value=chk;	
 } 


function pel_change(IDc) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
			var mval=this.responseText.trim(); 
			var mval=mval.split("\n");
			for (var i = 0; i < mval.length; ++i) {
				 var myrec=mval[i].split('=');
				if(document.getElementById(myrec[0])) {
					document.getElementById(myrec[0]).value = myrec[1];
				}
			}
		   }
        };
        xmlhttp.open("GET", "_PelRecRead.php?q="+IDc, true);
        xmlhttp.send();  
  }
  
/* ================================= */
/*     DATE AND NUMBER FUNCTIONS     */
/* ================================= */ 

 function isValidDate(dateString){
	
    // First check for the pattern
    if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
        return false;

    // Parse the date parts to integers
    var parts = dateString.split("/");
    var day = parseInt(parts[1], 10);
    var month = parseInt(parts[0], 10);
    var year = parseInt(parts[2], 10);

    // Check the ranges of month and year
    if(year < 1000 || year > 3000 || month == 0 || month > 12)
        return false;

    var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

    // Adjust for leap years
    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
        monthLength[1] = 29;

    // Check the range of the day
    return day > 0 && day <= monthLength[month - 1];
} 
 
 function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57) & charCode != 32) {
        return false;
    }
    return true;
}
 
 function notNumbers(evt) {
    evt = (evt) ? evt : window.event;

    var charCode = (evt.which) ? evt.which : evt.keyCode;
 
	if ( charCode > 31 && (charCode < 48 || charCode > 57) || (charCode==8) ) {
        return true;
    }
    return false;
}

function pad(num, size) {
    var s = "00000000000" + num;
    return s.substr(s.length-size);
}
//function pad(n, width, z) { z = z || '0'; n = n + ''; return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;}
     
/* ================================= */
/*     DATE AND NUMBER FUNCTIONS     */
/* ================================= */ 
 
function myBlurFunction() {
   alert("Δεν έχει γίνει αποθήκευση της καρτέλας. Να γίνει αποθήκευση της καρτέλλας; ");
}
 
function mySubmit(prefix){
	var find_field_name=prefix+'FindWhat';
	var txt=document.getElementById(find_field_name).value; 
	var myFrn="<?php echo $MyFormName; ?>";
	
 // alert(myFrn+".php?"+prefix+"FindWhat="+txt);
   document.cookie = find_field_name+'=' + txt;
 // document.cookie = prefix+"FindWhat+'=' + escape(txt);
  
// 	document.location.href="<?php echo $MyFormName; ?>.php?"+prefix+"FindWhat="+txt ;

document.location.href = myFrn+".php?"+prefix+"FindWhat="+txt; 

}
 
window.onunload = refreshParent;
 
 function refreshParent() {
       // window.opener.location.reload();
 }
 
function refresh(prefix){
	document.getElementById(prefix+'FindWhat').value='';
	document.location.href = "<?php echo $MyFormName; ?>.php" ;
}

function onLoadFocus(){
	if(isset($FocusField)){
	   document.getElementById('<?php echo $FocusField;?>').focus();
	}
 }
 
function gotoEndOfText(e){
    e.selectionStart = e.selectionEnd = e.value.length;
 } 
 
function gotoEndOfText1(e) {
 // var el = document.getElementById(id);
  
 e.focus();
  if (typeof e.selectionStart == "number") {
      e.selectionStart = e.selectionEnd = e.value.length;
  } else if (typeof e.createTextRange != "undefined") {           
      var range = e.createTextRange();
      range.collapse(false);
      range.select();
  }
 
  
}
 
 function empty(e) {
  switch (e) {
    case "":
    case 0:
    case "0":
    case null:
    case false:
    case typeof(e) == "undefined":
	case typeof(e) == "Undefined variable":
      return true;
    default:
      return false;
  }
}


function getCookie(name) {
    function escape(s) { return s.replace(/([.*+?\^$(){}|\[\]\/\\])/g, '\\$1'); }
    var match = document.cookie.match(RegExp('(?:^|;\\s*)' + escape(name) + '=([^;]*)'));
    return match ? match[1] : null;
}

</script>
 