/*------------------------------------------------------------------------------------------*/
function validate()
{
	var varsToCheck= new Array();
	varsToCheck[0]="first";
	varsToCheck[1]="last";
	varsToCheck[2]="email";

	var validated=true;
	var this_field_false_first=true;
	var errormsg="To get started, we need to know your name and email address, so we can send you the instructions and feedback from your friends. Don't worry, we don't share this information with anyone without your explicit permission.";

	for(var i = 0; i<varsToCheck.length; i++)
	{
		var n=varsToCheck[i]; var this_field_valid=true;
		var v=""; var el=document.getElementById("infoplus");

		switch (n)
		{
			case "email":
				v=document.getElementById(n).value; el=document.getElementById(n);
				if (emailcheck(v)==false)
				{
					errormsg=errormsg+"<br /><br />Email address doesn't seem to be correct. Please check it.";
					this_field_valid=false;
				}
				errorborder(this_field_valid,el);
			break;

			// make special case for friends

			// resume
			case "resume":
				// validation on length
				v=document.getElementById(n).value; el=document.getElementById(n);
				if (v.length<=1000)
				{
					errormsg=errormsg+"<br /><br />Resume seems too short to be real. Please check it.";
					this_field_valid=false;
				}
				errorborder(this_field_valid,el);
			break;

			// default case
			default: 
				// validation on length
				v=document.getElementById(n).value; el=document.getElementById(n);
				if (v.length<=1) { this_field_valid=false; }
				errorborder(this_field_valid,el);
			break;
		}

		// focus cursor on the first incorrect field
		if (this_field_false_first && !this_field_valid)
		{
			this_field_false_first=false;
			el.focus();
		}

		// highlight incorrect fields
		if (!this_field_valid)
		{
			validated=false;
		}
	}

	if (validated)
	{ return true; }
	else
	{
		document.getElementById("validatestr").innerHTML='<span class="validate-error">'+errormsg+'</span>';
		return false;
	}
}

/*------------------------------------------------------------------------------------------*/
function emailcheck(str)
{
	var at="@"; var dot=".";
	var lat=str.indexOf(at); var lstr=str.length; var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1) { return false; }
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) { return false; }
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) { return false; }
	if (str.indexOf(at,(lat+1))!=-1) { return false; }
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) { return false; }
	if (str.indexOf(dot,(lat+2))==-1) { return false; }
	if (str.indexOf(" ")!=-1) { return false; }

	return true					
}

/*------------------------------------------------------------------------------------------*/
function errorborder(valid,obj)
{
	if (valid==false)
		{ obj.style.border="2px solid #CC0000"; }
	else
		{ obj.style.border="none"; }
}

/*------------------------------------------------------------------------------------------*/
function validate_and_submit() 
{
	if (!validate())
	{ return false; }

	new Ajax.Request('/a/second_submit_location', { parameters: document.buyjob.serialize(true), onSuccess: function(transport){
      var response = transport.responseText || "no response text";
      alert("Success! \n\n" + response);
    }
 });

	return true;
}

/* if 'obj' checkbox is checked, hides element 'hide' & shows 'show' */
/* if 'obj' checkbox is unchecked, hides element 'show' & shows 'hide' */
function toggle_checkbox(obj,hide,show)
{
        var checked=obj.checked;

        if (checked)
        {
        	if (hide!="") { document.getElementById(hide).style.display="none"; }
			if (show!="") { document.getElementById(show).style.display="block"; }
        }
        else
        {
        	if (hide!="") { document.getElementById(hide).style.display="block"; }
			if (show!="") { document.getElementById(show).style.display="none"; }
        }
}

/* show div div_show, hide div_hide */
function show(div_hide,div_show)
{
	if (div_hide!="") { document.getElementById(div_hide).style.display="none"; }
	if (div_show!="") { document.getElementById(div_show).style.display="block"; }
}

