//*************************************/
//
//  MISCELLANEOUS FUNCTIONS 
//  v1.0 Initial version - bnb 4/12/08
//
//*************************************/




// TRIM LEADING AND TRAILING WHITESPACE FROM STRINGS //

function trimString(str){

  return str.replace(/^\s+|\s+$/g, '');

}


// REMOVE SPACES FROM STRING //

function removeSpaces(str){

  return str.replace(/\s*/g,'');

}


// CHANGE TO UPPER CASE //

function upperCase(str){

    return str.toUpperCase();

}


// CHANGE TO LOWER CASE //

function lowerCase(str){

    return str.toLowerCase();

}


// RETURN FOCUS TO CELL ON ERROR //

function returnFocus(Field_ID){ 


    //////////////////////////////////////////////////////////////////
    // IMPORTANT NOTES:
    //  you must define the field, but do NOT use var to define it.
    //  It must be a different name than the field_id name sent it.
    /////////////////////////////////////////////////////////////////


	field_id = Field_ID;

    // Had to use the setTimeout function because of a bug in Firefox

	setTimeout("document.getElementById(field_id).focus();",15);

    // Had to build this because of a bug in Firefox related to select fields

    if (document.getElementById(field_id).type !== "select-one"){
        document.getElementById(field_id).select();
    }
	return;
}



// SHOW / HIDE INLINE MESSAGES //

function displayInlineMsg(ShowHide, Example_Field_ID, Error_Field_ID, Error_Msg){

    ex_field_id = Example_Field_ID;
    err_field_id = Error_Field_ID;
    if(!Error_Msg){
        Error_Msg = "";
    }

	if(ShowHide === "Show" | ShowHide === "show"){
       	document.getElementById(ex_field_id).style.display = "none";
        document.getElementById(err_field_id).style.display = "inline";
        document.getElementById(err_field_id).innerHTML = Error_Msg;
	}    

    if(ShowHide === "Hide" | ShowHide === "hide"){
        document.getElementById(ex_field_id).style.display = "inline";
        document.getElementById(err_field_id).style.display = "none";
    }
  return;
}

// DETERMINE WHICH STYLE OF ERROR DISPLAY TO USE //

function errorDisplayType(Field_ID, 
                            Error_Display_Type, // A = alert, I = show inline
                            Example_Field_ID,   // example text span ID
                            Error_Field_ID,     // error text span ID
                            Error_Msg){         // error message to display
                            
    if (Error_Msg.length > 0){
        if(Error_Display_Type === "A" | Error_Display_Type === "a"){
            alert(Error_Msg);
        }
        else
        {
            displayInlineMsg("show", Example_Field_ID, Error_Field_ID, Error_Msg);
        }
        returnFocus(Field_ID);
    }
    return;
}



// REQUIRE AT LEAST X CHARACTERS //

function enoughCharacters(Field_ID, Field_Value, Min_Chars){ 
	
    // Remove all white space
    var str = removeSpaces(Field_Value);
    var minChars = (Min_Chars * 1);
    var charLength = str.length;

	if (charLength < minChars){
	   returnFocus(Field_ID, "Please enter at least " + minChars + " letters not including spaces.");
	   return false;
    }
    return;
}

// CHECK EMAIL ADDRESS //

function checkEmail(Field_ID, 
                            Error_Display_Type, // A = alert, I = show inline
                            Required,            // Y = required
                            Profanity,          // Y = search for and remove any profanities
                            Example_Field_ID,   // example text span ID
                            Error_Field_ID){    // error text span ID
   
    var Error_Msg = "";
    field_id = Field_ID;
    
    // Trim String   
    var strValue = trimString(document.getElementById(field_id).value);
       
    var i = 1;
    var strLength = strValue.length;

    // look for @
    while ((i < strLength) && (strValue.charAt(i) != "@")){i++;}

    if ((i >= strLength) || (strValue.charAt(i) != "@")){

	   Error_Msg = "The email address is missing an \@ symbol.";
    }
    else
    {
        // look for .
        while ((i < strLength) && (strValue.charAt(i) != ".")){i++;}

        // there must be at least one character after the .
        if ((i >= strLength - 1) || (strValue.charAt(i) != ".")){

    	   Error_Msg = "There must be at least one letter after the period \(.\) symbol.";
        }
        else if (strValue.length < 7) { // Check if strValue is too short
            Error_Msg = "The email address is too short to be valid.";
        }
        else if (strValue.length > 60) { // Check if strValue is too long
            Error_Msg = "The \"Email address\" field is limited to 60 characters.";
        }
        else 
        {
            // check for invalid characters
            var regExpPat = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
            if (!regExpPat.test(strValue)) {
                Error_Msg = "The email address does not appear to be valid.";
            }
            else
            {
            displayInlineMsg("hide", Example_Field_ID, Error_Field_ID, Error_Msg);
            }  
        }
    } 
    

    // Check if Required
    if ((upperCase(Required) === "Y" || upperCase(Required) === "YES") && 
        (strValue === null || strValue.length === 0)) {
        Error_Msg = "This is a required field.";
    }
    else
    {
       displayInlineMsg("hide", Example_Field_ID, Error_Field_ID, Error_Msg);
    }    
    
    // PROFANITY CHECK GOES HERE //

    // Determine how to handle the error message if any
    errorDisplayType(Field_ID, Error_Display_Type, Example_Field_ID, Error_Field_ID, Error_Msg);
    
    return Error_Msg;
}


// MASK EMAIL FROM SPAM SPIDERS //
function maskEmail(Username, Domain, Extension, Text, Subject, Body){

	var atSign = "&#64;";
	var address = Username + atSign + Domain + "." + Extension;
	if(Subject){
		address = address + "?subject=" + Subject;
	}
	if(Body){
		address = address + "&body=" + Body;
	}
	if(!Text){
		Text = address;
	}
	document.write("<a" + " href=" + "mailto:" + address + ">" + Text + "<\/a>");
}

// ALTERNATE IMAGES ON MOUSEOVER //

function change(Img_Url, Img_ID) {

    img_url = Img_Url; // location and name of the photo
	img_id = Img_ID; // id of the img to change
   
    document.getElementById(img_id).src = img_url;
}



//-->

