$(document).ready(function(){
function checkEmail(s)
	{
	var f=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return f.test(s);
	}
function checkUrl(s)
	{
    var v = new RegExp();
	v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    return v.test(s);
	} 
function checkTel(s)
	{
	var ValidChars = "0123456789+(). ";
	var Char;
	for (i = 0; i < s.length; i++) 
		{ 
		Char = s.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			{
			return false;
			}
		}
	return true;
	}
function trim(s)
	{
	ns = s.replace(/&/g, "");
	nns = ns.replace(/=/g, "");
	return nns.replace(/^\s+|\s+$/g,"");
	}

$('#sub_btn').click(function(){
	numErrors=0;
    type="";
	app="";
	$('.sub_frm .unsuccess').remove();
	$('.sub_frm textarea').each(function(i)
		{
		errorDetected=0;
		valType = $(this).attr('validation');
		valInput = trim($(this).val());
		if(valInput.length==0)
			{
			errorDetected=1;
			numErrors=numErrors+1;
			}
		if(errorDetected==0)
			$('#eR_'+$(this).attr('name')).remove();
				else
					$(this).before('<span class="unsuccess">'+$(this).attr('errormessage')+'<br></span>');
		
		});
	$('.sub_frm select').each(function(i)
		{
		errorDetected=0;
		nullValue = $(this).attr('nullvalue');
		valInput = trim($(this).val());
		
		if ($(this).attr('name') == 'type')
		{
		    type = trim($(this).val());
		}
		
		if(valInput.length==0 || valInput==nullValue)
			{
			errorDetected=1;
			numErrors=numErrors+1;
			}
		if(errorDetected==0)
			$('#eR_'+$(this).attr('name')).remove();
				else
					$(this).before('<span class="unsuccess">'+$(this).attr('errormessage')+'<br></span>');
		});

	$('.sub_frm input').each(function(i)
		{
		if($(this).attr('type')!=="button")
		if($(this).attr('type')!=="hidden")
			{
			errorDetected=0;
			valType = $(this).attr('validation');
			valInput = trim($(this).val());
			if(valInput.length==0)
				{
				if(valType!='v_none')
					{
						errorDetected=1;
						numErrors=numErrors+1;
					}
				}
			else if(valType=='v_url')
				{
				if(checkUrl(valInput)==false)
					{
					errorDetected=1;
					numErrors=numErrors+1;
					}
				}
			else if(valType=='v_email')
				{
				if(checkEmail(valInput)==false)
					{
					errorDetected=1;
					numErrors=numErrors+1;
					}
				}
			else if(valType=='v_tel')
				{
				if(checkTel(valInput)==false)
					{
					errorDetected=1;
					numErrors=numErrors+1;
					}
				}

			if(errorDetected==0)
				$('#eR_'+$(this).attr('name')).remove();
					else
						$(this).before('<span class="unsuccess">'+$(this).attr('errormessage')+'<br></span>');
			
			}
			
		});
	
	if(numErrors!=0)
	{
				alert('Please provide the information marked in yellow.');
				return false;
		}
	else 
	{
		queryString = '';
		$('.sub_frm textarea').each(function(i)
			{
			queryString = queryString+'&'+trim($(this).attr('name'))+'='+trim($(this).val());
			});
		$('.sub_frm input').each(function(i)
			{
			if($(this).attr('type')!=="hidden")
				if($(this).attr('type')!=="button")
					queryString = queryString+'&'+trim($(this).attr('name'))+'='+trim($(this).val());
			});
		$('.sub_frm select').each(function(i)
			{
			queryString = queryString+'&'+trim($(this).attr('name'))+'='+trim($(this).val());
			});
		if($('.newfileuploaded').length!==0)
			{
			queryString = queryString+'&uploadedfile=EventApp_'+$('.newfileuploaded').attr('flname');
			}
		
		$.post("process-applications.php", queryString+'&appName='+$('#appName').val(), function(data)
				{
				if(data.substr(0,2)=='ok')
					{
					//alert('Thank you, your information was submitted.');
					//alert($('#successMsg').val());

					if (type == "3")
					{
					    window.location = "featurebox.php";	
                    }
					else if ($('#appName').val() == 'eveApp')
					{
						//window.location = "event_app_success.php";
						window.location = "affiliate-success.php?event=1";	
					}
					else
					{
					    window.location = data.substr(2);	
					}
				} else
						{
						alert('Sorry there was a problem with your application:\n' + data);
						}
				});
		}
	});
});