﻿// ========================================
//            TRIM FUNCTIONS
// ========================================
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

var myShareValidator = new ShareValidator();

// ========================================
//         VALIDATOR CONSTRUCTOR
// ========================================

function ShareValidator()
{
	this.validateRegistration = validateRegistration;
	this.validateName = validateName;
	this.validateRecName = validateRecName;
	this.validateEmailAddress = validateEmailAddress;
	this.validateRecEmailAddress = validateRecEmailAddress;
	

// ========================================
//        FIELD VALIDATION FUNCTIONS
// ========================================

    // If test evaluates to true, return 0, else return 1
    // Used to facilitate counting boolean input validation functions
    function addError(test)
    {
        if (test) 
            return 0;
        else
            return 1;
    }
    
    function validateRegistration()
    {
        var totalErrors = 0;
        totalErrors += addError(myShareValidator.validateName(d("YourName")));
        totalErrors += addError(myShareValidator.validateRecName(d("FriendsName")));
        totalErrors += addError(myShareValidator.validateEmailAddress(d("YourEmail")));
        totalErrors += addError(myShareValidator.validateRecEmailAddress(d("FriendsEmail")));
        
        //totalErrors += addError(myShareValidator.checkGender(d("gender")));

        if (totalErrors == 0)
            return true;        // Everything validated
        else
            return false;       // One or more didn't validate
    }
   
	function validateName(myField)
	{
		var myValue = getValue(myField);
		if ( (isNotNull(myValue)) && 
			 (hasNoDigits(myValue)) && 
			 (hasNoCrLf(myValue)) &&  
			 (hasNoFicoChars(myValue)) && 
			 (hasNoFicoDelimeter(myValue)) &&
			 (notATestWord(myValue)) &&
			 (notTripleRepeat(myValue)) &&
			 (minLength(myValue,2)) && 
			 (maxLength(myValue,15))   )
		{
            clearError(myField);
			return true;
		} else {
			throwError(myField,"*");
			return false;
		}
	}

	function validateRecName(myField)
	{
		var myValue = getValue(myField);
		if ( (isNotNull(myValue)) && 
			 (hasNoCrLf(myValue)) &&  
			 (hasNoDigits(myValue)) && 
			 (hasNoFicoChars(myValue)) &&
			 (hasNoFicoDelimeter(myValue)) &&
			 (notTripleRepeat(myValue)) &&
			 (maxLength(myValue,20)) && 
			 (minLength(myValue,2))  )
		{
            clearError(myField);
			return true;
		} else {
			throwError(myField,"*");
			return false;
		}
	}

	function validateEmailAddress(myField)
	{
		var myValue = getValue(myField);
		if ( (isValidEmailAddress(myValue)) && 
			 (hasNoSpaces(myValue)) && 
			 (hasNoCrLf(myValue)) )
		{
            clearError(myField);
			return true;
		} else {
			throwError(myField,"*");
			return false;
		}
	}
	
	function validateRecEmailAddress(myField)
	{
		var myValue = getValue(myField);
		if ( (isValidEmailAddress(myValue)) && 
			 (hasNoSpaces(myValue)) && 
			 (hasNoCrLf(myValue)) )
		{
            clearError(myField);
			return true;
		} else {
			throwError(myField,"*");
			return false;
		}
	}

	

// ========================================
//        ERROR HANDLING/CLEARING
// ========================================

	function throwError(myField, myMessage)
	{
	    // Clear out previous error
	    if (d(myField.id + "Error"))
	    {
	        var el = d(myField.id + "Error");
	        el.innerHTML = "";
	    }
	    
	    // Display myMessage
	    if ((myMessage) && (myMessage.length > 0) && (d(myField.id + "Error")))
	    {
	        var el = d(myField.id + "Error");
            el.innerHTML = "<br />" + myMessage;
        }
	}
	
	function clearError(myField)
	{
	    // Sending null message to throwError() will just clear error
		throwError(myField, null);
	}

// ========================================
//        BASE  VALIDATOR FUNCTIONS
// ========================================

	function getValue(myField)
	{
		var myValue = '';
		switch(myField.type)
		{
			case "checkbox" :
				if (myField.checked) 
					myValue = myField.value;
				break;
			case "radio" :
				if (myField.checked) 
					myValue = myField.value;
                break;
			case "select-one" :
				var si = myField.selectedIndex;
				if (si >= 0)
					myValue = myField.options[si].value;
				break;
			case "button" :
			case "file" :
			case "hidden" :
			case "password" :
            case "reset" :
			case "select-multiple" :
			case "text" :
			case "textarea" :
			default : 
				myValue = myField.value;
				break;
		}
		return myValue;
	}
	
	function isNotNull(myText)
	{
		if ( (myText.length > 0) && (trim(myText).length > 0) ) {
			return true;
		} else {
			// alert("Not Null!");
			return false;
		}
	}

	function isNumeric(myText)
	{
		var charpos = myText.search("[^0-9]"); 
		if(myText.length > 0 && charpos >= 0) 
		{		  
			// alert("Non-Numeric character at position " + eval(charpos + 1)); 
			return false; 
		}
		return true;
	}

	function isAlpha(myText)
	{
		var charpos = myText.search("[^A-Za-z]"); 
		if(myText.length > 0 && charpos >= 0) 
		{		  
			// alert("Non-Alpha character at position " + eval(charpos + 1)); 
			return false; 
		}
		return true;
	}

	function isAlphaNumeric(myText)
	{
		var charpos = myText.search("[^A-Za-z0-9]"); 
		if(myText.length > 0 && charpos >= 0) 
		{		  
			// alert("Non-AlphaNumeric character at position " + eval(charpos + 1)); 
			return false; 
		}
		return true;
	}

	function hasNoDigits(myText)
	{
		var charpos = myText.search("[0-9]"); 
		if(myText.length > 0 && charpos >= 0) 
		{		  
			// alert("Numeric character at position " + eval(charpos + 1)); 
			return false; 
		}
		return true;
	}

	function hasNoSpaces(myText)
	{
		var charpos = myText.search(/\s/); 
		if(myText.length > 0 && charpos >= 0) 
		{		  
			// alert("Space at position " + eval(charpos + 1)); 
			return false; 
		}
		return true;
	}

	function hasNoCrLf(myText)
	{
		var charpos = myText.search(/\r\n/); 
		if(myText.length > 0 && charpos >= 0) 
		{		  
			// alert("Carriage Return or Line Feed at position " + eval(charpos + 1)); 
			return false; 
		}
		return true;
	}

	function notATestWord(myText)
	{
		var regTest = /^(TEST|TESTING|FIRST)$/i ;
		if (regTest.test(myText))
		{
			// alert("Name is a Test Word. Please correct and submit again.");
			return false;
		}
		return true;
	}
	
	function notTripleRepeat(myText)
	{
		var charpos = myText.search(/^([0-9a-zA-Z])\1\1/i); 
		if(myText.length > 0 && charpos >= 0) 
		{		  
			// alert("Triple Alpha Character " + myText.charAt(charpos) + " at position " + eval(charpos + 1)); 
			return false; 
		}
		return true;
	}
	
	function hasNoFicoDelimeter(myText)
	{
		var charpos = myText.search(/{/); 
		if(myText.length > 0 && charpos >= 0) 
		{		  
			// alert("FICO Delimeter Character " + myText.charAt(charpos) + " at position " + eval(charpos + 1)); 
			return false; 
		}
		return true;
	}
	
	function hasNoFicoChars(myText)
	{
		var charpos = myText.search(/@|#|\$|%|\*|\^|!|{|}|\[|\]|\?|\+|-|=|\\|\/|,|\.|\"/); 
		if(myText.length > 0 && charpos >= 0) 
		{		  
			// alert("Invalid Character " + myText.charAt(charpos) + " at position " + eval(charpos + 1)); 
			return false; 
		}
		return true;
	}
	
	function maxLength(myText,myMaxLength)
	{
		if (myText.length > myMaxLength)
		{
			// alert("More than " + myMaxLength + "!");
			return false;
		} else {
			return true;
		}
	}

	function minLength(myText,myMinLength)
	{
		if (myText.length < myMinLength)
		{
			// alert("Less than " + myMinLength + "!");
			return false;
		} else {
			return true;
		}
	}


	function isValidEmailAddress(myText)
	{
		var regEmail = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		var regex = new RegExp(regEmail);
		if (!regex.test(myText))
		{
			// alert("E-mail Address Is Not Valid");
			return false;
		}
		return true;
	}

}

function chkValidation()
{
    var Registration = myShareValidator.validateRegistration() 
            if (!Registration)
            {
                error = true;
                closeDisplaysUtil('popup');
            }
            else 
            {
                // Clear out appData object
                this.appData = new AppData();
                //check which page to switch to
                //alert("consumer type: "+ consumer_value );
                openDisplayUtil_position();
                
            }
}

function d(fieldName){
    var x;
    return (x = document.getElementById(fieldName)) ? x : alert(fieldName + " not found");
}