        // Removes leading whitespaces
    function LTrim( value ) 
    {    	
    	var re = /\s*((\S+\s*)*)/;
    	return value.replace(re, "$1");    	
    }
    
    // Removes ending whitespaces
    function RTrim( value ) 
    {    	
    	var re = /((\s*\S+)*)\s*/;
    	return value.replace(re, "$1");    	
    }
    
    // Removes leading and ending whitespaces
    function trim( value ) 
    {    	
    	return LTrim(RTrim(value));    	
    }
    
    function ValidateContactUs()
    {
        var retValue = true;
        var curValue = true;
        
        ValidatePhoneNumbers('phone1', 'phone2', 'phone3', 'phone');
        
        curValue = Validate('first_name','errorFirstName','NAME','First name is required.');
        retValue = curValue && retValue;
        
        curValue = Validate('last_name','errorLastName','NAME','First name is required.');
        retValue = curValue && retValue;
        
        curValue = Validate('email','errorEmail','EMAIL','Email is required.');
        retValue = curValue && retValue;
        
        curValue = Validate('phone','errorPhone','PHONE','Phone number is required.');
        retValue = curValue && retValue;

        curValue = Validate('subject','errorSubject','','Comment/Question is required.');
        retValue = curValue && retValue;  
 
        return retValue;        
    }
    
    function ValidateLinkExchange()
    {
        var retValue = true;
        var curValue = true;
        
        ValidatePhoneNumbers('phone1', 'phone2', 'phone3', 'phone');
        
        curValue = Validate('first_name','errorFistName','NAME','First name is required.');
        retValue = curValue && retValue;
        
        curValue = Validate('last_name','errorLastName','NAME','First name is required.');
        retValue = curValue && retValue;
        
        curValue = Validate('email','errorEmail','EMAIL','Email is required.');
        retValue = curValue && retValue;
        
        curValue = Validate('phone','errorPhone','PHONE','Phone number is required.');
        retValue = curValue && retValue;
        
        curValue = Validate('title','errorTitle','TEXT','Title is required.');
        retValue = curValue && retValue;        

        curValue = Validate('url','errorUrl','','Url number is required.');
        retValue = curValue && retValue;  
        
        curValue = Validate('description','errorDescription', 'TEXT', 'You must enter description.');
        retValue = curValue && retValue;                 
        
        return retValue;
    }
    
    function ValidateInput(textboxId, labelId)
    {
        var text = document.getElementById(textboxId).value;
        if(trim(text) == "")
        {
            document.getElementById(labelId).style.color = "red";
            return false;
        }else
        {
            document.getElementById(labelId).style.color = "black";
            return true;
        }
    }
    
    function GetControlValueByID(id)
    {
        var value = document.getElementById(id).value;
        return value;
    }
    
    function Validate(controlIdToValidate, errorSpanId, validationType, optionalMessage)
    {
        var credit_regex = new RegExp(/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/);
        var email_regex 		= new RegExp(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
		var name_regex 			= new RegExp(/^([a-zA-Z\'])+$/);
		var phone_regex 		= new RegExp(/^[2-9]\d{2}-\d{3}-\d{4}$/);
		var decimal_regex 		= new RegExp(/^\d+(\.\d{1,2})?$/);
        var alphaNumeric_regex  = new RegExp(/^[0-9a-zA-Z\s]+$/);
        var zipCode_regex       = new RegExp(/^[0-9]{5}(?:-[0-9]{4})?$/);
        var number_regex        = new RegExp(/^\d+$/);      

        if(document.getElementById(controlIdToValidate) == null)
        {
            return true;
        }
        
        if(document.getElementById(errorSpanId) == null)
        {
            alert('Control ID does not exist: ' + errorSpanId);
            return true;
        }
        
        var text = document.getElementById(controlIdToValidate).value;
        var valid = false;
		
        if(trim(optionalMessage) == "")
        {
            optionalMessage = "Missing or invalid information.";
        }
        
        
        switch(validationType)
        {
            case "NAME":
            valid = name_regex.test(text)
            if(valid == false || trim(text) == "")
            {
                document.getElementById(errorSpanId).style.display = "";
                document.getElementById(errorSpanId).innerHTML = optionalMessage;                
            }else
            {
                document.getElementById(errorSpanId).style.display = "none";
            }
            break;
            case "PHONE":
            valid = phone_regex.test(text)
            if(valid == false || trim(text) == "")
            {
                document.getElementById(errorSpanId).style.display = "";
                document.getElementById(errorSpanId).innerHTML = optionalMessage                
            }else
            {
                document.getElementById(errorSpanId).style.display = "none";
            }
            break;
            case "EMAIL":
            valid = email_regex.test(text)
            if(valid == false || trim(text) == "")
            {
                document.getElementById(errorSpanId).style.display = "";
                document.getElementById(errorSpanId).innerHTML = optionalMessage                
            }else
            {
                document.getElementById(errorSpanId).style.display = "none";
            }
            break;
            case "TEXT":
            valid = alphaNumeric_regex.test(text)
            if(valid == false || trim(text) == "")
            {
                document.getElementById(errorSpanId).style.display = "";
                document.getElementById(errorSpanId).innerHTML = optionalMessage                
            }else
            {
                document.getElementById(errorSpanId).style.display = "none";
            }
            break;
            case "ZIP":
            valid = zipCode_regex.test(text)
            if(valid == false || trim(text) == "")
            {
                document.getElementById(errorSpanId).style.display = "";
                document.getElementById(errorSpanId).innerHTML = optionalMessage                
            }else
            {
                document.getElementById(errorSpanId).style.display = "none";
            }
            break;
            case "CREDIT":
            valid = credit_regex.test(text);
            if(valid == false || trim(text) == "")
            {
                document.getElementById(errorSpanId).style.display = "";
                document.getElementById(errorSpanId).innerHTML = optionalMessage                
            }else
            {
                document.getElementById(errorSpanId).style.display = "none";
            }            
            break;  
            case "NUMBER":
            valid = number_regex.test(text);
            if(valid == false || trim(text) == "")
            {
                document.getElementById(errorSpanId).style.display = "";
                document.getElementById(errorSpanId).innerHTML = optionalMessage                
            }else
            {
                document.getElementById(errorSpanId).style.display = "none";
            }            
            break;             
            default:
            if(trim(text) == "")
                valid = false;
            else
                valid = true;
                
            if(valid == false)
            {
                document.getElementById(errorSpanId).style.display = "";
                document.getElementById(errorSpanId).innerHTML = optionalMessage                
            }else
            {
                document.getElementById(errorSpanId).style.display = "none";
            }                 
            break;          
        }
        
        return valid;
    }
    
    
        
    function submitChangePassword()
    {
        var form = document.getElementById("changePassword");
        var currPassword = document.getElementById("currentPassword").value;
        var newPassword = document.getElementById("newPassword").value;
        var confirmPassword = document.getElementById("confirmPassword").value;
		var confirmSpan = document.getElementById("confirmPasswordError");
		if(trim(currPassword) == "")
		{
			confirmSpan.style.display = "";
			confirmSpan.innerHTML = "Current password is required."
			return;
		}

        var valid = validatePassword(newPassword, {
			length:   [6, 14],
			alpha:    1,
			badWords: ["password", "password1"]
		});		
						 
		if(valid)
		{
			if(newPassword != confirmPassword)
			{
				confirmSpan.style.display = "";
                confirmSpan.innerHTML = "New and confirm password are not the same.";
				return;
			}
			if(newPassword == currPassword)
			{
				confirmSpan.style.display = "";
                confirmSpan.innerHTML = "New and current password should not be the same.";
				return;
			}			
			form.submit();
		} 
		else 
		{
			confirmSpan.style.display = "";
            confirmSpan.innerHTML = "Invalid password. Please try a new one.";
			return;			
		}
    }
    
    function validatePassword (pw, options) 
	{
		// default options (allows any password)
		var o = {
			lower:    0,
			upper:    0,
			alpha:    0, /* lower + upper */
			numeric:  0,
			special:  0,
			length:   [0, Infinity],
			custom:   [ /* regexes and/or functions */ ],
			badWords: [],
			badSequenceLength: 0,
			noQwertySequences: false,
			noSequential:      false
		};
	
		for (var property in options)
			o[property] = options[property];
	
		var	re = {
				lower:   /[a-z]/g,
				upper:   /[A-Z]/g,
				alpha:   /[A-Z]/gi,
				numeric: /[0-9]/g,
				special: /[\W_]/g
			},
			rule, i;
	
		// enforce min/max length
		if (pw.length < o.length[0] || pw.length > o.length[1])
			return false;
	
		// enforce lower/upper/alpha/numeric/special rules
		for (rule in re) {
			if ((pw.match(re[rule]) || []).length < o[rule])
				return false;
		}
	
		// enforce word ban (case insensitive)
		for (i = 0; i < o.badWords.length; i++) {
			if (pw.toLowerCase().indexOf(o.badWords[i].toLowerCase()) > -1)
				return false;
		}
	
		// enforce the no sequential, identical characters rule
		if (o.noSequential && /([\S\s])\1/.test(pw))
			return false;
	
		// enforce alphanumeric/qwerty sequence ban rules
		if (o.badSequenceLength) {
			var	lower   = "abcdefghijklmnopqrstuvwxyz",
				upper   = lower.toUpperCase(),
				numbers = "0123456789",
				qwerty  = "qwertyuiopasdfghjklzxcvbnm",
				start   = o.badSequenceLength - 1,
				seq     = "_" + pw.slice(0, start);
			for (i = start; i < pw.length; i++) {
				seq = seq.slice(1) + pw.charAt(i);
				if (
					lower.indexOf(seq)   > -1 ||
					upper.indexOf(seq)   > -1 ||
					numbers.indexOf(seq) > -1 ||
					(o.noQwertySequences && qwerty.indexOf(seq) > -1)
				) {
					return false;
				}
			}
		}
	
		// enforce custom regex/function rules
		for (i = 0; i < o.custom.length; i++) {
			rule = o.custom[i];
			if (rule instanceof RegExp) {
				if (!rule.test(pw))
					return false;
			} else if (rule instanceof Function) {
				if (!rule(pw))
					return false;
			}
		}
	
		// great success!
		return true;
	}
