function checkFields(formName) { // check if all required fields are filled
         var theForm = document.getElementById(formName);
         var rx = new RegExp('[^_]+__[^_]+');
         for (var i = 0; i < theForm.elements.length; i++) {
             if (theForm.elements[i].name)
                if (theForm.elements[i].name.match(rx)) {
                	var error = false;
                   if ((theForm.elements[i].name) && (theForm.elements[i].value == undefined))
                      error = true;
                   else
                   if ((theForm.elements[i].name) && (theForm.elements[i].value == ''))
                      error = true;
                   else
                   if ((theForm.elements[i].options) && (theForm.elements[i].options[theForm.elements[i].selectedIndex].value == 'choose'))
                      error = true;
                   if (error) {
                      alert('Va rugam completati: ' + theForm.elements[i].title);
                      theForm.elements[i].focus();
                      return false;
                      }
                   }
             }
         return true;
         }

function checkEmail(field) {
         if (!document.getElementById(field)) return 1;
         var rx = /^[\w][-\w._]*[@][\w][-\w._]*[.][\w][\w]{1,4}$/i;
         var e = document.getElementById(field);
         var ok = rx.test(e.value);
         if (!ok) {
            alert('Adresa email invalida');
            e.focus();
            }
         return ok;
       }

function singleCheck(checkedName,formName) {

	var wasChecked = document.getElementById(checkedName).checked;

         var rx = new RegExp('checked_[^*]+');
         var theForm;
         if (formName==undefined) formName = 'listview';

             theForm = document.forms[formName];
             for (var j = 0; j < theForm.elements.length; j++) {
                 if (theForm.elements[j].name)
                    if (theForm.elements[j].name.match(rx)) {
                       theForm.elements[j].checked = false;
                       }
                 }

	 if(wasChecked) 
	 	document.getElementById(checkedName).checked=true;
	 else 
	 	document.getElementById(checkedName).checked=false;
        
        return true;


}
       
function checkAll(op,formName,pattern) { // (un)check all checkboxes in a list view

        if (pattern==undefined) pattern ='checked_';
         var rx = new RegExp(pattern+'([^*]+)');
         var theForm;
         if (formName==undefined) formName = 'listview';

             theForm = document.forms[formName];

             for (var j = 0; j < theForm.elements.length; j++) {

                 if (theForm.elements[j].name)
                    if (theForm.elements[j].name.match(rx)) {

                       theForm.elements[j].checked = op;
                       }
                 }

         return true;
         }         
         
function checkList(formName,pattern) { // create a list of checked values from a listview

        if (pattern==undefined) pattern ='checked_';
         var rx = new RegExp(pattern+'([^*]+)');
         var theForm;
         var retString='';
         if (formName==undefined) formName = 'listview';
             theForm = document.forms[formName];
             for (var j = 0; j < theForm.elements.length; j++) {
                 if (theForm.elements[j].name){
                   var result = theForm.elements[j].name.match(rx);
                    if ((result)&&(theForm.elements[j].checked)) {
                       retString=retString + "'" + result[1] + "',";
                    }
                 }
               }
         return retString;
       }


function clearForm(formName) {
         var theForm = document.getElementById(formName);

         var rx = new RegExp('field_[_]?(.*)');
         var rx2 = new RegExp('form_[_]?(.*)');
         var rx3 = new RegExp('search_([^_].*)');
         var rx4 = new RegExp('skip');
         
         for (var i = 0; i < theForm.elements.length; i++) {

            if (((theForm.elements[i].name.match(rx))||(theForm.elements[i].name.match(rx2))||(theForm.elements[i].name.match(rx3)))&&(!theForm.elements[i].name.match(rx4))){
             if (theForm.elements[i].type=='checkbox')
                theForm.elements[i].checked = false;
             if ((theForm.elements[i].type=='text')||(theForm.elements[i].type=='textarea'))
                theForm.elements[i].value = '';
             if ((theForm.elements[i].type=='password'))
                theForm.elements[i].value = '';
             if (theForm.elements[i].type=='select-one')
                theForm.elements[i].selectedIndex = 0;
             //if (theForm.elements[i].type=='hidden')
             //   theForm.elements[i].value = '';

             }
         }
      return false;
    }

function orderBy(formName,fieldName,action){

    document.forms[formName].action = action;
    document.forms[formName].head_order_by.value=fieldName;
    document.forms[formName].submit();

}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= ".";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd.mm.yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function filterDigits(evt) {

     var c =(evt.keyCode||evt.which);
         return ((c >= 96 && c <= 105)||(c >= 48 && c <= 57)||(c==37)||(c==39)||(c==8)||(c==46)||(c==190)||(c==9)||(c==109)||(c==189)||(c==110));
         }

function filterQDigits(evt) {

     var c =(evt.keyCode||evt.which);
         return (c==37)||(c==39)||(c==8)||(c==46)||(c==190)||(c==9)||(c==109)||(c==189)||(c==110)||(c==70)||(c==102)||(c==66)||(c==98)||(c==69)||(c==101)||(c==73)||(c==105)||(c==83)||(c==115);
         }

function filterQGrades(val){

    if((val=='FB')||(val=='B')||(val=='S')||(val=='E')||(val=='I')||(val=='fb')||(val=='b')||(val=='s')||(val=='e')||(val=='i')) return true; else {alert('Introduceti va rugam o valoare valida: I,S,B,FB,E'); return false;}

}

function filterGrades(val){

    if((parseFloat(val)>=1)&&(parseFloat(val)<=10)) return true; else {alert('Introduceti va rugam o valoare valida intre 1 si 10'); return false;}

}

function filterPhone(evt) {

     var c =(evt.keyCode||evt.which);
         return ((c >= 96 && c <= 105)||(c >= 48 && c <= 57)||(c==37)||(c==39)||(c==32)||(c==8)||(c==46)||(c==9)||(c==187)||(c==107)||(c==61)||(c==189)||(c==110));
         }


