///<summary>
/// Validates a simple email address
///</summary>
///<param name="Email">The email address to validate</param>
function EmailValidate(Email)
{
	var re=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	return re.test(Email);
}//EmailValidate


///<summary>
/// Validate the EmailTo form in the issues forums
///</summary>
///<param name="oErrMsgs">The error Messages</param>
function ValidateFieldsEmailTo(oErrMsgs)
{
    var oForm = document.EmailTo;     
    
    // if the current user doesn't have an email address then we aren't goint to send it
    if(oForm["ADXSTUDIO.ConfirmationFrom"].value.length < 1)
    {
        alert(oErrMsgs["useremailnone"])
        AttemptFocus(oForm["ADXSTUDIO.ConfirmationFrom"]);           
        return false;          
    }
    else if( !CheckMultiSelect(oForm.Recipient) )
    {
        alert(oErrMsgs["user_selectnone"]);
        AttemptFocus(oForm.Recipient);           
        return false;      
    }
    else if(oForm.Title.value.length < 1)
    {
        alert(oErrMsgs["titlenone"]);
        AttemptFocus(oForm.Title);           
        return false;        
    }
    else if(!ValidateStory(oForm.PostText, oErrMsgs, false))
    {
        return false;    
    }      	
      
    return true;
}//ValidateFieldsEmailTo

///<summary>
/// Validate the Whisper form in the issues forums
///</summary>
///<param name="oErrMsgs">The error Messages</param>
function ValidateFieldsWhisper(oErrMsgs)
{
    var oForm = document.Whisper;        

    if( !CheckMultiSelect(oForm.RecipientList) )
    {
        alert(oErrMsgs["user_selectnone"]);
        AttemptFocus(oForm.RecipientList);           
        return false;      
    }
    else if(oForm.Title.value.length < 1)
    {
        alert(oErrMsgs["titlenone"]);
        AttemptFocus(oForm.Title);           
        return false;        
    }
    else if(!ValidateStory(oForm.PostText, oErrMsgs, false))
    {
        return false;    
    }
    else if(!CheckRadioButton(oForm.Emotion))
    {
        alert(oErrMsgs["emotionnone"]);
        AttemptFocus(oForm.Emotion);           
        return false;         
    }      
    
    return true;
    
}//ValidateFieldsWhisper

///<summary>
/// Validate the Make a Post form in the issues forums
///</summary>
///<param name="oErrMsgs">The error Messages</param>
function ValidateFieldsMakeAPost(oErrMsgs)
{
    var oForm = document.MakeAPost;
    
    if(oForm.Title.value.length < 1)
    {
        alert(oErrMsgs["titlenone"]);
        AttemptFocus(oForm.Title);           
        return false;        
    }
    else if(!ValidateStory(oForm.PostText, oErrMsgs, false))
    {
        return false;    
    }
    else if(!CheckRadioButton(oForm.PostType))
    {
        alert(oErrMsgs["posttypenone"]);
        AttemptFocus(oForm.PostType);           
        return false;         
    }
    else if(!CheckRadioButton(oForm.Emotion))
    {
        alert(oErrMsgs["emotionnone"]);
        AttemptFocus(oForm.Emotion);           
        return false;         
    }        
    
    return true;
}//ValidateFieldsMakeAPost

///<summary>
/// Validate the Technical difficulties form
///</summary>
///<param name="oErrMsgs">The error Messages</param>
function ValidateFieldsTechnicalDifficulties(oErrMsgs)
{
    var oForm = document.TechnicalDifficulties;
    
    if(oForm.Username.value.length < 1)
    {
        alert(oErrMsgs["emailnone"]);
        AttemptFocus(oForm.Username);           
        return false;       
    }
    else if(!EmailValidate(oForm.Username.value) )
    {
        alert(oErrMsgs["emailinvalid"]);
        AttemptFocus(oForm.Username);   
        return false;           
    }      
    else if( !ValidateStory(oForm.Description, oErrMsgs, false) )
    {
        return false;
    }
    
    return true;
}//ValidateFieldsTechnicalDifficulties

///<summary>
/// Validate the rate this post form
///</summary>
///<param name="oErrMsgs">The message associative array</param>
function ValidateFieldsRate(oErrMsgs)
{
    var oForm = document.RateAnIdea;
    
    if(!CheckRadioButton(oForm.Rating))
    {
        alert(oErrMsgs["ratenone"]);
        AttemptFocus(oForm.Rating);           
        return false;         
    }        
    
    return true;
}//ValidateFieldsRate

