function validateEmail(emailAddress) {
   var match = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/.test(emailAddress);
   return match;
}

function isEmail(string) {
	string = trimAll(string);
if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
	window.location="admin.php?fonction=DisplaybyEmails&sendto="+string;
else{
	alert("Please enter a valid email address!");
	return false;
}

}

function isEmailcsv(string) {
	string = trimAll(string);
if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
	window.location="admin.php?fonction=DisplaybyEmailscsv&sendto="+string;
else{
	alert("Please enter a valid email address!");
	return false;
}

}
function changecontent(){
var today = new Date()
var month = today.getMonth() + 1
if (month<10) {
 month = "0"+month	
}
var day = today.getDate()
var year = today.getFullYear()
var s = "-"

window.document.detailedForm.lastCalledYear.value = year 
window.document.detailedForm.lastCalledMonth.value = month 
window.document.detailedForm.lastCalledDay.value = day 
}
function soumettreAreaCode(textfield) {
	//alert( "Please enter only numbers!" );
    var string = trimAll(textfield.value);
	if ((string != "") && (string!= null) && (string.length == 3 || string.length == 1)){
    	for (var i=0;i < string.length;i++)
        	if ((string.substring(i,i+1) < '0') || (string.substring(i,i+1) > '9')) {
		 		alert( "Please enter numbers only!" );
				textfield.focus();
				return false;
			}
	window.location="queries.php?fonction=DisplaybyAreaCode&code="+string;	
	}else alert( "Invalid Format!" ); 
	
}
function soumettreYearMonth(textfield){
	var year = trimAll(textfield.value)
	var month = window.document.formulaire.month.value
	var day = window.document.formulaire.day.value
	
	window.location="queries.php?fonction=DisplaybyYearMonth&year="+year+"&month="+month+"&day="+day;
	
}
function soumettreTelemarketer(){
	var uid = window.document.formulaire.telemarketername.value;
	var year = window.document.formulaire.adminyear.value;
	var month = window.document.formulaire.adminmonth.value;
	var day = window.document.formulaire.adminday.value;
	
	window.location="admin.php?fonction=DisplaybyTelemarketer&telemarketerid="+uid+"&year="+year+"&month="+month+"&day="+day;
	
}
function soumettreTelemarketerCode(){
	var code = window.document.formulaire.telemarketercode.value;
	var year = window.document.formulaire.codeyear.value;
	var month = window.document.formulaire.codemonth.value;
	var day = window.document.formulaire.codeday.value;
	
	window.location="admin.php?fonction=DisplaybyTelemarketerCode&code="+code+"&year="+year+"&month="+month+"&day="+day;
}
function trimAll(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
return sString;
}

function emails(form, rows){
	var input, mails = "";
	 
	for (var i = 0; i < rows; i++){
		input = "window.document.formemails.email"+i+".value";
		
		if (i != (rows-1)) {
			 mails = mails + eval(input)+";";		
		}else mails = mails + eval(input);
	}
	
	location="mailto:?bcc="+mails;
	
	return true;
}

function checkscript(){
	var d = new Date();
	var curr_year = d.getFullYear();
	var curr_month = d.getMonth();
	curr_month++;
	var curr_day = d.getDate();
	var curr_hour = d.getHours();
	var curr_min = d.getMinutes();
	var val = "";
		
	for (var i=0; i<window.document.detailedForm.code.length; i++)  { 
		if (window.document.detailedForm.code[i].checked)  {
			val = window.document.detailedForm.code[i].value;
		}
	}
	
	if (val == "") {
		alert("Select a code !");
		return false;
	}else if (val =="CBL"){
		var sel_hour = parseInt(window.document.detailedForm.hourCBL.value);
		var sel_min = parseInt(window.document.detailedForm.minutesCBL.value);
		//alert("val= "+val);
		if (sel_hour < curr_hour) {
			alert("Time has to be later today!"); 
			return false;
		}else if (sel_hour == curr_hour && sel_min <= curr_min) {
			alert("Time has to be later today!");
			return false;
		}
		
	}else if (val == "CB"){
		var sel_hour = window.document.detailedForm.hourCB.value;
		var sel_min = window.document.detailedForm.minutesCB.value;
		var sel_year = window.document.detailedForm.yearCB.value;
		
		if (sel_year<curr_year) {
			alert("Cannot be a previous year!");
			return false;
		}else if (sel_year==curr_year && sel_month< curr_month) {
			alert("Cannot be a previous month!");
			return false;
		}else if (sel_year==curr_year && sel_month == curr_month && sel_day<=curr_day) {
			alert("Cannot be a previous or cuurent day!");
			return false;
		}
		
		
	}else if (val == "WCB"){
		var sel_year = window.document.detailedForm.yearWCB.value;
		var sel_month = window.document.detailedForm.monthWCB.value;
		var sel_day = window.document.detailedForm.dayWCB.value;
		if (sel_year<curr_year) {
			alert("Cannot be a previous year!");
			return false;
		}else if (sel_year==curr_year && sel_month< curr_month) {
			alert("Cannot be a previous month!");
			return false;
		}else if (sel_year==curr_year && sel_month == curr_month && sel_day<curr_day) {
			alert("Cannot be a previous day!");
			return false;
		}
	}
	
	//alert(val + " "+curr_year +" "+ curr_month +" "+curr_day+ " " + curr_hour+" " + curr_min);
	return true;
}

function isBissextile(an) {  return  ((( an % 4 == 0 ) && ( an % 100 != 0 || an % 400 == 0 )) ? true : false ); }


function checkDate(an,mois,day){
	
	if(mois == 1 || mois == 3 || mois == 5 || mois == 7 || mois == 8 || mois == 10 || mois == 12){
		day.options[28]=new Option("29", "29", false, false);
		day.options[29]=new Option("30", "30", false, false);
		day.options[30]=new Option("31", "31", false, false);
		
	}else if (mois == 4 || mois == 6 || mois == 9 || mois == 11){
		day.options[28]=new Option("29", "29", false, false);
		day.options[29]=new Option("30", "30", false, false);
		day.options.length = 30;
		
	}else if (mois == 2 && isBissextile(an)){
		day.options[28]=new Option("29", "29", false, false);
		day.options.length = 29;
				
	}else day.options.length = 28;
		
	
}
