/*
    Author: Omar Ellis
    Subject: Javascript Validation and Formatting library.
    Date Created: 6/28/2006

    The function will valid any SSN type (e.g. dashes, spaces, or no spaces).
    @ param sSSN The SSN to be validated.
    return bool
*/
/*
    The function will remove any spaces found in a string.
    @ param sString The string to be formatted.
    return string
*/
function Format_RemoveAllSpaces(sString){
   return sString.replace(/\s/g,"");
}
/*
    The function will leave only single spaces in a string.
    @ param sString The string to be formatted.
    return string
*/        
function Format_SingleSpace(sString){
   return sString.replace(/\s{2,}/g," ");
}
/*
     The function will check to make sure there is no dashes and the length is the correct
     size before formatting the string.
     *** Requires function Format_RemoveAllSpaces()
     @ param sSSN The SSN string to be formatted.
     return string
*/
function Format_SSN_Dashed(sSSN){
    if(/^[-]{1,}$/.test(sSSN)) return sSSN;
    sSSN = Format_RemoveAllSpaces(sSSN);
    if(/^(\d){10,}$/.test(sSSN)) return sSSN;
    return sSSN.substring(0,3) + "-" + sSSN.substring(3,5) + "-" + sSSN.substring(5,sSSN.length+2);
}
/*
     The function will check to make sure there is no dashes, no parentheses, and the length is the correct
     size before formatting the string.
     *** Requires function Format_RemoveAllSpaces()
     @ param sPhoneNumber The Phone number excluding area code to be formatted.
     @ param Seperator The character used to seperate the sections in the phone number
     return string
*/        
function Format_PhoneNumber(sPhoneNumber,Seperator){
    if( /^[-]{1,}$/.test(sPhoneNumber) ) return sPhoneNumber;
    sPhoneNumber = Format_RemoveAllSpaces(sPhoneNumber);
    if( /^(.){8,}$/.test(sPhoneNumber) ) return sPhoneNumber;
    return sPhoneNumber.substring(0,3) + Seperator + sPhoneNumber.substring(3,sPhoneNumber.length+2);
}        
/*
     The function will check to make sure there is no dashes, no parentheses, and the length is the correct
     size before formatting the string.
     *** Requires function Format_RemoveAllSpaces()
     @ param sPhoneNumber The Phone number including area code to be formatted.
     @ param Seperator The character used to seperate the sections in the phone number
     return string
*/        
function Format_PhoneNumberWithAreaCode(sPhoneNumber,Seperator){
    if( /^[-]{1,}$/.test(sPhoneNumber) ) return sPhoneNumber;
    sPhoneNumber = Format_RemoveAllSpaces(sPhoneNumber);
    if( /^(.){11,}$/.test(sPhoneNumber) ) return sPhoneNumber;
    return sPhoneNumber.substring(0,3) + Seperator + sPhoneNumber.substring(3,6) + Seperator + sPhoneNumber.substring(6,sPhoneNumber.length+2);
}
function Format_FormattedText(osWord){
	var iLEN = osWord.length;
	var nsWord = "";
	
	for(var i=0;i<iLEN;i++){
		if(i == 0){
			nsWord+=osWord.charAt(i).toUpperCase();
		}
		else if(osWord.charAt(i) == '-' || osWord.charAt(i) == ' ' || osWord.charAt(i) == '\''){
			nsWord+=osWord.charAt(i);
			nsWord+=osWord.charAt((i+1)).toUpperCase();
			i++;  // skip uppercased letter	
		}
		else{
			nsWord+=osWord.charAt(i).toLowerCase();
		}
	}
	return nsWord;
}

function Format_TextArea(sString){
    var d=sString,iSpaceCount = 0, sTextArea="";
    for(var i=0;i<d.length;i++){
        if(d.charAt(i) == "\n"){
	        sTextArea+="<br>";
	        //sTextArea+=d.charAt(i);
        }
        else if(d.charAt(i) == " "){
	        if(iSpaceCount == 2){
		        sTextArea+="&nbsp;";
		        //sTextArea+=d.charAt(i);			
		        iSpaceCount = 0; // reset space counter
	        }
	        else{
		        sTextArea+=d.charAt(i);
		        if(d.charAt(i-1) == " " && i != 0){
			        iSpaceCount++;
		        }
	        }
        }
        else{
	        sTextArea+=d.charAt(i);
        }
    }
    return sTextArea;
}

