// General validation Functions

function st_retval(myselect)
{
	return( myselect.options[myselect.selectedIndex].value );
}

function st_valid_text( txt )
{
	var r1 = new RegExp("[^ ]+");
	return(r1.test(txt));
}

function st_valid_numeric( txt )
{
	var r1 = new RegExp("[^ ]+");
	return(r1.test(txt));
}

function st_valid_numeric( txt )
{
	var r1 = new RegExp("^\\d+$");
	return(r1.test(txt));

}

function st_min_chars( txt,min,max )
{	
	var r1 = new RegExp(".{" + min + "," + max + "}");
 	return(r1.test(txt));
}

function st_illegal_chars( txt )
{
	var r1 = new RegExp("[^a-zA-Z0-9]+");
 	return(r1.test(txt));
}


function st_valid_email( txt )
{
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return(!r1.test(txt) && r2.test(txt));
}

// NOT LOGGON ON - SET MENU ACTION
function menuaction( theform, action )
{
	// Modify the form action
	var oldaction = theform.action;
	var newaction = oldaction.substring(0, oldaction.lastIndexOf("/") ) + "/" + action; 
	theform.action = newaction;

	// Submit the form
	theform.submit();
}

// LOGON	
function validate_logon( theform ) 
{
	if( !st_valid_text(theform.con_email.value) )
	{
		alert( "Please enter your email address." );
		return false;
	}
	else if( !st_valid_text(theform.con_password.value) )
	{
		alert( "Please enter your password." );
		return false;
	}				
	return true;
}

// EMAIL A FRIEND	
function validate_eaf( theform ) 
{
	if( !st_valid_text( theform.eaf_fromname.value ) )
	{
		alert( "You must provide your name." );
		theform.eaf_fromname.focus();
		return false;
	}
	else if( !st_valid_text( theform.eaf_fromemail.value ) )
	{
		alert( "You must provide your email address." );
		theform.eaf_fromemail.focus();
		return false;
	}
	else if( !st_valid_email(theform.eaf_fromemail.value))
	{
		alert( "Your email Address is not valid." );
		theform.eaf_fromemail.focus();
		return false;
	}
	else if( !st_valid_text( theform.eaf_message.value ) )
	{
		alert( "You must provide a message." );
		theform.eaf_message.focus();
		return false;
	}
	else if( !st_valid_text( theform.eaf_toname1.value ) )
	{
		alert( "You must provide a name for Friend 1." );
		theform.eaf_toname1.focus();
		return false;
	}
	else if( !st_valid_text( theform.eaf_toemail1.value ) )
	{
		alert( "You must provide an email address for Friend 1." );
		theform.eaf_toemail1.focus();
		return false;
	}
	else if( !st_valid_email(theform.eaf_toemail1.value))
	{
		alert( "The email address you provided for Friend 1 is not valid." );
		theform.eaf_toemail1.focus();
		return false;
	}
	else if( theform.eaf_toname2.value != "" || theform.eaf_toemail2.value != "" )
	{
	    if( !st_valid_text( theform.eaf_toname2.value ) )
	    {
		    alert( "You must provide a name for Friend 2." );
		    theform.eaf_toname2.focus();
		    return false;
	    }
	    else if( !st_valid_text( theform.eaf_toemail2.value ) )
	    {
		    alert( "You must provide an email address for Friend 2." );
		    theform.eaf_toemail2.focus();
		    return false;
	    }
	    else if( !st_valid_email(theform.eaf_toemail2.value))
	    {
		    alert( "The email address you provided for Friend 2 is not valid." );
		    theform.eaf_toemail2.focus();
		    return false;
	    }
	
	}
	else if( theform.eaf_toname3.value != "" || theform.eaf_toemail3.value != "" )
	{
	    if( !st_valid_text( theform.eaf_toname3.value ) )
	    {
		    alert( "You must provide a name for Friend 3." );
		    theform.eaf_toname3.focus();
		    return false;
	    }
	    else if( !st_valid_text( theform.eaf_toemail3.value ) )
	    {
		    alert( "You must provide an email address for Friend 3." );
		    theform.eaf_toemail3.focus();
		    return false;
	    }
	    else if( !st_valid_email(theform.eaf_toemail3.value))
	    {
		    alert( "The email address you provided for Friend 3 is not valid." );
		    theform.eaf_toemail3.focus();
		    return false;
	    }
	
	}
	return true;
}
// FEEDBACK	
function validate_feedback( theform ) 
{
	if( !st_valid_text( theform.fbk_name.value ) )
	{
		alert( "You must provide a preferred name." );
		theform.fbk_name.focus();
		return false;
	}
	else if( !st_valid_text( theform.fbk_email.value ) )
	{
		alert( "You must provide an email address." );
		theform.fbk_email.focus();
		return false;
	}
	else if( !st_valid_email(theform.fbk_email.value))
	{
		alert( "Email Address is not valid." );
		theform.fbk_email.focus();
		return false;
	}
	else if( !st_valid_text( theform.fbk_message.value ) )
	{
		alert( "You must provide a message." );
		theform.fbk_message.focus();
		return false;
	}
	return true;
}


