// JavaScript Document

function IsNumeric(fieldname)
{
		if(isNaN(fieldname.value))
		   {
			 alert("Invalid data format.\n\nOnly numbers are allowed.");
			 fieldname.value="";
			 fieldname.focus();
			 return (false);
		   }
}

function checkEmail(email){
	checkedEmail=email.value;
 	invalidEmail=0;
  	invalidChars = " /:,;><?+="
	//is the field empty
	if (checkedEmail=="") {
	invalidEmail=1;	
	}
	//does the field contain invalid characters
	for (i=0;i<invalidChars.length;i++) {
		badChar=invalidChars.charAt(i)
		if (checkedEmail.indexOf(badChar,0)>-1) {
			invalidEmail=1;
		}
	}
	//is there @ in the email
	atPos=checkedEmail.indexOf("@",1)
	if (atPos==-1) {
		invalidEmail=1;	
	}
	//is there a second @ in the field
	if (checkedEmail.indexOf("@",atPos+1)>0) {
		invalidEmail=1;
	}
	//is there a second a "." after the @
	dotPos=checkedEmail.indexOf(".",atPos+1)
	if (dotPos==-1) {
		invalidEmail=1;
	}
	
	if (dotPos+3 > checkedEmail.length){
		invalidEmail=1}

	if (invalidEmail!= 0){
		alert("You must enter a valid e-mail address. Please, go back and try again.Thank you!"); 
		email.value=""; 
		email.focus();
		return false
	}
return true
}

function ValidDate (currdate, toAlert) {
veryTmp=currdate
var tmpDate = currdate.split("/");
	if ((tmpDate.length < 3 && currdate !="") || tmpDate.length > 3) {
	currdate = "";}
	else if (isNaN(tmpDate[0]) || isNaN(tmpDate[1]) || isNaN(tmpDate[2])) {
		currdate = "";}
	else { 
		tmpDate[0] = GetNumFromStr (tmpDate[0])
		tmpDate[1] = GetNumFromStr (tmpDate[1])
		
		if (!(parseInt(tmpDate[1]) >0 && parseInt(tmpDate[1])<13 && parseInt(tmpDate[0]) >0 && parseInt(tmpDate[0]) <32 && 	parseInt(tmpDate[2])>1900 && parseInt(tmpDate[2])<2050 && ValidDateForMonth (tmpDate[0], tmpDate[1],tmpDate[2]))) {
			currdate = "";
		}
		else {			
			var r1,r2;
			r1=""; r2=""
			if (parseInt(tmpDate[0]) < 10) {r1 = "0";}
			if (parseInt(tmpDate[1]) < 10) {r2 = "0";}
			currdate = r1+parseInt(tmpDate[0]) + "/" + r2 + parseInt(tmpDate[1]) + "/" + parseInt(tmpDate[2])
			var f=ValidDateForMonth(parseInt(tmpDate[0]),parseInt(tmpDate[1]),parseInt(tmpDate[2]))		
		}
	}
	if (currdate == "" && toAlert) {alert("You must enter a valid date format: DD/MM/YYYY. Please, go back and try again. Thank you!")}
return currdate;
}
function GetNumFromStr (str) {
	var pos=str.indexOf("0")
	if (pos == 0 ) {str = str.substring(1,str.length);}
return str;
}

function ValidDateForMonth (dayIs, monthIs,yearIs) {
var mDays= new Array (31,28,31,30,31,30,31,31,30,31,30,31)
if (parseInt(yearIs) % 400 == 0 ) {mDays[1]=29;}
if (parseInt(yearIs) % 4 == 0 && parseInt(yearIs) % 100 != 0) {mDays[1]=29;}

if (parseInt(dayIs)>mDays[parseInt(monthIs)-1]) {return false;}
else {return true;}
}

function checkMandatory(form_fields) {
for(i = 0; i < form_fields.elements.length; i++)
 {
  atPos = form_fields.elements[i].name.indexOf("__",1)
  if(atPos != -1)
   {
    if(form_fields.elements[i].value=="")	
   {
	alert("Please fill in all Mandatory fields. Thank you!"); return false}
   }
 }
return true
}
