/* debug action */

/*  http://ciz.ppk.go/mg/list/it/0/index.html  */

var WAITING_PROCCESS ="<p align=\"center\"><br /><br /><br /><br /><img src=\"/db/searching.gif\" title=\"Please waiting ... \" alt=\"Loading ...\" border=\"0\" /> Loading...</p>";
var actual_Language = "de";
var PutWriterD = "DisplayDEBUGResult";
var PutWriter = "DisplayFinderResult";
var URL_ONEp = "/db/finder.php";
var URL_ONEa = "/db/authcall.php";
var URL_ONEloading = "/db/Loader.php";
var URL_login = "/db/Login.php";
var URL_edit = "/mg/info/";
var CurrentMail = "nomail";
var CurrentID = "0";
var DefaultWord = "Search";
var LoadEndingPoster = 0;


function Id(objID) {
	if (!document.getElementById)	return null;	// not compatible
	return document.getElementById(objID);
}



function Finder_Search(mode)  {

  if (mode == 'undefined') {
      mode = "AA"
   }
        
  var Finderfrom = $('FromID');
  actual_Language = document.getElementById('HelpSearchLabel').title;
  var felder = "KANTON,VERBAENDE,REGI_CLASSIFICATION,BERUF";
  var zusaz = felder.split(",");
  var option = "la="+actual_Language+"&from="+Finderfrom.value+"&";
  for (var i = 0; i < zusaz.length; ++i)  {
     if (document.getElementById(zusaz[i]).value !="") {
      option +=zusaz[i]+"="+escape(document.getElementById(zusaz[i]).value)+"&";
     }
  }
  
  var FinderWord = $('FinderWord');
  var wordx = FinderWord.value;
  wordx = wordx.replace("+", "*");
  wordx = wordx.replace("|", "");
  if (wordx == "") {
   wordx ='all';
  }
  if (wordx.length > 2) {
  var findnowg = encodeURI(wordx);
  var findnowp = escape(wordx);
  option +="findquery="+findnowp+"&";
  }
  var rewrites = wordx;
  FinderWord.value = rewrites;
  var FullURIBuild = URL_ONEp +'?'+ option;
  //////alert('finder search call mode like='+ FullURIBuild);
    var WFORM = $('DisplayFinderResult');
	var req = new Request.HTML({
		//////url: URL_ONEp,		// l'url da contattare
		evalScripts: false,		// gli script contenuti nel responso non verranno azionati
		onRequest: function() {
			WFORM.set('html',WAITING_PROCCESS);
		},
		onComplete: function(tree, elements, html, js) {
			WFORM.set('html', html);
		},
		onFailure: function() {
			WFORM.set('html', 'The Request has failed...');
		}
	});
    req.get(FullURIBuild);
}

var LoadSelector = function(language) {

    actual_Language = document.getElementById('HelpSearchLabel').title;
    var WFORM = $('OptionSE');
	var req = new Request.HTML({
		url: URL_ONEp,		// l'url da contattare
		//////////update: PutWriter,			// l'elemento che verrą aggiornato al completamento della richiesta
		evalScripts: true,		// gli script contenuti nel responso non verranno azionati
		onRequest: function() {
			WFORM.set('html',WAITING_PROCCESS);
		},
		onComplete: function(tree, elements, html, js) {
			WFORM.set('html', html);
		},
		onFailure: function() {
			WFORM.set('html', 'The Request has failed...');
		}
	});
    req.get({'init': '2' , 'la': actual_Language });
};

function doit() {
if (!window.print){
alert("Your Browser not support java print!");
return
}
window.print();
}

function printhtml( string ) {

var jetzt = string.replace(/</g, "_A_");
var  order = jetzt.replace(/>/g, "_Z_");
return order;
}


function showFunction( string ) {
  hideGroup();
  document.getElementById(string).style.display = 'block';
}

function hideFunction( string ) {
  document.getElementById(string).style.display = 'none';
}

function SwapLayer( string , buttons )  {
    if (document.getElementById(string).style.display == 'none') {
         showFunction( string );
         document.getElementById(buttons).style.backgroundColor="rgb(255,204,204)";
    } else {
         hideFunction( string );
         document.getElementById(buttons).style.backgroundColor="#ccc";
    }
}

function SwapLayerEasy( string  )  {
    if (document.getElementById(string).style.display == 'none') {
         showFunction( string );
    } else {
         hideFunction( string );
    }
}


function hideGroup() {

   hideFunction('MGDetail0');
   hideFunction('MGDetail1');
   hideFunction('MGDetail2');
   //////hideFunction('MGDetail3');
   document.getElementById('Button_0').style.backgroundColor="#ccc";
   document.getElementById('Button_1').style.backgroundColor="#ccc";
   document.getElementById('Button_2').style.backgroundColor="#ccc";
   //////document.getElementById('Button_3').style.backgroundColor="#ccc";
   hideFunction('HelpSearchLabel');

}