///<summary>
/// Validates the forum edit form
///</summary>
///<param name="oErrMsgs">The message associative array</param>
function ValidateFieldsEdit(oErrMsgs)
{
    var oForm = document.EditOnAPost
    
    if(oForm.Title.value.length < 1)
    {
        alert(oErrMsgs["titlenone"]);
        AttemptFocus(oForm.Title);           
        return false;         
    }
    else if(!ValidateStory(oForm.PostText, oErrMsgs, false))
    {
        return false;
    }
    
    return true;
}//ValidateFieldsEdit

///<summary>
/// Validates the forum translate form
///</summary>
///<param name="oErrMsgs">The message associative array</param>
function ValidateFieldsTranslate(oErrMsgs)
{
    var oForm = document.TranslateAPost
    
    if(oForm.Title.value.length < 1)
    {
        alert(oErrMsgs["titlenone"]);
        AttemptFocus(oForm.Title);           
        return false;         
    }
    else if(!ValidateStory(oForm.PostText, oErrMsgs, false))
    {
        return false;
    }
    
    return true;   
}//ValidateFieldsEdit

///<summary>
/// Validates the forum reply form
///</summary>
///<param name="oErrMsgs">The message associative array</param>
function ValidateFieldsReply(oErrMsgs)
{
    var oForm = document.CommentOnAPost
    
    if(!ValidateStory(oForm.PostText, oErrMsgs, false))
    {
        return false;
    }
    else if(!CheckRadioButton(oForm.Emotion))
    {
        alert(oErrMsgs["emotionnone"]);
        AttemptFocus(oForm.Emotion);           
        return false;         
    }    
    
    return true;
}//ValidateFieldsReply
    
///<summary>
/// Validate a subscription sign up
///</summary>
///<param name="oErrMsgs">The message associative array</param>
function ValidateFieldsSubscriptionSignUp(oErrMsgs)
{
    var oForm = document.SubscriptionSignUp
    
    if( oForm.FirstName.value.length < 1)
    {
        alert(oErrMsgs["firstnamenone"]);
        AttemptFocus(oForm.FirstName);           
        return false;          
    }
    else if( oForm.LastName.value.length < 1)
    {
        alert(oErrMsgs["lastnamenone"]);
        AttemptFocus(oForm.LastName);           
        return false;          
    }    
    else if(oForm.EmailAddress.value.length < 1 )
    {
        alert(oErrMsgs["emailaddressnone"]);
        AttemptFocus(oForm.EmailAddress);           
        return false;              
    }
    else if(!EmailValidate(oForm.EmailAddress.value) )
    {
        alert(oErrMsgs["emailaddressinvalid"]);
        AttemptFocus(oForm.EmailAddress);   
        return false;           
    }     
    else if(oForm.EmailAddress.value != oForm.EmailAddressConfirm.value)   
    {
        alert(oErrMsgs["emailaddressconfirmnotmatch"]);
        AttemptFocus(oForm.EmailAddress);   
        return false;               
    }
 
    
    return true;
}//ValidateFieldsSubscriptionSignUp

///<summary>
/// Validate a Forgotton Password form
///</summary>
///<param name="oErrMsgs">The message associative array</param>
function ValidateFieldsForgottenPassword(oErrMsgs)
{
    var oForm = document.ForgotPassword;    

    if(oForm.Email.value.length < 1)
    {
        alert(oErrMsgs["youremailaddressnone"]);
        AttemptFocus(oForm.Email);           
        return false;       
    }
    else if(!EmailValidate(oForm.Email.value) )
    {
        alert(oErrMsgs["emailaddressinvalid"]);
        AttemptFocus(oForm.Email);   
        return false;           
    }    
    
    return true;     

}//ValidateFieldsForgottenPassword

