// Copy the text between this line and the ***** line exactly
var prevCountry;
function saveCountry(elementID) 
{
    prevCountry = document.getElementById(elementID).value;
}

// Copy the text between this line and the ***** line exactly
function setState(elementID) {
    //
    // don't do anything if the user is selecting the same country (US or CA)
    // again
    //
	if( ( document.getElementById('country').value == 'United States' && ( prevCountry == 'United States' || prevCountry == 'Canada' )) ||
	    ( document.getElementById('country').value == 'Canada'        && ( prevCountry == 'Canada'        || prevCountry == 'United States' )) )
    {
        return;
    }


	if( document.getElementById('country').value != 'United States' && document.getElementById('country').value != 'Canada')
    { 
        document.getElementById(elementID).innerHTML = stateField;
    } 
    else 
    {
        document.getElementById(elementID).innerHTML = stateList;
    }
}

function isBlank(text) {
	for(var i = 0; i < text.length; ++i) {
		var c = text.charAt(i);
	}
}

function verifyText(text) {
	if(text == null || text == " " || text == "")
	   return false;
  else
	   return true;
}

function isSetRadioButton(buttons) {
    var checked = false;
    for(var i=0; i < buttons.length; ++i) {        
        if(buttons[i].checked)
            checked = buttons[i].value;
    }       
    return checked;
}

function isSet(element) {
    switch(element.type.toLowerCase()) {    
        case "text" :
            return verifyText(element.value);
            break;
        case "textarea" :            
            return verifyText(element.value);
            break;
        case "select-one" :
            if(element.value == "- Select One -" || element.value == "- Please Select -")
                return false;
            else
                return true;
            break;
        case "radio" :
            var checked = false;
            for(var m=0; m<element.length; m++) {
                if(element[m].checked)
                    checked = true;
            }
            if(!checked) // add error
                return false;
            break;
    }
}

function isEmail(value) {
    p=value.indexOf('@');
    if (p<1 || p==(value.length-1)) 
        return false;
    return true;
}

function isSetEmail(element) {
    if(verifyText(element.value) && isEmail(element.value))
        return true;
    return false;   
}

/************************************************************************/


// Modify the code in this function to check each form name element that is required.
// Any Radio button fields must use isSetRadioButton, text and textarea elements should
// use isSet.
function bidVerify(form) {
	var elementErrorString = new Array();
	var errorNumber = 0;
	
	
	// Check each element that you want to verify
	
	//if(!isSet(form["messageSubject"]))
	 //  elementErrorString.push("Please select a product you are interested in.");	   
	
	if(!isSet(form["firstname"]))
	   elementErrorString.push("Please enter your fitrst name.");
	if(!isSet(form["lastname"]))
	   elementErrorString.push("Please enter your last name.");	     
	if(!isSet(form["company"]))
	   elementErrorString.push("Please enter the company name.");
	if(!isSet(form["phone"]))
	   elementErrorString.push("Please enter a phone number.");
	if(!isSetEmail(form["submittedBy"]))
	   elementErrorString.push("Please enter a valid email address.");	      
	if(!isSet(form["address"]))
	   elementErrorString.push("Please enter an address.");	   
	if(!isSet(form["city"]))
	   elementErrorString.push("Please enter a city.");	   
	if(!isSet(form["state"]))
	   elementErrorString.push("Please enter a state.");	   
	if(!isSet(form["zip"]))
	   elementErrorString.push("Please enter a postal code.");	   	   
	if(!isSet(form["country"]))
	   elementErrorString.push("Please select a country.");
	  
	if(!isSet(form["security_code"]))
	   elementErrorString.push("Please enter Security Code.");
	   
	   



	
	/** Do NOT MODIFY THE CODE BELOW **/
    if(elementErrorString.length > 0) {
	   	var alertString = "The following required ";
		if(elementErrorString.length > 1)
			alertString += "fields are incomplete :\n ";
		else {
			alertString += "field is incomplete :\n ";
  			alertString += "\n";
  		}
		for(var j = 0; j < elementErrorString.length; ++j) {
            alertString += elementErrorString[j] + "\n";
		}
		alertString += "\n";
		alert(alertString);
		return false;
  	}
  	else
		return true;
	   
}