// SNAPTAPS _ EMAIL	
function validate_email( theform ) 
{
	if( !st_valid_text( theform.email.value ) )
	{
		alert( "You must provide an email address." );
		theform.email.focus();
		return false;
	}
	else if( !st_valid_email(theform.email.value))
	{
		alert( "Email Address is not valid." );
		theform.email.focus();
		return false;
	}

	return true;
}


function sendpassword( theform ) 
{
	if( !st_valid_text(theform.con_email.value) )
	{
		alert( "Please enter your email address." );
		theform.con_email.focus();
	}
	else if( !st_valid_email(theform.con_email.value))
	{
		alert( "Email Address is not valid." );
		theform.con_email.focus();
	}
	else
	{
	    var oldaction = theform.action;
		var newaction = oldaction.substring(0, oldaction.lastIndexOf("/") ) + "/SENDPASSWORD"; 
		theform.action = newaction;

		// Submit the form
		theform.submit();
	}		
}

// REGISTER
function validate_register( theform ) 
{
	if( !st_valid_text( theform.con_name.value ) )
	{
		alert( "You must provide a preferred name." );
		theform.con_name.focus();
		return false;
	}
	else if( !st_valid_text( theform.con_email.value ) )
	{
		alert( "You must provide an email address." );
		theform.con_email.focus();
		return false;
	}
	else if( !st_valid_email(theform.con_email.value))
	{
		alert( "Email Address is not valid." );
		theform.con_email.focus();
		return false;
	}
	else if( !st_valid_text(theform.con_password.value))
	{
		alert( "Please specify a password." );
		theform.con_password.focus();
		return false;
	}
	else if( !st_valid_text(theform.con_password_confirm.value))
	{
		alert( "Please confirm your password." );
		theform.con_password_confirm.focus();
		return false;
	}
	else if( theform.con_password_confirm.value != theform.con_password.value  )
	{
		alert( "Passwords do not match." );
		theform.con_password_confirm.value = "";
		theform.con_password_confirm.focus();
		return false;
	}
		
	return true;
}

// MY DETAILS
function validate_editdetails( theform ) 
{
	if( !st_valid_text( theform.con_name.value ) )
	{
		alert( "You must provide a preferred name." );
		theform.con_name.focus();
		return false;
	}
	else if( !st_valid_text( theform.con_email.value ) )
	{
		alert( "You must provide an email address." );
		theform.con_email.focus();
		return false;
	}
	else if( !st_valid_email(theform.con_email.value))
	{
		alert( "Email Address is not valid." );
		theform.con_email.focus();
		return false;
	}
		
	return true;
}

