var requiredMsg = null;
var errors = false;

function fnValidateFields(){ 
	// Make quick references to our fields 
	//*Password	
	var firstname = document.getElementById('first_name');
	
	var lastname = document.getElementById('last_name');
	var email = document.getElementById('email');
	var company = document.getElementById('company');

	var passwordOne = document.getElementById('password');
	var passwordTwo = document.getElementById('passwordConfirm');

	var selectcountry=document.getElementById('countrySelect');
	var selectState=document.getElementById('state');

	var otherState=document.getElementById('stateOther');

	var phone=document.getElementById('phone');

		 requiredMsg = "The following fields are required: \n\n";
	
	// Check each input in the order that it appears in the form!
       errors = false;
	
	
	validateValue(firstname, " ", "First Name");
	validateValue(lastname, " ", "Last Name");
	validateEmail(email, " ", "Email");
	validateValue(company, " ", "Company Name");
	

	validatePasswordMatch(passwordOne,passwordTwo,'Password and Confirm Password mismatch')
	
	validateSelection(selectcountry.options.selectedIndex,"Select Country");
	if(selectcountry.options.selectedIndex ==1)
	 {
		validateSelection(selectState.options.selectedIndex,"Select State");
	 }
	 if(selectcountry.options.selectedIndex !=1 && selectcountry.options.selectedIndex !=0)
		{
			validateValue(otherState, "*State/Province", "State Name");
		}

	  validateValue(phone, " ", "Phone Number");
	  requiredMsg += "\nPlease correct and try again."

        if (errors) {
	   alert(requiredMsg);
           return false; 
        } else { 
           return true; 
        }

           
}


function validateValue(elem, defaultText, helperMsg){
	if(elem.value.length == 0 || elem.value == defaultText){
           requiredMsg += "   " + helperMsg + "\n";
           errors = true;
	}
}

function validatePasswordMatch(passwordOne,passwordTwo,helperMsg){
	if((passwordOne.value.length == 0 && passwordTwo.value.length == 0)|| (passwordOne.value == '*Password' || passwordTwo.value == '*Confirm Password')
		|| (passwordOne.value!= passwordTwo.value)){
           requiredMsg += "   " + helperMsg + "\n";
           errors = true;
	}
}


function validateSelection(elem, helperMsg){
	if(elem == 0){
           requiredMsg += "   " + helperMsg + "\n";
           errors = true;
	}
}

function validatePhone(elem, defaultText, helperMsg){
	var phoneExp = /^[0-9\.\-\*\+]+$/;
	if(elem.value != defaultText && !elem.value.match(phoneExp)){
           requiredMsg += "   " + helperMsg + "\n";
           errors = true;
	}
}

function validateEmail(elem, defaultText, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value != defaultText && !elem.value.match(emailExp)){
           requiredMsg += "   " + helperMsg + "\n";
           errors = true;
	}
}


function clearDefault(thisfield, defaulttext) {
         if (thisfield.value == defaulttext) {
            thisfield.value = "";
         }
}
function StateMenus()
	{ 
		if(document.make_contact.country.value =='US')
			{ 	
			//alert("us state - " + document.make_contact.country.value);
			document.getElementById('selectStateOther').style.display='none';
			document.getElementById('selectState').style.display='block';
			}
		else 
			{
			//alert("non us state - " + document.make_contact.country.value);
			document.getElementById('selectState').style.display='none';
			document.getElementById('selectStateOther').style.display='block';
			}
	}

	function clearText(thefield){
		thefield.style.backgroundColor = 'white';

		if (thefield.defaultValue==thefield.value){
			thefield.value = "";
		}	
	} 
