/*
Mihai CLEJA <mihaicleja@ucs.ro>
01/04/2005

primeste ca parametru un obiect de tip Date()
intoarce urmatoarea zi calendaristica, in formatul yyyy-mm-dd
*/

var regExp = new Object();
regExp.numar = /^-?((([1-9][0-9]*)(\.[0-9]+)?)|(0\.[0-9]+)|0)$/;

function clearFormatting (str) {
	var temp = '';
	var i = 0;
	
	while (str.charAt(i)) {
		if (str.charAt(i) == '.') {
  			temp += '';
  		}
 		else if (str.charAt(i) == ',') {
 			temp += '.';
 		}
 		else {
 			temp += str.charAt(i);
 		}
 		
		i++;
	}	
	
	return temp;
}

function getNextDay (currentDate) {
    var day = currentDate.getDate();
    var month = currentDate.getMonth() + 1;
    var year = currentDate.getYear();

    //Firefox hack
    var browserName = navigator.appName; 
    if (browserName == "Netscape") {
    	year += 1900;
    }
		
    var next_day = day;
    var next_month = month;
    var next_year = year;
    
    /*
    lunile cu 31 de zile:
    1  Ianuarie
    3  Martie
    5  Mai
    7  Iulie
    8  August
    10 Octombrie
    */
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10) {
        if (day == 31) {
            next_day = 1;
            next_month = month + 1;
        }
        else {
            next_day = day + 1;
        }
    }

    /*
    lunile cu 30 de zile:
    4  Aprilie
    6  Iunie
    9  Septembrie
    11 Noiembrie
    */
    if (month == 4 || month == 6 || month == 9 || month == 11) {
        if (day == 30) {
            next_day = 1;
            next_month = month + 1;
        }
        else {
            next_day = day + 1;
        }
    }

    /*
    caz special:
    Decembrie
    */
    if (month == 12) {
        if (day == 31) {
            next_day = 1;
            next_month = 1;
            next_year = year + 1;
        }
        else {
            next_day = day + 1;
        }
    }

    /*
    caz special:
    Februarie
    */
    if (month == 2) {
        if ((day == 29) && ((year%4 == 0 && year%100 != 0) || (year%400 == 0)) ) {
            next_day = 1;
            next_month = month + 1;
        }
        else if (day == 28) {
            next_day = 1;
            next_month = month + 1;
        }
        else {
            next_day = day + 1;
        }
    }

    return next_year + "-" + next_month + "-" + next_day;
}

/* **************************************************
Mihai CLEJA <mihaicleja@ucs.ro>
5/19/2005
functia care formateaza o valoare numerica 
ex: primeste 1000000.75 si intoarce 1.000.000,75
************************************************** */
function formatNumber (str) {
	var temp = '';
	var isSigned = false;
	var commaPos = 0;
	var i = 0;
	var j = 0;
	var r=0;
	//madalin
	//modificare  cu verificare daca este numar sau nu
	
	if (str == '') {
				alert ("Nu ati completat campul ");
				return r;
				}
			else if (str != '') {
				var aux = clearFormatting(str);
				var filter = regExp['numar'];
				if (!(filter.test(aux))) {
					alert("Campul nu a fost completat corect.");
				return r;	
				}
		       else {
	
	
	//verific daca este numar negativ
	if (str.charAt(0) == '-') {
		isSigned = true;
	}
	
	//parsez numarul si elimin semnul minus si punctele 
	i = 0;
	while (str.charAt(i)) {
		if (str.charAt(i) == '.' || str.charAt(i) == "-") {
  			temp += '';
  		}
 		else {
 			temp += str.charAt(i);
 		}
		i++;
	}


	//daca valoarea obtinuta este un numar cu virgula, aflu pozitia virgulei
	//initial setez pozitia virgulei la valoarea temp.length, adica dupa sfarsitul sirului
	i = 0;
	commaPos = temp.length;
	while (temp.charAt(i)) {
		if (temp.charAt(i) == ',') {
			commaPos = i;
		}
		i++;
	}

	/* ********************************************************************
	
                                           _____ temp.length - 1	
                                          |
										  V
										  
    temp    | 1 | 0 | 0 | 0 | 0 | , | 9 | 9 | 
			  
              0   1   2           A
                                  |
                                  |___ commaPos
                                  
								  
	******************************************************************** */

	//parcurg partea intreaga a numarului de la sfarsit la inceput (adica de la pozitia commaPos - 1)
	//copiez cifra cu cifra in variabila str; dupa fiecare trei cifre adaug un punct
	str = '';
	for (i = commaPos - 1; i >= 0; i--) {
		str = temp.charAt(i) + str;
		j++;
		
		if ( (j%3 == 0) && (i > 0) ) {
			str = '.' + str;
		}	
	}
	
	//am terminat cu partea intreaga, lipesc si zecimalele	
	for (i = commaPos; i < temp.length ; i++) {
		str += temp.charAt(i);
	}
	
	//daca este valoare negativa, adaug in fata si semnul minus
	if (isSigned) {
		str = "-" + str;
	}

	return str;
	
	 }
	 }
}


