var globalvar;
var numberRe = /^(((\d{1,3})(.\d{3})*)|(\d+))(,\d+)?$/;
var dateRe = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
var mailRe = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}) {1,2}$/;

function regExpValid(fldValue, regExp) {
	if(fldValue) {
		return (fldValue.search(regExp) != -1);
	}
	
	return true;
}

function isValidNumber(fldValue) {
	return regExpValid(fldValue, numberRe);
}

/**
 * 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 normalizeAndCheckDate(oDt){
	normalizeDate(oDt);
	var iNum = Math.round((cal_prs_date1(oDt.value) - cal_prs_date1(sDtToday)) / 86400000)
	if(iNum < 0)
	{
			return false
	}
}

function normalizeAndSetDateTo(oDt,oDtTo){
	normalizeDate(oDt);
	
	sDt = oDt.value;
	if(sDt != '')
	{
		var iPos1=sDt.indexOf(dtCh)
		var iPos2=sDt.indexOf(dtCh,iPos1+1)
		
		var sDay=sDt.substring(0,iPos1)
		var sMonth=sDt.substring(iPos1+1,iPos2)
		var sYear=sDt.substring(iPos2+1)
	
		var oDtToNew = new Date();
		//alert(oDtToNew.getMonth() + '-' + sMonth)
		
		var sNewDay = 1 
		var sNewMonth = 0
		var sNewYear = parseInt(sYear,10) + 1
	
		if(!(sDay == '31' && sMonth == '12'))
		{
			oDtToNew.setDate(sDay);
			oDtToNew.setMonth(sMonth - 1);
			oDtToNew.setYear(sYear);
	
			oDtToNew.setDate(oDtToNew.getDate()+1);
			sNewDay = oDtToNew.getDate();
			sNewMonth = oDtToNew.getMonth() +1;
			sNewYear = oDtToNew.getFullYear();
		}	
		oDtTo.value= sNewDay + dtCh + sNewMonth + dtCh + sNewYear;
		normalizeDate(oDtTo)
	}	
	
}
function normalizeDate(oDt){
		
	var sDt = oDt.value
	if (sDt != '') {
		var iPos1=sDt.indexOf(dtCh)
		var iPos2=sDt.indexOf(dtCh,iPos1+1)
		
		var sDay=sDt.substring(0,iPos1)
		var sMonth=sDt.substring(iPos1+1,iPos2)
		var sYear=sDt.substring(iPos2+1)
		
		//alert(sDay + '-' + sMonth + '-' + sYear + 'len' +sDay.length) 
		
		var iDay=parseInt(sDay, 10)
		var iMonth=parseInt(sMonth, 10)
		var iYear=parseInt(sYear, 10)
		
		if(iDay == 0) sDay = "1";
		if(iMonth == 0) sMonth = "1";
		if(iYear == 0) sYear = "7";
		
		if(sDay.length == 1) sDay= '0' + sDay
		if(sMonth.length == 1) sMonth= '0' + sMonth
		if(sYear.length == 1) sYear= '200' + sYear
		
		oDt.value = sDay + dtCh + sMonth + dtCh + sYear
	}
}	

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 isValidDate(dtStr){
	
	//dtStr = oElemDate.value
	//alert(oElemDate + ' - ' + dtStr) 
	var iNum = Math.round((cal_prs_date1(dtStr) - cal_prs_date1(sDtToday)) / 86400000)
	if(iNum < 0)
	{
			return false
	}
	
	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, 10)
	day=parseInt(strDay, 10)
	year=parseInt(strYr, 10)
	if (pos1==-1 || pos2==-1){
		//-- alert("The date format should be : mm/dd/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 isValidDate(fldValue) {
		
	var ret = false;
	var now = new Date();
	return true;
	// aggiungere la condizione (vd.getTime() < nowPlus3M.getTime()) se interessa un limite di mesi 
	//var nowPlus3M = new Date();
        //nowPlus3M.setMonth(nowPlus3M.getMonth() + 3);
	if(fldValue) {
		if(regExpValid(fldValue, dateRe)) {
			var sd = fldValue.split('/');
			if(sd.length == 3) {
				var vd = new Date();
				vd.setDate(sd[0]);
				vd.setMonth(sd[1] - 1);
				vd.setYear(sd[2]);
				vd.setHours(0);
				vd.setMinutes(0);
				vd.setSeconds(0);
				ret = ((sd[0] == vd.getDate()) && ((sd[1] - 1) == vd.getMonth()) && (sd[2] == vd.getFullYear())  
                                      //&& (vd.getTime() > now.getTime())
                                       );                                
			}
		}
	}
	else {
		ret = true;
	}
	
	return ret;
}
*/

function isEmail(fldValue)
{
	/**var apos = fldValue.indexOf("@");
	var dotpos = fldValue.lastIndexOf(".");
	if ((apos < 1) || (dotpos - apos < 2)) {
		return false;
	}
	else {
		return true;
	}**/
	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(fldValue));
}

function isValidPhone(element) {
	return (element.value.length==0 || (/^\+?[\d\s-\/\.]{3,}$/.test(element.value)));
}

function getSelectedValue(selectObj) {
	return selectObj.options[selectObj.selectedIndex].value;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
