var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

/* ********************************************
 * IsEmailValid field
 *
 ********************************************** */
function isEmailValid(field){

var bValid  = true;

// make sure it is a string
var strEmailAddress = new String( field.value );

var atSym	= strEmailAddress.indexOf('@')
var atSym2	= strEmailAddress.indexOf('@', atSym + 1 )
var period	= strEmailAddress.lastIndexOf('.')
var space	= strEmailAddress.indexOf(' ')
var length	= strEmailAddress.length

if (	( atSym < 1 )			||	// '@' cannot be in first position
		( period <= atSym+1 )	||	// Must be at least one valid char btwn '@' and '.'
		( period == length-1 )	||	// Must be at least one valid char after '.'
		( space  != -1 )		||	// No empty spaces permitted
		( atSym2 != -1 ) )			//Check for more than one @      
   {  
		bValid = false;
   }
	if(!bValid){
		//this.errors.push(field.name);
		//this.dataOK = false;
		//this.errorMsg += "The email address you specified is invalid.n\n";
		return true;
	} else {
	    return false;
	}
}

function isInternationalPhoneNumber(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
    
    function RTrim(VALUE)
    {
        var w_space = String.fromCharCode(32);
        var v_length = VALUE.length;
        var strTemp = '';
        if(v_length < 0)
        {
            return'';
        }
        var iTemp = v_length -1;
    
        while(iTemp > -1)
        {
            if(VALUE.charAt(iTemp) == w_space)
            {
            }
            else
            {
                strTemp = VALUE.substring(0,iTemp +1);
                break;
            }
            iTemp = iTemp-1;
        } //End While
        return strTemp;
    } //End Function
    
    function LTrim(VALUE)
    {
        var w_space = String.fromCharCode(32);
        if(v_length < 1)
        {
            return'';
        }
        var v_length = VALUE.length;
        var strTemp = '';
    
        var iTemp = 0;
    
        while(iTemp < v_length)
        {
            if(VALUE.charAt(iTemp) == w_space)
            {
            }
            else
            {
                strTemp = VALUE.substring(iTemp,v_length);
                break;
            }
            iTemp = iTemp + 1;
        } //End While
        return strTemp;
    } //End Function


function isNull(str_Name) {
    // checks a given select list to ensure it is not empty
    
    if(str_Name.length < 1)
    {
        return true;
    }
    str_Name = RTrim(str_Name);
    str_Name = LTrim(str_Name);
    if(str_Name=='')
    {
        return true;
    }
    else
    {
        return false;
    }

}

function addMessage(str_Msg)  {

	//  Adds messages to the array.   Appends it.
		this.astr_Errors[this.astr_Errors.length] = str_Msg;	// Format the message
		this.hasErrors = true;					// Record that there is an error in this object
		return true;
}

function setErrorMsgDiv(strDiv) {

// Displays the error messages.  If there weren't any false is returned and no message is displayed.

if (this.hasErrors == false)   {
        document.getElementById(strDiv).innerHTML = '';
        return false;
} else {
        var str_msg = '<UL>';

        for (var i = 0; i < this.astr_Errors.length; i ++) 
		    str_msg += '<LI>' + this.astr_Errors[i] + '</LI>';
		
        str_msg += '</UL>';     
        
        document.getElementById(strDiv).innerHTML = str_msg;
                
    return true;
}
}

function ErrorMsg()
{
	//  This is the object you create to keep track of errors in the document

	this.astr_Errors = new Array();		//  Creates an array of error strings
	this.hasErrors = false;		//  Tells us there are currently no errors in this form.

	this.addMessage = addMessage;
	this.setErrorMsgDiv = setErrorMsgDiv;
	
	// General Validation
	this.isNull = isNull;
    this.isInternationalPhoneNumber = isInternationalPhoneNumber;
    this.isEmailValid = isEmailValid;
}

//Use in special case where brand input must be displayed (i.e iconestates, northlakewines, cwines)

function displayBrand() 

	{
document.getElementById("BrandInputComment").innerHTML = "Brand*: <br> <input  name='Brand' value=''>";
	 
document.getElementById("BrandInputComplaint").innerHTML = "Brand*: <br> <input  name='Brand' value=''>";
	}



