/**
 * Application : Mosaic
 * File        : fe/_js/form.js
 * @version    : 0.1
 * @copyright  : copyright 07.03.2009 by http://www.getunik.com
 * 
 * Description:
 * Checks the entered data of the form which allows user to participate the mosaic.
 * 
 * @param   HTML Form Object  the form within the html page which provides the formfields whcih must be checked, a.e.: document.myform
 * @return                    on success it returns 0, otherwise the error code which is > then 0.
 */


function checkFeForm(oForm)
{
	bProcStat = true;
	iErrorId  = 0;
	
	
	/**
	 * fMoImgSrcId
	 * check if hidden form field fMoImgSrcId is set with an id
	 */
	if(bProcStat)
	{
		if(oForm.fMoImgSrcId)
		{
			// check > 0 and emptiness
			if(trim(oForm.fMoImgSrcId.value) != "")
			{
				bProcStat = true;
			}
			else
			{
				bProcStat = false;
				iErrorId  = 1;
			}
		}
	}
	
	
	/**
	 * fMoLangId
	 * check if hidden form field fMoLangId is set with an id
	 */
	if(bProcStat)
	{
		if(oForm.fMoLangId)
		{
			// check > 0 and emptiness
			if(trim(oForm.fMoLangId.value) != "")
			{
				bProcStat = true;
			}
			else
			{
				bProcStat = false;
				iErrorId  = 2;
			}
		}
	}
	
	
	/**
	 * fMoUserFirstname
	 * check if firstname is set
	 */
	if(bProcStat)
	{
		if(oForm.fMoUserFirstname)
		{
			// check emptiness
			if(trim(oForm.fMoUserFirstname.value) != "")
			{
				bProcStat = true;
			}
			else
			{
				sf("fMoFirstname");
				bProcStat = false;
				iErrorId  = 3;
			}
		}
	}
	
	
	/**
	 * fMoUserLastname
	 * check if lastname is set
	 */
	if(bProcStat)
	{
		if(oForm.fMoUserLastname)
		{
			// check emptiness
			if(trim(oForm.fMoUserLastname.value) != "")
			{
				bProcStat = true;
			}
			else
			{
				sf("fMoLastname");
				bProcStat = false;
				iErrorId  = 4;
			}
		}
	}
	
	
	/**
	 * fMoUserEmail
	 * check if e-mail address is set
	 */
	if(bProcStat)
	{
		if(oForm.fMoUserEmail)
		{
			// check emptiness
			if(trim(oForm.fMoUserEmail.value) == "")
			{
				sf("fMoUserEmail");
				bProcStat = false;
				iErrorId  = 6;
			}
			else
			{
				// check by RegExp if format is correct
				if(bProcStat)
				{
					var oRegExp = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$/;
					if (oRegExp.test(oForm.fMoUserEmail.value))
					{
						bProcStat = true;
					}
					else
					{
						sf("fMoUserEmail");
						bProcStat = false;
						iErrorId  = 5;
					}
				}
			}
		}
	}
	
	
	/**
	 * fMoImage
	 * check if image is selected and if file format is allowed
	 * 
	 */
	if(bProcStat)
	{
		if(oForm.fMoImage)
		{
			// check emptiness
			
			if(trim(oForm.fMoImage.value) == "")
			{
				sf("fMoImage");
				bProcStat = false;
				iErrorId  = 7;
			}
			/**
			// check spaces
			if(bProcStat)
			{
				sPathDelimiter = "/";
				sFilePath      = trim(oForm.fMoImage.value);
				sFileName      = "";
				if(sFilePath.indexOf("/") != -1)
				{
					sPathDelimiter = "/";
				}
				if(sFilePath.indexOf("\\") != -1)
				{
					sPathDelimiter = "\\";
				}
				sFileName = sFilePath.substr(sFilePath.lastIndexOf(sPathDelimiter) + 1, sFilePath.length);
				
				if(sFileName.indexOf(" ") >= 0)
				{
					sf("fMoImage");
					bProcStat = false;
					iErrorId  = 8;
				}
			}
			*/
			
			/* check file ending
			 * 
			 * IMPORTANT:
			 * Win32 only because MAC suppress sometimes file ending.
			 * because of that, serverside file format check is implicitly needed!
			 */
			if(bProcStat && navigator.platform.charAt('Win'))
			{
				// get the file ending
				aFile       = oForm.fMoImage.value.split(".");
				sFileEnding = aFile[aFile.length - 1].toLowerCase();
				bFileEnding = false;
				// loops trough all allowed file endings
				for(i=0; i<aAvailableFiles.length; i++)
				{
					if(sFileEnding == aAvailableFiles[i].toLowerCase())
					{
						bFileEnding = true;
						break;
					}
					else
					{
						bFileEnding = false;
					}
				}
				if(!bFileEnding)
				{
					sf("fMoImage");
					bProcStat = false;
					iErrorId  = 10; // was original error code 9
				}
			}
		}
	}
	
	/**
	 * fMoTerms
	 * check if user checked rules
	 */
	if(bProcStat)
	{
		if(oForm.fMoTerms)
		{
			// is checked?
			if(oForm.fMoTerms.checked)
			{
				bProcStat = true;
			}
			else
			{
				sf("fMoTerms");
				bProcStat = false;
				iErrorId  = 15;
			}
		}
	}
	
	return iErrorId;
	
}