
var asd=0;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


//My Functions


//Make Simple RQ
function simpleRQ(url){
	
	new Ajax(url, {
	method: 'get',
	 onSuccess: function (responseText) {
      responseText;
    }
	}).request();

}

function newPass(){
	
	var deNewPaz = $('myNewPass').value;
	
	if(deNewPaz != ''){
		simpleRQ('includes/actions.php?do=newPass&pass='+deNewPaz);
		showElement('newPass', -400);
		$('myNewPass').value = '';
	}else{
		$('statusPass').innerHTML = "<center style=\"color:#FFFF00;\">Моля въведете парола!</center>";		
	}
}

function delItem(id, type){
	
	if(type == "record"){
		simpleRQ('includes/actions.php?do=delRecord&id='+id);
		setTimeout('document.location = \'records.php\'', 1400);
	}
	
	if(type == "note"){
		simpleRQ('includes/actions.php?do=delNote&id='+id);
		setTimeout('document.location = \'notes.php\'', 1400);
	}	
	
}


function editNote(id, element, pixels){
	
	var zzz = new Ajax('includes/actions.php?do=listNote&id='+id, {
		method: 'get',
		 onSuccess: function (responseText) {
		  var rqResult = eval("("+responseText+")");

		  $(element).action = 'includes/actions.php?do=updateNote';
		  
		  $('note').value = rqResult.note;
		  $('mins').value = rqResult.mins;
		  $('hour').value = rqResult.hour;
		  $('day').value = rqResult.day;
		  $('month').value = rqResult.month;

		}
	}).request();
	
	$('submit').innerHTML = '<input type=\"hidden\" id=\"goTo\" value=\"notes.php\" /> <input type=\"hidden\" name=\"noteID\" value=\"'+id+'\"/> <input type=\"image\" src=\"images/update.jpg\"/>';
	
	showElement(element, pixels);
	
	  $('note').value = '';
	  $('mins').value = 0;
	  $('hour').value = 0;
	  $('day').value = 0;
	  $('month').value = 0;
}

function editRecord(id, element, pixels){
	
	var zzz = new Ajax('includes/actions.php?do=listRecord&id='+id, {
		method: 'get',
		 onSuccess: function (responseText) {
		  var rqResult = eval("("+responseText+")");

		  $(element).action = 'includes/actions.php?do=updateRecord';
		  //$('goTo').value = 'records.php';
		  
		  $('recName').value = rqResult.name;
		  $('day').value = rqResult.day;
		  $('month').value = rqResult.month;
		  $('year').value = rqResult.year;
		}
	}).request();
	
	$('submit').innerHTML = '<input type=\"hidden\" id=\"goTo\" value=\"records.php\" /> <input type=\"hidden\" name=\"recID\" value=\"'+id+'\"/> <input type=\"image\" src=\"images/update.jpg\"/>';
	
	showElement(element, pixels);
	
    $('recName').value = '';
    $('day').value = 0;
    $('month').value = 0;
    $('year').value = 0;
}


function showElement(element, pixels){
	
	$(element).style.display = '';
	
 	var marginChange = new Fx.Style(element, 'margin-top', {duration:800});
    marginChange.start(pixels);
}

//Mootools ajaxFormSubmit
window.addEvent('domready', function (){
	$('dataForm').addEvent('submit', function(e) {
		/**
		 * Prevent the submit event
		 */
		new Event(e).stop();
	
		/**
		 * This empties the log and shows the spinning indicator
		 */
		
		$('status').innerHTML = "Зареждане...";

		//Check Inputs
		var allInputs = $('dataForm').getElementsByTagName("input");
		
		for(var i=0; i<allInputs.length; i++){
			if(allInputs[i].value == '' && allInputs[i].type != 'image'){
				asd++;
			}
		}
		
		//Check select
		var allSelects = $('dataForm').getElementsByTagName("select");
	
		for(var i=0; i<allSelects.length; i++){
			if(allSelects[i].value == 0){
				asd++;
			}
		}
		
		//Check textarea
		var allSelects = $('dataForm').getElementsByTagName("textarea");
	
		for(var i=0; i<allSelects.length; i++){
			if(allSelects[i].value == ''){
				asd++;
			}
		}
		
		//alert(asd);

		/**
		 * send takes care of encoding and returns the Ajax instance.
		 * onComplete removes the spinner from the log.
		 */
		if(asd == 0){
			
			this.send({
					  onComplete: function(responseText){
						  
						  var rqResult = eval("("+responseText+")");
						  
						  if(rqResult.res == "error"){
							  $('status').innerHTML = rqResult.resTXT;
						  }
						  if(rqResult.res == "done"){
							  var marginChange = new Fx.Style('dataForm', 'margin-top', {duration:800});
							  marginChange.start(-650);
							  document.location = $('goTo').value;
						  }
					  }
					 });
			
		}else{
			$('status').innerHTML = "Имате "+asd+" пропуснати полета!";
		}
		
		asd = 0;
	});
});