function check_admin_login()
{
        var flag=0;

                 if(document.admin_login.username.value=="")
                     {
                      change_text_info('div_username','Nu ai completat Username');
                      flag=1;
                     }
                 if(document.admin_login.password.value=="")
                     {
                      change_text_info('div_password','Nu ai completat Parola');
                      flag=1;
                     }

        if(flag==1) return false;
          document.admin_login.submit();
}

function isNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}


//Basket Operation
function editBasketItem(formId){
 	var cantitateNoua = prompt('Introduceti noua cantitate', ' ');	
	if(isNumeric(cantitateNoua)) {
		document.forms['basketOperation' + formId].cantitateNoua.value = cantitateNoua;
		document.forms['basketOperation' + formId].action = 'index.php?target=basket&action=edit&doEdit=do';
		document.forms['basketOperation' + formId].submit();
	}
	else
		alert('Noua cantitate trebuie sa fie numar intreg!');
 }

function deleteBasketItem(formId){
	var agree = confirm("Sunteti sigur ca doriti sa stergi acest produs");
	if (agree) {
 		document.forms['basketOperation' + formId].action = 'index.php?target=basket&action=delete';
	 	document.forms['basketOperation' + formId].submit();
	}
}


// Products management
function addProduct(){
 	document.location = 'index.php?target=loggedin&action=products&operation=add';
 }

function editProduct(formId){
 	document.forms['productOperation' + formId].action = 'index.php?target=loggedin&action=products&operation=edit';
	document.forms['productOperation' + formId].submit();
 }


function deleteProduct(formId){
	var agree = confirm("Are you sure you want to delete this product?");
	if (agree) {
 		document.forms['productOperation' + formId].action = 'index.php?target=loggedin&action=products&operation=delete';
	 	document.forms['productOperation' + formId].submit();
	}
}

function associateToSystem(formId){
 	document.forms['productOperation' + formId].action = 'index.php?target=loggedin&action=products&operation=addToSystem';
	document.forms['productOperation' + formId].submit();
}

function associateToDisease(formId){
 	document.forms['productOperation' + formId].action = 'index.php?target=loggedin&action=products&operation=addToDisease';
	document.forms['productOperation' + formId].submit();
}


function selectByDisease(object){
	var whoIsSelected = object.selectedIndex;
    var selectedValue = object.options[whoIsSelected].value;   
	document.location = 'index.php?target=products&dId='+selectedValue;
}

function selectByProduct(object) {
	var whoIsSelected = object.selectedIndex;
    var selectedValue = object.options[whoIsSelected].value;   	
	document.location = 'index.php?target=products&pId='+selectedValue;	
}

// used for the direction of links
function goDirection(direction, page,form){
	document.forms[form].action = 'index.php?target=loggedin&action='+direction+'&page='+page;
	document.forms[form].submit();
}

function goDirectionMain(direction, page,form){
	document.forms[form].action = 'index.php?target='+direction+'&page='+page;
	document.forms[form].submit();
}

function goDirectionMain2(direction, page,form){
	document.forms[form].action = 'index.php?'+direction+'&page='+page;
	document.forms[form].submit();
}

// Categories management
function addCategory(){
	document.location = 'index.php?target=loggedin&action=systems&operation=add';	
 }

function editCategory(formId){
 	document.forms['categoryOperation' + formId].action = 'index.php?target=loggedin&action=systems&operation=edit';
	document.forms['categoryOperation' + formId].submit();
 }

function deleteCategory(formId){
	var agree = confirm("Doresti sa stergi aceasta sistem?");
	if (agree) {
 		document.forms['categoryOperation' + formId].action = 'index.php?target=loggedin&action=systems&operation=delete';
	 	document.forms['categoryOperation' + formId].submit();
	}
}

// Disease management
function addDisease(){
	document.location = 'index.php?target=loggedin&action=diseases&operation=add';	
 }

function editDisease(formId){
 	document.forms['diseaseOperation' + formId].action = 'index.php?target=loggedin&action=diseases&operation=edit';
	document.forms['diseaseOperation' + formId].submit();
 }

function deleteDisease(formId){
	var agree = confirm("Doresti sa stergi aceasta boala?");
	if (agree) {
 		document.forms['diseaseOperation' + formId].action = 'index.php?target=loggedin&action=diseases&operation=delete';
	 	document.forms['diseaseOperation' + formId].submit();
	}
}

function showOrderDisease(formId){
 	document.forms['diseaseOperation' + formId].action = 'index.php?target=loggedin&action=diseases&operation=showOrder';
	document.forms['diseaseOperation' + formId].submit();
 }

function showOrderSystem(formId){
 	document.forms['categoryOperation' + formId].action = 'index.php?target=loggedin&action=systems&operation=showOrder';
	document.forms['categoryOperation' + formId].submit();
 }


var clockID = 0;

function UpdateClock(){ 
var dte = new Date();
var hrs = dte.getHours();
var min = dte.getMinutes(); 
var sec = dte.getSeconds();
var col = ":";
var dateSpacer = "/";
var spc = " ";
var day = dte.getDate();
var month = dte.getMonth();
var year = dte.getFullYear();
var apm;

month = month + 1;

	if (12 < hrs) { 
	apm="PM";
	hrs-=12;
	}
	else {
	apm="AM";
	}

	if (hrs == 0) hrs=12;
	if (min<=9) min="0"+min;
	if (month<=9) month="0"+month;	
	if (sec<=9) sec="0"+sec;

	document.getElementById('clock').innerHTML=hrs+col+min+col+sec+spc+apm+ '&nbsp;' + day+ dateSpacer + month + dateSpacer + year;
	
	clockID = setTimeout("UpdateClock()", 1000);
} 


function StartClock() {
   clockID = setTimeout("UpdateClock()", 500);
}

function KillClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }
}


function checkDate(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
	   DateValue = DateField;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   
   //alert('Day: '+ day +  ' month: ' + month + ' year: ' + year);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      //DateField.value = day + seperator + month + seperator + year;
	  return true;
   }
   /* Error-message if err != 0 */
   else {
     /* alert("Date is incorrect!");	  
      DateField.select();
	  DateField.focus();*/
	  return false;
   }
}

// Succes management
function addSucces(){
 	document.location = 'index.php?target=loggedin&action=succeses&operation=add';
 }

function editSucces(formId){
 	document.forms['succesOperation' + formId].action = 'index.php?target=loggedin&action=succeses&operation=edit';
	document.forms['succesOperation' + formId].submit();
 }

function deleteSucces(formId){
	var agree = confirm("Doresti sa stergi acest succes?");
	if (agree) {
 		document.forms['succesOperation' + formId].action = 'index.php?target=loggedin&action=succeses&operation=delete';
	 	document.forms['succesOperation' + formId].submit();
	}
}


function showProductPopup(idProdus, whatToShow) {
	window.open('template/showProductData.php?idProdus='+idProdus+'&whatToShow='+whatToShow,'mywindow','width=664,height=419,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no');	
}

function showImagePopup(imageName) {
	window.open('../template/admin/imagePopup.php?imgName='+imageName,'mywindow2','width=100,height=170,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no');	
}

function editOption(formId){
 	document.forms['optionOperation' + formId].action = 'index.php?target=loggedin&action=options&operation=edit';
	document.forms['optionOperation' + formId].submit();
}