///<summary>
/// Validate an Email A Friend Form
///</summary>
///<param name="oErrMsgs">The message associative array</param>
function ValidateFieldsEmailAFriend(oErrMsgs)
{
    var oForm = document.EmailaFriend;
    
    if(oForm.FriendEmail.value.length < 1)
    {
        alert(oErrMsgs["friendemailnone"]);
        AttemptFocus(oForm.FriendEmail);           
        return false;       
    }
    else if(oForm.YourName.value.length < 1)
    {
        alert(oErrMsgs["yournamenone"]);
        AttemptFocus(oForm.YourName);           
        return false;       
    }
    else if(oForm.YourEmail.value.length < 1)
    {
        alert(oErrMsgs["youremailaddressnone"]);
        AttemptFocus(oForm.YourEmail);           
        return false;       
    }
    else if(!EmailValidate(oForm.YourEmail.value) )
    {
        alert(oErrMsgs["emailaddressinvalid"]);
        AttemptFocus(oForm.YourEmail);   
        return false;           
    }    
    else if(oForm.Message.value.length >  parseInt(oErrMsgs["textmaxlength"], 10))
    {
        var iDiff = (oForm.Message.value.length - parseInt(oErrMsgs["textmaxlength"], 10));
        var sMsg = oErrMsgs["texttoomuch"];
            sMsg = sMsg.replace(/%MAX%/gi, oErrMsgs["textmaxlength"]).replace(/%CUR%/gi, oForm.Message.length).replace(/%DIFF%/gi, iDiff);            
        alert(sMsg);
        AttemptFocus(oForm.Message);           
        return false;      
    }
    
    var aAddresses = oForm.FriendEmail.value.split(",");
    for(var i=0; i<aAddresses.length; i++)
    {
        if( !EmailValidate(aAddresses[i]) )
        {
            alert(oErrMsgs["emailaddressinvalid"]);
            AttemptFocus(oForm.FriendEmail);   
            return false;         
        }
    }
    
    return true;  
}//ValidateFieldsEmailAFriend

///<summary>
/// Validate a registration submission
///</summary>
///<param name="oErrMsgs">The message associative array</param>
function ValidateFieldsRegistration(oErrMsgs)
{
    var oForm = document.Register;
    
    if(oForm.FirstName.value.length < 1)
    {
        alert(oErrMsgs["firstnamenone"]);
        AttemptFocus(oForm.FirstName);   
        return false;       
    }
    else if(oForm.LastName.value.length < 1)
    {
        alert(oErrMsgs["lastnamenone"]);
        AttemptFocus(oForm.LastName);   
        return false;       
    }
    else if(oForm.Email.value.length < 1)
    {
        alert(oErrMsgs["emailaddressnone"]);
        AttemptFocus(oForm.Email);   
        return false;       
    }
    else if(oForm.Email_reenter.value.length < 1)
    {
        alert(oErrMsgs["confirmationemailnone"]);
        AttemptFocus(oForm.Email_reenter);   
        return false;       
    }
    else if(oForm.Email.value != oForm.Email_reenter.value)
    {
        alert(oErrMsgs["emailaddressnomatch"]);
        AttemptFocus(oForm.Email);   
        return false;       
    }
    else if( !EmailValidate(oForm.Email.value) )
    {
        alert(oErrMsgs["emailaddressbad"]);
        AttemptFocus(oForm.Email);   
        return false;       
    }
    
    return true;
}//ValidateFieldsRegistration

///<summary>
/// Validate the Login/Change Password form
///</summary>
///<param name="bChangePassword">Are we changing the password</param>
///<param name="oErrMsgs">The message associative array</param>
function ValidateFieldLogin(bChangePassword, oErrMsgs)
{
    if(bChangePassword)
    {
        return ValidatePasswordChange(oErrMsgs);
    }
    else
    {
        return ValidateLogin(oErrMsgs);
    }
}//ValidateFieldLogin

///<summary>
/// Validates a Change Password attempt
///</summary>
///<param name="oMsgs">The message associative array</param>
function ValidatePasswordChange(oMsgs)
{
    var oForm = document.Logon;
    
        var iMin = parseInt( oMsgs["passwordminlength"] );
        var iMax = parseInt( oMsgs["passwordmaxlength"] );   
        
        if(iMin != -1 && oForm.NewPassword.value.length < iMin) 
        {
            var sMinMsg = oMsgs["passwordtooshort"];
                sMinMsg = sMinMsg.replace(/%MIN%/gi, oMsgs["passwordminlength"]).replace(/%MAX%/gi, oMsgs["passwordmaxlength"]);
            alert(sMinMsg);

            AttemptFocus(oForm.NewPassword);   
            return false;           
        }
        else if(iMax != -1 && oForm.NewPassword.value.length > iMax)
        {
            var sMaxMsg = oMsgs["passwordtoolong"];
                sMaxMsg = sMaxMsg.replace(/%MIN%/gi, oMsgs["passwordminlength"]).replace(/%MAX%/gi, oMsgs["passwordmaxlength"]);
            alert(sMaxMsg);
            AttemptFocus(oForm.NewPassword);   
            return false;           
        }
        else if(oForm.NewPasswordConfirm.value != oForm.NewPassword.value)
        {
            alert(oMsgs["passwordsnonmatch"]);
            AttemptFocus(oForm.NewPassword);   
            return false;          
        }        
    
    return true;          
}//ValidatePasswordChange