function BuildSearchForm(eid) {

    actual_Language = document.getElementById('HelpSearchLabel').title;
    var Lastactionword;
    var DefaultFrom = '0';
    var CookieGrab = false;
    /* cookie operator */
    var operator = 0;
    var LastJob = getCookie('REGFINDER');
    if (LastJob) {
    var param_array = LastJob.split("|");
    Lastactionword = param_array[0];
    Lastactionword = Lastactionword.replace("+", " ");
            if (Lastactionword.length > 1) {
                DefaultWord = Lastactionword;
                DefaultFrom = param_array[1];
                CookieGrab = true;
            }
    }
    /* cookie operator */
    
    


    var txt0 = document.createTextNode(" ");
    document.getElementById(eid).innerHTML = ""; //// <h1>buffo</h1>
    var fsea = document.createElement("form");
    fsea.setAttribute("accept-charset","UTF-8");
    fsea.setAttribute("method","post");
    fsea.setAttribute("id","SearchPanel");
    
    var line = document.createElement("nobr");
    
    var fa1 = document.createElement("input");
    fa1.setAttribute("id","FinderWord");
    fa1.setAttribute("name","q");
    fa1.setAttribute("size","18");
    fa1.setAttribute("maxlength","100");
    fa1.setAttribute("type","text");
    fa1.setAttribute("value",DefaultWord);
    fa1.setAttribute("style","width: 123px; font-weight: bold; padding-left:5px; border:none;");
    
    var bu1 = document.createElement("span");
    bu1.setAttribute("id","ButtonSend");
    bu1.setAttribute("class","RButton");
    var ESign = String.fromCharCode(187);
    var txt1 = document.createTextNode(ESign);
    
    bu1.appendChild(txt1);       /**/
    
    var fa2 = document.createElement("input");
    fa2.setAttribute("id","FromID");
    fa2.setAttribute("name","fromrow");
    fa2.setAttribute("type","hidden");
    fa2.setAttribute("value","0");
    
    var fa3 = document.createElement("input");
    fa3.setAttribute("id","FromLA");
    fa3.setAttribute("name","la");
    fa3.setAttribute("type","hidden");
    fa3.setAttribute("value",actual_Language);
    var opt1 = document.createElement("div");
    opt1.setAttribute("id","OptionSE");
    line.appendChild(fa1);
    line.appendChild(txt0);
    line.appendChild(bu1);
    fsea.appendChild(line);
    fsea.appendChild(fa3);
    fsea.appendChild(fa2);
    fsea.appendChild(opt1);
    document.getElementById(eid).appendChild(fsea);
    ////document.getElementById(PutWriterD).innerHTML = "<h1>xxx-" + actual_Language + "-xxx</h1>";
    LoadSelector(actual_Language);
    if (CookieGrab) {
        window.setTimeout("Finder_Search()",500);
    }
    
    
}


function LaunchNext(fromnext) {

   if (fromnext > 0) {
   var Finderfrom = $('FromID');
   Finderfrom.value = fromnext;
   /////alert(fromnext);
   window.setTimeout("Finder_Search()",200);
   }


}





/* dom loader event appender */

window.addEvent('domready', function() {

   /* load form on id FinderAll */
   /////document.getElementById('FinderAll').innerHTML = WAITING_PROCCESS;
   BuildSearchForm('FinderAll');
   $('FinderWord').addEvent('focus', function(e) {
   e.stop();
       if (document.getElementById('FinderWord').value == DefaultWord) {
       document.getElementById('FinderWord').value = "";
       }
   });

   
   /* bottoni */
   var Finderfrom = $('FromID');

   if (document.getElementById('ButtonSend')) {
      $('ButtonSend').addEvent('click', function(e) {
      e.stop();
      Finderfrom.value = 0;
      Finder_Search('BB');
      });
      
      $('SearchPanel').addEvent('submit', function(e) {
      e.stop();
      Finderfrom.value = 0;
      Finder_Search('CC');
      });
   }
   
});

function  AppendOne()  {

  
   var MGID = $('Maddo_ID');
   var mg_isness = parseInt( MGID.value ); 
   var addomenge_jetzt = $('Maddo_SUMME');
   var p_isness = parseInt( addomenge_jetzt.value ) + 1; 
   
     alert('load AppendOne ....' + p_isness);
}

function  RemoveOne()  {
     alert('load RemoveOne ....');
}

//***************************************************************************************

