// ------------------------------------------------------------------
// Author:	
//		Tomi Ilvonen, ??/??/2006
//          
// Updates:	
//		11.06.2007: Eriytetty omaksi .js tiedostoksi. -MV
// ------------------------------------------------------------------

function CheckPhoneNum(strNum) 
{
	var strAllowed = "() -";
	var strStripped = "";
	var x = 0;
	var i;
	var blnInternational = false;
	if (strNum.charAt(0) == "+") { x = 1; blnInternational = true; }
	for (i = x; i < strNum.length; i++) {
		if (strAllowed.indexOf(strNum.charAt(i)) == -1) {
			strStripped += strNum.charAt(i);
		}
	}
	if ((blnInternational && strStripped.length < 10) || (!blnInternational && strStripped.length < 7) || strStripped.length > 14 || isNaN(strStripped) || (!blnInternational && strStripped.charAt(0) != "0")) {
		return false;
	} else {
		return true;
	}
}

function CheckEmail(strAddress) 
{
	return strAddress.match(/^[\w\.-]+@[\w\.-]+\.[a-zA-Z0-9]+$/);
}

function CheckForm()
{
	var blnCanSubmit = true;
	var theForm = document.JobReqForm;
	var validExt = ".doc;.docx;.pptx;.xlsx;.ppt;.pps;.xls;.rtf;.txt;.pdf;.jpg;.jpeg;.bmp;.tif;.gif;;"
	var validPic = ".jpg;.jpeg;.bmp;.tif;.gif;;"

	//vaaditut kentät
	if(theForm.strLastname.value == "")
	{
		alert("Sukunimi on pakollinen tieto.");
		theForm.strLastname.focus();
		blnCanSubmit = false;
		
	}
	else if(theForm.strFirstname.value == "")
	{
		alert("Etunimi on pakollinen tieto.");
		theForm.strFirstname.focus();
		blnCanSubmit = false;
		
	}
	else if(theForm.strStreetAddress.value == "")
	{
		alert("Katuosoite on pakollinen tieto.");
		theForm.strStreetAddress.focus();
		blnCanSubmit = false;
	}
	else if(theForm.strStreetNumber.value == "")
	{
		alert("Katunumero on pakollinen tieto.");
		theForm.strStreetNumber.focus();
		blnCanSubmit = false;
	}
	else if(theForm.strPostnumber.value == "" || isNaN(theForm.strPostnumber.value))
	{
		alert("Postinumero on pakollinen tieto.");
		theForm.strPostnumber.focus();
		blnCanSubmit = false;
		
	}
	else if(theForm.strPostarea.value == "")
	{
		alert("Postitoimipaikka on pakollinen tieto.");
		theForm.strPostarea.focus();
		blnCanSubmit = false;
		
	}
	else if(isDate(theForm.intBirthDateYear.value+'-'+theForm.intBirthDateMonth.value+'-'+theForm.intBirthDateDay.value,"y-M-d") == 0)
	{
		alert("Syntymäaika ei ole oikea päivämäärä!");
		theForm.intBirthDateDay.focus();
		blnCanSubmit = false;
	}
	else if(theForm.strMobile.value == "")
	{
		alert("Matkapuhelinnumero on pakollinen tieto.");
		theForm.strMobile.focus();
		blnCanSubmit = false;
		
	}
	else if(!CheckPhoneNum(theForm.strMobile.value)) {
		alert("Matkapuhelinnumero on virheellinen.");
		theForm.strMobile.focus();
		blnCanSubmit = false;
	}
	else if(isDate(theForm.intFirstWorkDayYear.value+'-'+theForm.intFirstWorkDayMonth.value+'-'+theForm.intFirstWorkDayDay.value,"y-M-d") == 0)
	{
		alert("Töiden aloitusajankohta ei ole oikea päivämäärä!");
		theForm.intFirstWorkDayDay.focus();
		blnCanSubmit = false;
	}

	// vapaaehtoiset kentät
	if(blnCanSubmit && theForm.strPhone.value != "") {
		if(!CheckPhoneNum(theForm.strPhone.value)) {
			alert("Puhelinnumero on virheellinen.");
			theForm.strPhone.focus();
			blnCanSubmit = false;
		}
	}
	if(blnCanSubmit && theForm.strEmail.value != "") {
		if(!CheckEmail(theForm.strEmail.value)) {
			alert("Sähköpostiosoite on virheellinen.");
			theForm.strEmail.focus();
			blnCanSubmit = false;
		}
	}
	if(blnCanSubmit && theForm.strWageReq.value != "") {
		if(isNaN(theForm.strWageReq.value)) {
			alert("Palkkatoive on virheellinen.");
			theForm.strWageReq.focus();
			blnCanSubmit = false;
		}
	}
	if(blnCanSubmit && theForm.intLastWorkDayYear.value+theForm.intLastWorkDayMonth.value+theForm.intLastWorkDayDay.value != "") {
		// alert(theForm.intLastWorkDayYear.value+theForm.intLastWorkDayMonth.value+theForm.intLastWorkDayDay.value);
		if(isDate(theForm.intLastWorkDayYear.value+'-'+theForm.intLastWorkDayMonth.value+'-'+theForm.intLastWorkDayDay.value,"y-M-d") == 0) {
			alert("Töiden lopetusajankohta ei ole oikea päivämäärä.");
			theForm.intLastWorkDayDay.focus();
			blnCanSubmit = false;
		}
	}
	//Tiedostot
	if(blnCanSubmit && theForm.file1.value.length > 0) {
		curExt = "."+theForm.file1.value.split(".")[theForm.file1.value.split(".").length - 1]+";"
		if (validExt.indexOf(curExt.toLowerCase()) == -1) {
			alert("Tiedosto "+theForm.file1.value.split("\\")[theForm.file1.value.split("\\").length - 1]+" ei ole sallittu.\n\nSallitut muodot ovat:\n*"+validExt.replace(/;;/,"").replace(/;/ig,", *"));
			theForm.file1.focus();
			blnCanSubmit = false;
		}
		if (blnCanSubmit && (theForm.file1_type.value == 3 || theForm.file1_type.value == 4) && (validPic.indexOf(curExt.toLowerCase()) == -1)) {
			alert("Tiedosto "+theForm.file1.value.split("\\")[theForm.file1.value.split("\\").length - 1]+" ei ole kuvatiedosto.\n\nSallitut muodot ovat:\n*"+validPic.replace(/;;/,"").replace(/;/ig,", *"));
			theForm.file1.focus();
			blnCanSubmit = false;
		}
	}
	if(blnCanSubmit && theForm.file2.value.length > 0) {
		curExt = "."+theForm.file2.value.split(".")[theForm.file2.value.split(".").length - 1]+";"
		if (validExt.indexOf(curExt.toLowerCase()) == -1) {
			alert("Tiedosto "+theForm.file2.value.split("\\")[theForm.file2.value.split("\\").length - 1]+" ei ole sallittu.\n\nSallitut muodot ovat:\n*"+validExt.replace(/;;/,"").replace(/;/ig,", *"));
			theForm.file2.focus();
			blnCanSubmit = false;
		}
		if (blnCanSubmit && (theForm.file2_type.value == 3 || theForm.file2_type.value == 4) && (validPic.indexOf(curExt.toLowerCase()) == -1)) {
			alert("Tiedosto "+theForm.file2.value.split("\\")[theForm.file2.value.split("\\").length - 1]+" ei ole kuvatiedosto.\n\nSallitut muodot ovat:\n*"+validPic.replace(/;;/,"").replace(/;/ig,", *"));
			theForm.file2.focus();
			blnCanSubmit = false;
		}
	}
	if(blnCanSubmit && theForm.file3.value.length > 0) {
		curExt = "."+theForm.file3.value.split(".")[theForm.file3.value.split(".").length - 1]+";"
		if (validExt.indexOf(curExt.toLowerCase()) == -1) {
			alert("Tiedosto "+theForm.file3.value.split("\\")[theForm.file3.value.split("\\").length - 1]+" ei ole sallittu.\n\nSallitut muodot ovat:\n*"+validExt.replace(/;;/,"").replace(/;/ig,", *"));
			theForm.file3.focus();
			blnCanSubmit = false;
		}
		if (blnCanSubmit && (theForm.file3_type.value == 3 || theForm.file3_type.value == 4) && (validPic.indexOf(curExt.toLowerCase()) == -1)) {
			alert("Tiedosto "+theForm.file3.value.split("\\")[theForm.file3.value.split("\\").length - 1]+" ei ole kuvatiedosto.\n\nSallitut muodot ovat:\n*"+validPic.replace(/;;/,"").replace(/;/ig,", *"));
			theForm.file3.focus();
			blnCanSubmit = false;
		}
	}
	// Lähetys jos kaikki ok
	if(blnCanSubmit)
	{
		theForm.sbutton.disabled = true;
		theForm.sbutton.value = "Odota...";
		theForm.submit();
	} 
}

function LimitTextarea(intMax, objElement)
{
	if (objElement.value.length > intMax) 
	{
		objElement.value = objElement.value.slice(0, intMax);
	}
}