///<summary>
/// Validates a Login attempt
///</summary>
///<param name="oMsgs">The message associative array</param>
function ValidateLogin(oMsgs)
{
    var oForm = document.Logon;
    
    if(oForm.Username.value.length < 1)
    {
        alert(oMsgs["username"]);
        AttemptFocus(oForm.Username);   
        return false;                
    }
    else if(oForm.Password.value.length < 1)
    {
        alert(oMsgs["password"]);
        AttemptFocus(oForm.Password);   
        return false;                    
    }
    
    return true;                    
    
}//ValidateLogin

///<summary>
/// Validate the solution submission form
/// Supports the %MAX%, %CUR% and %DIFF% replaces in the text area
///</summary>
///<param name="oErrMsgs">The message associative array</param>
function ValidateFieldsSolution(oErrMsgs)
{
    var oForm = document.Solution;    
        
    if( !ValidateCategory(oForm.Category, oErrMsgs))
    {
        return false;             
    }
    else if( !ValidateTitle(oForm.Title, oErrMsgs))
    {
        return false;                  
    }
    else if( oForm.Story.value.length < 1 && oForm.Attachment.value.length < 1)
    {
        alert(oErrMsgs["attchmentnoneandstorynone"]);
        AttemptFocus(oForm.Story);
        return false;
    }
    else if(oForm.Story.value.length > parseInt(oErrMsgs["textmaxlength"], 10))
    {
            // build the message
            var iDiff = (oForm.Story.value.length - parseInt(oErrMsgs["textmaxlength"], 10));
            var sMsg = oErrMsgs["texttoomuch"];
                sMsg = sMsg.replace(/%MAX%/gi, oErrMsgs["textmaxlength"]).replace(/%CUR%/gi, oForm.Story.value.length).replace(/%DIFF%/gi, iDiff);
                
            alert(sMsg);
            AttemptFocus(oForm.Story);   
           
        return false;         
    }//MaxLength violation    
    else if( !ValidateShare(oForm.Share, oErrMsgs) )
    {
        return false;                 
    }
   
   return true;
}//ValidateFieldsSolution

///<summary>
/// Validate the story submission form
/// Supports the %MAX%, %CUR% and %DIFF% replaces in the text area
///</summary>
///<param name="oErrMsgs">The message associative array</param>
function ValidateFieldsStory(oErrMsgs)
{
    var oForm = document.Story;    
        
    if( !ValidateCategory(oForm.Category, oErrMsgs))
    {
        return false;             
    }
    else if( !ValidateTitle(oForm.Title, oErrMsgs))
    {
        return false;                  
    }
    else if( !ValidateStory(oForm.Story, oErrMsgs))
    {
        return false;
    }
    else if(!ValidateShare(oForm.Share, oErrMsgs) )
    {
        return false;                 
    }
   
   return true;

}//ValidateFieldsStory

///<summary>
/// Validates that there is a file attached
///</summary>
///<param name="oField">The File upload field</param>
///<param name="oMsgs">The message associative array</param>
///<param name="bSilent">Does not display a message to the user</param>
function ValidateAttachment(oField, oMsgs, bSilent)
{
    if(oField.value.length < 1)
    {
        if(bSilent == null || bSilent == false)
        {
            alert(oMsgs["attchmentnone"]);
            AttemptFocus(oField);   
        }//we are not silent
        
        return false;  
    }//bad value
    
    return true;     
    
}//ValidateAttachment

///<summary>
/// Validates the category input
///</summary>
///<param name="oField">The category select</param>
///<param name="oMsgs">The message associative array</param>
function ValidateCategory(oField, oMsgs)
{
    var sCat = oField.options[oField.selectedIndex].value;
    if(sCat == "" || sCat == "BadValue")
    {
        alert(oMsgs["categorynone"]);
        AttemptFocus(oField);   
        return false;             
    }
    
    return true;             
}//ValidateCategory

///<summary>
/// Validates the title input
///</summary>
///<param name="oField">The Title field</param>
///<param name="oMsgs">The message associative array</param>
function ValidateTitle(oField, oMsgs)
{
	if(oField != null)
	{
		if(oField.value.length < 1)
		{
			alert(oMsgs["titlenone"]);
			AttemptFocus(oField);   
			return false;                  
		}
    }

    return true;
    
}//ValidateTitle