function SendLogin()  {
    /////alert('login');
	var CurrentID = extractMemberId(window.location.pathname);
    var AFORM = $('AuthMessages');
	var req = new Request.HTML({
		url: URL_ONEa,		// l'url da contattare
		evalScripts: true,		// gli script contenuti nel responso non verranno azionati
		onRequest: function() {
			AFORM.set('html',WAITING_PROCCESS);
		},
		onComplete: function(tree, elements, html, js) {
			AFORM.set('html', html);
            var LastJob = getCookie('REGAUTH');
            if (LastJob) {
            	var param_array = LastJob.split("|");
    	    	var coppid = param_array[0];
                if (coppid.length > 1 && coppid == CurrentID) {
                        LoadAddressPage();
                }
            }
		},
		onFailure: function() {
			AFORM.set('html', 'The Request has failed...');
		}
	});
    var kpass = document.getElementById('kpars1').value;
    req.post({'authinit': '2' , 'CurrentMail': CurrentMail , 'UserPass': kpass , 'CurrentID': CurrentID });
}

function SendRPass()  {

	var CurrentID = extractMemberId(window.location.pathname);
   	var userwritemail = $('kmail2235');
    var AFORM = $('AuthMessages1');
    
    
	var req = new Request.HTML({
		url: URL_ONEa,		// l'url da contattare
		evalScripts: true,		// gli script contenuti nel responso non verranno azionati
		onRequest: function() {
			AFORM.set('html',WAITING_PROCCESS);
		},
		onComplete: function(tree, elements, html, js) {
        	AFORM.set('html', html);
		},
		onFailure: function() {
        	AFORM.set('html', 'The Request has failed...');
		}
	});
   
   req.post({'authinit': '3' , 'CurrentMail': userwritemail.value , 'Language': actual_Language , 'CurrentID': CurrentID });
   /////alert('pass new request enddddddd');
}
   
function IsNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}

function extractMemberId(url) {
	//extract member id from url
   var partsOfUrl = url.split('/');

	for (x = 0; x < partsOfUrl.length; x++) {
		if (IsNumeric(partsOfUrl[x]))
			return CurrentID = partsOfUrl[x];
	}
}



function SwapLoginPane()  {
    if (document.getElementById('WorkLPanel').style.display == 'none') {
         showFunction( 'WorkLPanel' ); 
         showFunction( 'WorkRPanel' ); /* pannello di lavoro */
         LoadLoginPanel();
    } else {
         hideFunction( 'WorkLPanel' );
         hideFunction( 'WorkRPanel' );
         ResetLayer( 'WorkRPanel' );
    }
}

/* svuota indetrno di un elemento  */
function ResetLayer( string ) {
	document.getElementById(string).innerHTML = "";
}


/* ritira configurazioni esistenti */

function getCookie(Name) {
	var search = Name + "=";					//REGAUTH=
	var offset;
	var end;
	
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
	
			var trovo = unescape(document.cookie.substring(offset, end));
			return trovo;
		}
	}
	return false;
}

//***************************************************************************************

function LoadLoginPanel() {

    var WFORM = $('WorkRPanel');
    var GetUrl = URL_login + "?CurrentMail=" +CurrentMail + "&Language=" + actual_Language;
	var req1 = new Request.HTML({
		evalScripts: false,		// gli script contenuti nel responso non verranno azionati
		onRequest: function() {
			WFORM.set('html',WAITING_PROCCESS);
		},
		onComplete: function(tree, elements, html, js) {
			WFORM.set('html', html);
		},
		onFailure: function() {
			WFORM.set('html', 'The Request has failed...');
		}
	});
    req1.get(GetUrl);
}

function LoadAddressPage() {

    var WFORM = $('WorkRPanel');
    var GetAddressUrl = URL_edit + actual_Language + '/' + CurrentID + '/index.html?type=formind&CurrentID=' + CurrentID;
	var req2 = new Request.HTML({
		evalScripts: false,		// gli script contenuti nel responso non verranno azionati
		onRequest: function() {
			WFORM.set('html',WAITING_PROCCESS);
		},
 		onComplete: function(tree, elements, html, js) { 
			window.location.href = GetAddressUrl;
 		}, 
		onFailure: function() {
			WFORM.set('html', 'The Request has failed...');
		}
 	}); 
    req2.get(GetAddressUrl);
}

function ClosePage(CurrentID) {

    var GetIndexUrl = URL_edit + actual_Language + '/' + CurrentID + '/index.html?type=fiche';
	window.parent.location.href = GetIndexUrl;
}

function reverse(string) {

	var newString = "";
	var counter = string.length;

	while (counter>0) {
		newString += string.substring(counter-1,counter);
		counter--;
	}

	return newString;
}
