// {{{ explode
function explode( delimiter, string, limit ) {
    // Split a string by string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_explode/
    // +       version: 809.522
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
 
    var emptyArray = {0: ''};
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}// }}}



/*
	Trebuiesc incluse: 
	<script type="text/JavaScript" src="include/js/jquery.js"></script>
	<link type="text/css" href="include/css/redmond/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
	<script type="text/javascript" src="include/js/jquery-ui-1.7.2.custom.min.js"></script>
 *
 **/

	function jDatePicker(name){
		/* example of use: <input type="text" value="08/10/2008" id="datepicker"> */
		/* Trebuiesc folosite:
			<script type="text/JavaScript" src="include/js/jquery.js"></script>
			<link type="text/css" href="include/css/redmond/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
			<script type="text/javascript" src="include/js/jquery-ui-1.7.2.custom.min.js"></script>
		 */

		/* ("slow", "normal", "fast"), or '' for immediately */
		/* slideDown', 'fadeIn', show */
		$(function(){
			// Datepicker
			$('#'+name).datepicker({
				inline: true,
				changeYear: false,
				changeMonth: false,
				closeText: 'Close',
				dateFormat: 'mm/dd/yy',
				duration: 'slow',
				gotoCurrent: true,
				showAnim: 'fadeIn',
				showButtonPanel: true,
				yearRange: '1950:2020'
			});
		});
	}








	function jSortable(inputUpdate){
		/* Pentru folosire
			<script type="text/JavaScript">jSortable('sortedList'); </script
			<input id="sortedList" type="hidden" />
			<ul id="sortable">
				<li id="a" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 1</li>
				<li id="b" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 2</li>
			</ul>
		 */


		/* Trebuiesc introduse clasele
			#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
			#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
			#sortable li span { position: absolute; margin-left: -1.3em; }
		*/

		$(function() {
			return $('#sortable').sortable({
				cancel: 'button',
				update: function(){
					document.getElementById(inputUpdate).value = $('#sortable').sortable('toArray');
				}
			});
			$("#sortable").disableSelection();
		});
	}


	function jDialog2(otherParams){
		/* to use:
			<button onclick="jDialog2('elemId:=dialog;;dWidth:=500;; dHeight:=200;;)">Dialog</button>
			<div id="dialog" title="Varu Dialog" style="display: none"></div>
		 *    */
		if(otherParams != ''){ /* se pot adauga parametrii in urmatoarea sintaxa: var1:valoare;var2=valoare2 */
			while ( otherParams.indexOf(' ') != -1){
				otherParams = otherParams.replace(' ', '');
			}

			slices = explode(';;', otherParams);
			for(zu = 0; zu < slices.length-1; zu++){
				sliceVar = explode(':=', slices[zu]);
				varAll = sliceVar[0] + ' = "'+ sliceVar[1]+'";';
				eval(varAll);
			}

/* check variable and assign default values */
			if(typeof(elemId) == 'undefined')
				elemId = 'dialog';
			if(typeof(dWidth) == 'undefined')
				dWidth = '500';
			if(typeof(dHeight) == 'undefined')
				dHeight = '400';
		}
		$.ui.dialog.defaults.bgiframe = true;
		$(function() {
			$("#"+elemId).dialog({
				autoOpen: false,
				closeOnEscape: true,
				draggable: true,
				maxHeight: 700,
				maxWidth: 960,
				modal: true,
				position: 'center',
				resizable: false,
				width: parseInt(dWidth),
				height: parseInt(dHeight),
				bgiframe: true
			});
		});
		$("#"+elemId).dialog( 'open')
		/*	height: 530
		 * Possible values: 'center', 'left', 'right', 'top', 'bottom',
		 **/
	}

	function jDialog(){
		/* to use:
			<button onclick="jDialog()">Dialog</button>
			<div id="dialog" title="Varu Dialog" style="display: none"></div>
		 *    */


		$.ui.dialog.defaults.bgiframe = true;
		$(function() {
			$("#dialog").dialog({
				autoOpen: false,
				closeOnEscape: true,
				draggable: true,
				show: 'slide',
				hide: 'slide',
				maxHeight: 600,
				maxWidth: 960,
				modal: true,
				position: 'center',
				resizable: false
			});
		});
		$('#dialog').dialog( 'open')
		/*	height: 530
		 * Possible values: 'center', 'left', 'right', 'top', 'bottom',
		 **/
	}


	var flipKHJKH = 0;
	function jToggle(elementId){
		/*if(flipKHJKH ++ % 2 == 0)
			$(elementId).fadeIn("slow");
		else
			$(elementId).slideUp("slow"); */
		$('#'+elementId).slideToggle("slow");
	}



	function jAjax(urlToProcess, dataToSend, otherParams){
		/*	urlToProcess - fisierul care prelucarea datele sau fisierul care va fi afisat
		 *	dataToSend -  data care se trimite la prelucrat
		 * otherParams - alti posibili parametrii
				timeOut (ms),
				async (true || false)
				updateElem  - elementul care va fi actualizat. Vtoto: sa fac cu tip elem: 1. innerHTML sau value (pt inputuri)
				updateElemType - ( inner || value )daca se poate cu innerHtml sau value
				loadingElem
				redirectTo - daca se face redirect la pagina
				jFnct - daca se executa o functie de javascript dupa ce s-a terminat requestul.
			exemplu: timeOut: 200; redirTo: index.php;

			Exemplu utilzare:
			jAjax('logins.php', postStringForm(document.forms[0]), 'timeOut:= 2000;; updateElem:= toUpdate;; updateElemType:= inner;; loadingElem:= loading;; jFnct:= alert(\'Funct!\');;')

			Cea mai scurta apelare este: Trebuie sa existe elementul pentru loading si elementul pentru update. 
			Ele trebuiesc numite: loading si toUpdate
			Divul de loading trebuie sa aibe display: none;
			jAjax('login.php', postStringForm(document.forms[0]), '');
			sau
			jAjax('login.php', 'var1=valoare1', '');
		 **/

		if(otherParams != ''){ /* se pot adauga parametrii in urmatoarea sintaxa: var1:valoare;var2=valoare2 */
			while ( otherParams.indexOf(' ') != -1){
				otherParams = otherParams.replace(' ', '');
			}

			slices = explode(';;', otherParams);
			for(zu = 0; zu < slices.length-1; zu++){
				sliceVar = explode(':=', slices[zu]);
				varAll = sliceVar[0] + ' = "'+ sliceVar[1]+'";';
				eval(varAll);
			}
		}

/* check variable and assign default values */
		if(typeof(timeOut) == 'undefined')
			timeOut = '10000'; /* 10 secunde */
		if(typeof(updateElem) == 'undefined')
			updateElem = 'toUpdate';
		if(typeof(updateElemType) == 'undefined')
			updateElemType = 'inner';
		if(typeof(loadingElem) == 'undefined')
			loadingElem = 'loading';
		if(typeof(redirectTo) == 'undefined')
			redirectTo = '';
		if(typeof(jFnct) == 'undefined')
			jFnct = '';

		$.ajax({
			type : 'POST',
			url : urlToProcess,
			dataType : 'html',
			timeout: timeOut,
			//cache: false,
			async: true,
			data: dataToSend,

			beforeSend: function(){
				$("#"+loadingElem).fadeIn(700);
			},

			success : function(data){ /*A function to be called if the request succeeds. The function gets passed two arguments: The data returned from the server, formatted according to the 'dataType' parameter, and a string describing the status. */
				$("#"+loadingElem).fadeOut(700);

				if (data.error === true)
					$('#'+updateElem).innerHTML = 'Errors in received data.';
				else{

					if(redirectTo!= '' && data == '')
						window.location = redirectTo;

					if(updateElemType == 'inner') /* se face la elementele care suporta innerHTML */
						$('#'+updateElem).html(data);
					else
						document.getElementById(updateElem).value = data; /* se face update la input value */

					if(jFnct!='')
						eval(jFnct);
				}
			},
			error : function(xhr, textStatus, errorThrown) {
				$("#"+loadingElem).fadeOut(500);
				eroare ='There was an error with the request. Error code: '+textStatus;
				if(updateElemType == 'inner')
					$('#'+updateElem).removeClass().addClass('err').html(eroare).show(500);
				else
					document.getElementById(updateElem).value = eroare;
			}
		});
	}