///<summary>
/// Validates the share type
///</summary>
///<param name="oField">The share radio buttons</param>
///<param name="oMsgs">The message associative array</param>
function ValidateShare(oField, oMsgs)
{
    if(oField != null)
    {
        if( !CheckRadioButton(oField) )
        {
            alert(oMsgs["sharenone"]);
            AttemptFocus(oField);   
            return false;                 
        }
    }
   
    return true;

}//ValidateShare

///<summary>
/// Validates the story textarea
///</summary>
///<param name="oField">The story textarea object</param>
///<param name="oMsgs">The message associative array</param>
///<param name="bSilent">Does not display a message to the user</param>
function ValidateStory(oField, oMsgs, bSilent)
{    
    if(oField.value.length < 1)
    {
        if(bSilent == null || bSilent == false)
        {
            alert(oMsgs["textnone"]);
            AttemptFocus(oField);   
        }//we are not silent
        
        return false;         
    }
    else if(oField.value.length > parseInt(oMsgs["textmaxlength"], 10))
    {
        if(bSilent == null || bSilent == false)
        {
            // build the message
            var iDiff = (oField.value.length - parseInt(oMsgs["textmaxlength"], 10));
            var sMsg = oMsgs["texttoomuch"];
                sMsg = sMsg.replace(/%MAX%/gi, oMsgs["textmaxlength"]).replace(/%CUR%/gi, oField.value.length).replace(/%DIFF%/gi, iDiff);
                
            alert(sMsg);
            AttemptFocus(oField);   
        }//we are not silent
        
        return false;         
    }//MaxLength violation
    
    return true;
    
}//ValidateStory

///<summary>
/// Validates the story textarea
///</summary>
///<param name="oField">The story textarea object</param>
///<param name="bSilent">Does not display a message to the user</param>
///<param name="None">The none message</param>
///<param name="TooMuch">The TooMuch message</param>
///<param name="Max">The maximum length</param>
function ValidateStoryNoAssociativeArray(oField, bSilent, None, TooMuch, Max)
{    
    if(oField.value.length < 1)
    {
        if(bSilent == null || bSilent == false)
        {
            alert(None);
            AttemptFocus(oField);   
        }//we are not silent
        
        return false;         
    }
    else if(oField.value.length > parseInt(Max, 10))
    {
        if(bSilent == null || bSilent == false)
        {
            // build the message
            var iDiff = (oField.value.length - parseInt(Max, 10));
            var sMsg = TooMuch;
                sMsg = sMsg.replace(/%MAX%/gi, Max).replace(/%CUR%/gi, oField.value.length).replace(/%DIFF%/gi, iDiff);
                
            alert(sMsg);
            AttemptFocus(oField);   
        }//we are not silent
        
        return false;         
    }//MaxLength violation
    
    return true;
    
}//ValidateStory

///<summary>
/// Validates that a multiselect has at least 1 non-empty selection made
///</summary>
///<param name="oInput">The input to check</param>
function CheckMultiSelect(oInput)
{
    var AtLeastOne = false;
    if(oInput != null)
    {
        for(var i=0; i<oInput.options.length; i++)
        {
            if(oInput.options[i].selected == true && oInput.options[i].value != "")
            {
                AtLeastOne = true;
            }
        }//for each option
    }//good input
    return AtLeastOne;
}//CheckMultiSelect

///<summary>
/// Validates a Canadian postal code
///</summary>
///<param name="inPostal">The postal code to test</param>
function ValidatePostalCode(inPostal)
{
    var sPostal = inPostal.toLowerCase();
    var PostalPattern = /[a-z]\d[a-z] \d[a-z]\d/;
    return PostalPattern.test(sPostal);    
}//ValidatePostalCode

///<summary>
/// Validate a phone number in the format (111) 111-1111
///</summary>
///<param name="inPhone">The phone number to match</param>
function ValidatePhoneNumber(inPhone)
{
    var PhonePattern = /\(\d\d\d\) \d\d\d-\d\d\d\d/;
    return PhonePattern.test(inPhone);
}//ValidatePhoneNumber

///<summary>
/// Attempt to focus on a given input
///</summary>
///<param name="oInput">The input to focus on</param>
function AttemptFocus(oInput)
{
    try
    {
        oInput.focus();
        return true;
    }
    catch(e)
    {
        return false;
    }
}//AttemptFocus

///<summary>
/// Submits a form if the submission data is valid.
///</summary>
function SubmitForm(formName, validSubmission)
{
	if(validSubmission)
	{
		var oForm = document.forms[formName];
		oForm.submit();
	}
}
