// validate that the user has checked one of the radio buttons
function isValidRadio(radio) {
    var valid = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return true;
        }
    }
    alert("Make a choice from the radio buttons.");
    return false;
}

function isMatch(elem1, elem2)
{
	var str1 = elem1.value;
	var str2 = elem2.value;
	if(str1 != str2)
	{
		alert(str1 + " should match with " + str2);
		setTimeout("focusElement('" + elem2.form.name + "', '" + elem2.name + "')", 0);
		return false;
	}
	else
	{
		return true;	
	}	
}

// validate that the user made a selection other than default
function isChosen(select) {
    if (select.selectedIndex == 0) {
        alert("Please make a choice from the list.");
        return false;
    } else {
        return true;
    }
}

function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus( );
    elem.select( );
}


function isEMailAddr(elem) {
    var str = elem.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
        alert("Verify the email address format.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);        
        return false;
	} else {
		return true;
	}
}

// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
    var str = elem.value;
    var re = /.+/;
    if(!str.match(re)) {
        alert("Please fill in the required field.");       
        return false;
    } else {
        return true;
    }
}

function checkAgree(form)
{
	if(!form.checked)
	{
		alert('You must accept the Terms and Agreement of Jobmarket Online');
		return false;
	}
	else
	{
		return true; 	
	}	
}

function quick_apply(form)
{
	if(isNotEmpty(form.jobseeker_name))
	{
		if (isEMailAddr(form.jobseeker_email))
	    {
	    	if(isNotEmpty(form.cover_letter))
			{	
				form.submit();
			}
	    }
	}
	return false;
}

function validateForm(form)
{
    if (isEMailAddr(form.email))
	{
		if (isEMailAddr(form.email_confirm))
	    {
	    	if(!isMatch(form.email, form.email_confirm))
	        {
		        if (isNotEmpty(form.passwd))
		        {
		            if (isNotEmpty(form.passwd_confirm))
		            {   
		            	//if(isMatch(form.passwd, form.passwd_confirm))
		            	//{
			                if (isNotEmpty(form.firstname))
			            	{
			            		if (isNotEmpty(form.lastname))
				            	{
				            		if(checkAgree(form.accepted_terms))
									{
										form.submit();
									}
				            	}		
			            	}	            	
		            	//}
		            }
		        }
	        
	    	}
	    }
	}
    return false;
}