var mpUrl='modality_pages.php';
var mDiv='modalityDIV';
var checkTXT='';

// Swissterminal custom
$(document).ready(function(){ 
    $("#searchDiv").keyup(function(event){
      if(event.keyCode == 13){
        ZMSearchST();
      }
    });
});

function ZMSearchST(){
        fContainernr = $('#containernr').val();
        fKundenreferenz = $('#kundenreferenz').val();
        fFreistellung = $('#freistellung').val();  
        fAvisierung = $('#avisierung').val();  
        if(fContainernr == '' && fKundenreferenz == '' && fFreistellung == '' && fAvisierung == ''){
            alert('At least one searchfield should be filled in.')
        }  else{
            ahah(mpUrl, 'p=11&containernr=' + fContainernr + '&kundenreferenz=' + fKundenreferenz + '&freistellung=' + fFreistellung + '&avisierung=' + fAvisierung, mDiv);  
        }
       
}

// END Swissterminal custom 

function ahah(url,vars, target) {
	document.getElementById(target).innerHTML =  'Request is send.';
   if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {ahahDone(target);};
        req.open("POST", url, true);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        req.send(vars);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {ahahDone(target);};
            req.open("POST", url, true);
			req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            req.send(vars);
        }
    }
}

function ZM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function ahahDone(target) {
   // only if req is "loaded"
   if (req.readyState == 4) {
       // only if "OK"
       if (req.status == 200 || req.status == 304) {
           results = req.responseText;
           document.getElementById(target).innerHTML =  results;
       } else {
           document.getElementById(target).innerHTML="ahah error:\n" +
               req.statusText;
       }
   }
}

function ahahTXT(url) {
   if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {ahahDoneTXT();};
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {ahahDoneTXT();};
            req.open("GET", url, true);
            req.send();
        }
    }
	
}

function ahahDoneTXT() {
   // only if req is "loaded"
   if (req.readyState == 4) {
       // only if "OK"
       if (req.status == 200 || req.status == 304) {
           checkTXT = req.responseText;
       } 
   }
}


function ZMGetVal(el){
	var ret;
	field=document.getElementById(el);
	ret=eval('field.value');	
	return ret;
}

function ZMLogin(){
	var usercode=ZMGetVal('usercode');
	var password=ZMGetVal('password');
	if(usercode==''){
		alert('Please fill in your username');
	} else if(password==''){
		alert('Please fill in your password');
	} else{
		ahah(mpUrl, 'usercode=' + usercode + '&password=' + password, mDiv);
	}
}

function ZMLogoff(){
	ahah(mpUrl, 'action=logoff', mDiv);
}

function ZMSearch(pFunctionNumber, pSearchFor){
   	ahah(mpUrl, 'p=11&functionNumber=' + pFunctionNumber + '&searchFor=' + pSearchFor, mDiv);
}



function ZMshowPage(id){
	ahah(mpUrl, 'p=' + id, mDiv);
}

function ZMcheckContainerId(t, tnr) { //v3.0
  if(tnr==0){
      var fn = document.containers.containernr;
    }
  else if(tnr==1){
      var fn = document.srequest.query;
    }
  var waarde = fn.value, i = 1, errortext ='', cw = new Array();
  cw['0']=0, cw['1']=1, cw['2']=2, cw['3']=3, cw['4']=4, cw['5']=5, cw['6']=6, cw['7']=7; cw['8']=8, cw['9']=9;
  cw['A']=10, cw['B']=12, cw['C']=13, cw['D']=14, cw['E']=15, cw['F']=16, cw['G']=17, cw['H']=18, cw['I']=19, cw['J']=20;
  cw['K']=21, cw['L']=23, cw['M']=24, cw['N']=25, cw['O']=26, cw['P']=27, cw['Q']=28, cw['R']=29, cw['S']=30, cw['T']=31;
  cw['U']=32, cw['V']=34, cw['W']=35, cw['X']=36, cw['Y']=37, cw['Z']=38;

  while(waarde.length > 10 && i < 6){
        waarde = waarde.replace(' ','');
        i++;
        }
  // even mooi opmaken
  waarde = waarde.toUpperCase();
  var netjes = waarde.substr(0,4) + ' ' + waarde.substr(4,6) + ' ' + waarde.substr(10,1);
  fn.value=netjes;

  // als aantal in string groter dan 11 is dan waarschuwen
  if(waarde.length > 11)
          errortext += 'Container number should contain 11 digits \n';
  else if(waarde.length < 11)
          errortext += 'Container number should contain 11 digits \n';
  else {

      var multiply = 1, totSum = 0, w;
      for(var j = 0 ; j < 10; j++){
         w = waarde.substr( j, 1);
         totSum += multiply * cw[w];
         multiply *= 2;
         }
      var controlegetal = totSum - 11 * Math.floor(totSum/11);
      // regel: indien controlegetal gelijk is aan 10 dan 0 nemen
      if(controlegetal == 10)
         controlegetal=0;
      w = waarde.substr( j, 1);
      if(controlegetal != cw[w])
         errortext += 'Container number doesn\'t satisfy the standard. \n Last digit should be ' + controlegetal + '\n' ;

    // bekijk of de juiste letters / cijfers gebruikt zijn
    for(var k = 10 ; k >= 0; k--){
        w = waarde.substr(k,1);
        if(k >= 4){
            var code = w.charCodeAt(0);
            if((code<48) || code>57){
                errortext = 'Container number doesn\'t satisfy the standard. \n Last seven digits should be numeric characters (0-9) \n' ;
                }
            }
        else{
            // waarde moet character zijn
            var code = w.charCodeAt(0);
            if((code<65) || (code>90 && code<97) || code>122){
                errortext = 'Container number doesn\'t satisfy the standard. \n First four digits should be letters (A-Z) \n' ;
                }
            }
        }
    }
  // bereken de formule
  // geef error indien nodig
  if( '' < errortext){
      if(t == 'alert'){
        alert(errortext);
        }
    else{
        return errortext;
        }
    }
  else{
    //even juiste opmaak maken
    return '';
    }


}// einde function checkId()








