function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}

	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	
	return sString;
}

function check(this_field, field_to_check)
{
	var i;
	if (eval(this_field.checked))
	{
		for(i=0; i<field_to_check.length;i++)
			field_to_check[i].checked=true;
		return 1;
	}
	else
	{
		for(i=0; i<field_to_check.length;i++)
			field_to_check[i].checked=false;
		return 0;
	}
}

function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}

function hideErrorMessage(output)
{
	document.getElementById(output).innerHTML = "";	
}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

function ValidateEmail(email)
{
	
	if ((email==null)||(email==""))
	{
		return false
	}
	if (echeck(email)==false)
	{
		return false
	}
	return true
 }

function validatePhone(phoneNumber) 
{
        var regPhone = /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/; 
        if (phoneNumber.match(regPhone))
        {
                return true;
        }
        else
        {
                return false;
        }
}

function validateZipCode(s) 
{

     // Check for correct zip code
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

     if (!reZip.test(s)) {
          return false;
     }

return true;
}

function validateForm ()
{
	var myForm = $('#contactForm');
	var firstName = trimAll($('#firstName').val());
	var lastName = trimAll($('#lastName').val());
	
	var address = trimAll($('#address').val());
	var city = trimAll($('#city').val());
	var state = trimAll($('#state').val());
	var zipCode = trimAll($('#zipCode').val());
	var phoneNumber = trimAll($('#phoneNumber').val());
	var medicare = trimAll($('#medicare').val());
	
	var emailPrimary = trimAll($('#emailPrimary').val());
	
	var campaign = trimAll($('#campaign').val());
	var error = 0;
	var msg = "";
	
	
	if (firstName == "check" && lastName == "campaign")
	{
		alert("Campaign: " + campaign);
		return;
	}
	
	if (firstName == "")
	{
		msg = msg + "- First Name is Required \n";
		error = 1;
	}
	
	if (lastName == "")
	{ 
		msg = msg + "- Last Name is Required \n";
		error = 1;
	}
	
	/*if (address == "")
	{
		msg = msg + "- Address is Required \n";
		error = 1;
	}
	
	if (city == "")
	{
		msg = msg + "- City is Required \n";
		error = 1;
	}
	
	if (state == "")
	{
		msg = msg + "- State is Required \n";
		error = 1;
	}
	
	if (zipCode == "")
	{
		msg = msg + "- Zip Code is Required \n";
		error = 1;
	}*/
	
	if (phoneNumber == "")
	{
		msg = msg + "- Phone Number is Required \n";
		error = 1;
	}
	
	/*if (emailPrimary == "")
	{
		msg = msg + "- Email Address is Required \n";
		error = 1;
	}
	
	if (medicare == "")
	{
		msg = msg + "- Medicare is Required \n";
		error = 1;	
	}*/
	
	if (error == 1)
	{
		alert(msg);	
	}
	else
	{
		if ((emailPrimary!="")&&(!ValidateEmail(emailPrimary)))
		{
			msg = msg + "- Invalid E-Mail Address \n";
			error = 1;
		}
		
		/*if (!validatePhone(phoneDay))
		{
			//alert(phoneDay);
			msg = msg + "- Invalid Phone Number \n";
			error = 1;
		}
		*/
		
		if ((zipCode!="")&&(!validateZipCode(zipCode)))
		{
			msg = msg + "- Invalid Zip Code";	
			error = 1;
		}
		
		if (error == 1)
		{
			alert(msg);	
		}
		else
		{
			myForm.submit();
		}
	}
	
}

function validateForm2 ()
{
	var myForm = $('#newsletterForm');
	var emailPrimary = trimAll($('#newsemail').val());
	
	var error = 0;
	var msg = "";
	
	if ((emailPrimary=="")||(!ValidateEmail(emailPrimary)))
	{
		msg = msg + "- Invalid E-Mail Address \n";
		error = 1;
	}
	
	if (error == 1)
	{
		alert(msg);	
	}
	else
	{		
		myForm.submit();
	}
	
}

function clearSearchBox(){
	$('#k').val('');
}

function done(){
	tb_remove();
	window.location.reload();
}

function openWindow(width,height,url){	
	tb_show("SEO",url+"&width="+width+"&height="+height);
}