// MY DETAILS - PASSWORD
function validate_password( theform ) 
{
    if( !st_valid_text(theform.existing.value))
	{
		alert( "Please provide your existing password." );
		theform.existing.focus();
		return false;
	}
	else if( !st_valid_text(theform.con_password.value))
	{
		alert( "Please specify a new password." );
		theform.con_password.focus();
		return false;
	}
	else if( !st_valid_text(theform.con_password_confirm.value))
	{
		alert( "Please confirm your new password." );
		theform.con_password_confirm.focus();
		return false;
	}
	else if( theform.con_password_confirm.value != theform.con_password.value  )
	{
		alert( "New passwords do not match." );
		theform.con_password_confirm.value = "";
		theform.con_password_confirm.focus();
		return false;
	}
		
	return true;
}


// MYALERTS

function validate_cin( theform ) 
{
    // We're only validating on create / update CIN
    var formaction = theform.action;
    var progaction = formaction.substring(formaction.lastIndexOf("/")+1);

        
    if( progaction != "SUBMITCIN" && progaction != "UPDATECIN" && progaction != "SUBMITALERTME" )
        return true;
        
	if( !st_valid_text(theform.cin_searchstring.value) )
	{
		alert( "Please provide a search term." );
		theform.cin_searchstring.focus();
		return false;
	}
	else if( theform.cin_searchstring.value == "e.g. French, red, 'Cabernet Sauvignon'" )
	{
		alert( "Please provide a search term." );
		return false;
	}
	else if( theform.cin_minprice.value >= 99999 )
	{
		alert( "Minimum price must less than 99999GBP." );
		theform.cin_minprice.focus();
		return false;
	}	
	else if( !st_valid_text(theform.cin_maxprice.value) )
	{
		alert( "Please provide a maximum price." );
		theform.cin_maxprice.focus();
		return false;
	}	
	else if( !st_valid_numeric(theform.cin_maxprice.value) )
	{
		alert( "Maximum price must be numeric, in pounds." );
		theform.cin_maxprice.focus();
		return false;
	}	
	else if( theform.cin_maxprice.value >= 99999 )
	{
		alert( "Maximum price must less than 99999GBP." );
		theform.cin_maxprice.focus();
		return false;
	}				
	return true;
}

function clearsearchstring( element )
{
    if( element.value == "e.g. French, red, 'Cabernet Sauvignon'" )
        element.value = "";
}


function clearmessage( element )
{
    if( element.value == "I found this great service and thought you might be interested in signing up" )
        element.value = "";
}

function clearemail( element )
{
    if( element.value == "Email address" )
        element.value = "";
}


function delete_cin( theform, id )
{
	if( confirm("Are you sure that you want to PERMANENTLY DELETE this interest?") )
	{
		// Modify the form action
		var oldaction = theform.action;
		var newaction = oldaction.substring(0, oldaction.lastIndexOf("/") ) + "/DELETECIN"; 
		theform.action = newaction;

		// Set the value of the hidden form element 'cin_id' to be this one
		theform.cin_id.value = id;

		// Submit the form
		theform.submit();
	}
}

function edit_cin( theform, id )
{
	// Modify the form action
	var oldaction = theform.action;
	var newaction = oldaction.substring(0, oldaction.lastIndexOf("/") ) + "/EDITCIN"; 
	theform.action = newaction;

	// Set the value of the hidden form element 'cin_id' to be this one
	theform.cin_id.value = id;

	// Submit the form
	theform.submit();
}

function activate_cin( theform, id )
{
	// Modify the form action
	var oldaction = theform.action;
	var newaction = oldaction.substring(0, oldaction.lastIndexOf("/") ) + "/ACTIVATECIN"; 
	theform.action = newaction;

	// Set the value of the hidden form element 'cin_id' to be this one
	theform.cin_id.value = id;

	// Submit the form
	theform.submit();
}

function deactivate_cin( theform, id )
{
	// Modify the form action
	var oldaction = theform.action;
	var newaction = oldaction.substring(0, oldaction.lastIndexOf("/") ) + "/DEACTIVATECIN"; 
	theform.action = newaction;

	// Set the value of the hidden form element 'cin_id' to be this one
	theform.cin_id.value = id;

	// Submit the form
	theform.submit();
}

