function GetXmlHttpObject()
{
	var xmlHttp=null;

	try
	{
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttp;
}

function validateRegistration()
{

	if (document.getElementById('txt_user_email').value == "" || document.getElementById('txt_user_email').value == null)
	{
		alert("Your email field cannot be blank");
		document.getElementById('txt_user_email').focus();
		return false;
	}

	else if (!echeck(document.getElementById('txt_user_email').value))
	{
		alert("Please enter a valid email address");
		document.getElementById('txt_user_email').value = "";	
		document.getElementById('txt_user_email').focus();		
		return false;
	}	

	else if (document.getElementById('txt_user_member').value == "" || document.getElementById('txt_user_member').value == null)
	{
		alert("Your username field cannot be blank");
		document.getElementById('txt_user_member').focus();
		return false;
	}

	else if (document.getElementById('txt_pass_member').value == "" || document.getElementById('txt_pass_member').value == null)
	{
		alert("Your password field cannot be blank");
		document.getElementById('txt_pass_member').focus();
		return false;
	}

	else if (document.getElementById('txt_pass_member2').value == "" || document.getElementById('txt_pass_member2').value == null)
	{
		alert("Your confirmation password field cannot be blank");
		document.getElementById('txt_pass_member2').focus();
		return false;
	}


	else if (document.getElementById('txt_user_name').value == "" || document.getElementById('txt_user_name').value == null)
	{
		alert("Your first name field cannot be blank");
		document.getElementById('txt_user_name').focus();
		return false;
	}

	else if (document.getElementById('txt_user_last').value == "" || document.getElementById('txt_user_last').value == null)
	{
		alert("Your last name field cannot be blank");
		document.getElementById('txt_user_last').focus();
		return false;
	}

	else if (document.getElementById('chk_user_terms').checked == false)
	{
		alert("You must read and agree to the terms of use.");
		return false;
	}

	else
		return true;
}


function confirmPassword()
{
	if (document.getElementById('txt_pass_member').value != document.getElementById('txt_pass_member2').value)
	{
		document.getElementById('registration_submit').disabled = true;
		document.getElementById('txt_pass_member2').value = "";
		document.getElementById('txt_pass_member2').focus();
		document.getElementById('checkmatch').innerHTML = "Your Passwords Do Not Match";
	}
	else
	{
		document.getElementById('registration_submit').disabled = false;	
		document.getElementById('checkmatch').innerHTML = "";
	}
}


function validateVendorRegistration()
{
	if (document.getElementById('txt_vendor_email').value == "" || document.getElementById('txt_vendor_email').value == null)
	{
		alert("Your email field cannot be blank");
		document.getElementById('txt_vendor_email').focus();
		return false;
	}

	else if (!echeck(document.getElementById('txt_vendor_email').value))
	{
		alert("Please enter a valid email address");
		document.getElementById('txt_vendor_email').value = "";	
		document.getElementById('txt_vendor_email').focus();		
		return false;
	}	

	else if (document.getElementById('txt_user_vendor').value == "" || document.getElementById('txt_user_vendor').value == null)
	{
		alert("Your username field cannot be blank");
		document.getElementById('txt_user_vendor').focus();
		return false;
	}

	else if (document.getElementById('txt_pass_vendor').value == "" || document.getElementById('txt_pass_vendor').value == null)
	{
		alert("Your password field cannot be blank");
		document.getElementById('txt_pass_vendor').focus();
		return false;
	}

	else if (document.getElementById('txt_pass_vendor2').value == "" || document.getElementById('txt_pass_vendor2').value == null)
	{
		alert("Your confirmation password field cannot be blank");
		document.getElementById('txt_pass_vendor2').focus();
		return false;
	}

	else
		return true;
}


function confirmVendorPassword()
{
	if (document.getElementById('txt_pass_vendor').value != document.getElementById('txt_pass_vendor2').value)
	{
		document.getElementById('registration_submit').disabled = true;
		document.getElementById('txt_pass_vendor2').value = "";
		document.getElementById('txt_pass_vendor2').focus();
		document.getElementById('checkmatch').innerHTML = "Your Passwords Do Not Match";
	}
	else
	{
		document.getElementById('registration_submit').disabled = false;	
		document.getElementById('checkmatch').innerHTML = "";
	}
}


function validateContact()
{
	if (document.getElementById('txt_vendor_name').value == "" || document.getElementById('txt_vendor_name').value == null)
	{
		alert("Your company name field cannot be blank");
		document.getElementById('txt_vendor_name').focus();
		return false;
	}
	else if (document.getElementById('txt_vendor_contact').value == "" || document.getElementById('txt_vendor_contact').value == null)
	{
		alert("Your contact name field cannot be blank");
		document.getElementById('txt_vendor_contact').focus();
		return false;
	}	
	else if (document.getElementById('txt_vendor_address').value == "" || document.getElementById('txt_vendor_address').value == null)
	{
		alert("Your address field cannot be blank");
		document.getElementById('txt_vendor_address').focus();
		return false;
	}	

	else if (document.getElementById('txt_vendor_city').value == "" || document.getElementById('txt_vendor_city').value == null)
	{
		alert("Your city field cannot be blank");
		document.getElementById('txt_vendor_city').focus();
		return false;
	}	
	
	else if (document.getElementById('txt_user_state').options[document.getElementById('txt_user_state').selectedIndex].value == "99")
	{
		alert("You must select a state");
		return false;
	}

	else if (document.getElementById('txt_vendor_zip').value == "" || document.getElementById('txt_vendor_zip').value == null)
	{
		alert("Your zip field cannot be blank");
		document.getElementById('txt_vendor_zip').focus();
		return false;
	}	

	else if (document.getElementById('txt_vendor_phone').value == "" || document.getElementById('txt_vendor_phone').value == null)
	{
		alert("Your phone field cannot be blank");
		document.getElementById('txt_vendor_phone').focus();
		return false;
	}	





	else if (document.getElementById('txt_vendor_email').value == "" || document.getElementById('txt_vendor_email').value == null)
	{
		alert("Your email field cannot be blank");
		document.getElementById('txt_vendor_email').focus();
		return false;
	}

	else if (!echeck(document.getElementById('txt_vendor_email').value))
	{
		alert("Please enter a valid email address");
		document.getElementById('txt_vendor_email').value = "";	
		document.getElementById('txt_vendor_email').focus();		
		return false;
	}	
	else
		return true;
}



function validateMemberForgotPassword()
{
	if (document.getElementById('txt_email_member').value == "" || document.getElementById('txt_email_member').value == null)
	{
		alert("Your email field cannot be blank");
		document.getElementById('txt_email_member').focus();
		return false;
	}

	else if (!echeck(document.getElementById('txt_email_member').value))
	{
		alert("Please enter a valid email address");
		document.getElementById('txt_email_member').value = "";	
		document.getElementById('txt_email_member').focus();		
		return false;
	}	
	else
		return true;
}


function validateVendorForgotPassword()
{
	if (document.getElementById('txt_email_vendor').value == "" || document.getElementById('txt_email_vendor').value == null)
	{
		alert("Your email field cannot be blank");
		document.getElementById('txt_email_vendor').focus();
		return false;
	}

	else if (!echeck(document.getElementById('txt_email_vendor').value))
	{
		alert("Please enter a valid email address");
		document.getElementById('txt_email_vendor').value = "";	
		document.getElementById('txt_email_vendor').focus();		
		return false;
	}	
	else
		return true;
}

function validateComment()
{
	if (document.getElementById('ta_user_comment').value == "" || document.getElementById('ta_user_comment').value == null)
	{
		alert("Your comment field cannot be blank");
		document.getElementById('ta_user_comment').focus();
		return false;
	}
	else
		return true;
}


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 displayLocationInterface()
{
	myChoice = document.getElementById('txt_vendor_locations').options[document.getElementById('txt_vendor_locations').selectedIndex].value;



	myURL = "../SierraController.php?CMD=6&c=" + myChoice;
	
	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null)
	{
		alert("Browser does not support AJAX");
		return;
	} 

	xmlHttp.onreadystatechange = 
	function() 
	{
		if (xmlHttp.readyState == 4)
		{
	
			document.getElementById('myupload').innerHTML = xmlHttp.responseText;
			
			if (myChoice == "U")
				document.getElementById('myupload2').innerHTML = "";
			else if (myChoice == "M")			
				document.getElementById('myupload2').innerHTML = "<form id=\"registration_vendor\" method=\"post\" action=\"SierraController.php\"><fieldset><input type=\"hidden\" name=\"CMD\" value=\"10\" /><div class=\"row\"><input class=\"btn_continue\" type=\"submit\" name=\"registration_submit\" id=\"registration_submit\" value=\"\" /></div><!--end row--></fieldset></form>";
            else if (myChoise == "N")
                document.getElementById('myupload').innerHTML = "";
		}
	}

	xmlHttp.open("GET", myURL, true);
	xmlHttp.send(null);
}



function addVLocation(myVendor)
{
	var myCompanyName;
	var myAddy;
	var myAddy2;
	var myCity;
	var myState;
	var myZip;
	var myPhone;
	
	myCompanyName = escape(document.getElementById('txt_location_name').value);
	myAddy = escape(document.getElementById('txt_location_address').value);
	myAddy2 = escape(document.getElementById('txt_location_address2').value);
	myCity = escape(document.getElementById('txt_location_city').value);
	myState = escape(document.getElementById('txt_user_state').value);
	myZip = escape(document.getElementById('txt_location_zip').value);
	myPhone = escape(document.getElementById('txt_location_phone').value);


	if (myCompanyName == "" || myCompanyName == null)
	{
		alert("Company Name is required");
		document.getElementById('txt_location_name').focus();
		return;
	}
	
//	if (myAddy == "" || myAddy == null)
//	{
//		alert("Address Field is required");
//		document.getElementById('txt_location_address').focus();
//		return;
//	}
	
	if (myCity == "" || myCity == null)
	{
		alert("City Field is required");
		document.getElementById('txt_location_city').focus();
		return;
	}
	
	if (myState == "99")
	{
		alert("You must select a State");
		return;
	}
	
	if (myZip == "" || myZip == null)
	{
		alert("Zip Code Field is required");
		document.getElementById('txt_location_zip').focus();
		return;
	}
	
	/*
	if (myPhone == "" || myPhone == null)
	{
		alert("Phone Field is required");
		document.getElementById('txt_location_phone').focus();
		return;
	}
	*/
	myCompanyName = escape(myCompanyName);
							
	myURL = "../SierraController.php?CMD=7&v=" + myVendor + "&company=" + myCompanyName + "&addy1=" + myAddy + "&addy2=" + myAddy2 + "&city=" + myCity + "&state=" + myState + "&zip=" + myZip + "&phone=" + myPhone;
	
	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null)
	{
		alert("Browser does not support AJAX");
		return;
	} 
	
	xmlHttp.onreadystatechange = 
	function() 
	{
		if (xmlHttp.readyState == 4)
		{
			
		
			var resp = xmlHttp.responseText;
			
			if(resp == 'existing')
			{
				alert("You have already added this location!");
			}
			else if(resp == 'MAX')
			{
				alert("You have reached your maximum number of locations");
			}
			else
			{
				document.getElementById('txt_location_name').value = "";
				document.getElementById('txt_location_address').value = "";
				document.getElementById('txt_location_address2').value = "";
				document.getElementById('txt_location_city').value = "";
				document.getElementById('txt_location_zip').value = "";
				document.getElementById('txt_location_phone').value = "";
				document.getElementById('mynewlocations').innerHTML = resp;
			}
			
		}
	}

	xmlHttp.open("GET", myURL, true);
	xmlHttp.send(null);
}


function removeVLocation(myVendor)
{
	myURL = "../SierraController.php?CMD=8&v=" + myVendor;
	
	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null)
	{
		alert("Browser does not support AJAX");
		return;
	} 

	xmlHttp.onreadystatechange = 
	function() 
	{
		if (xmlHttp.readyState == 4)
		{
			document.getElementById('txt_location_name').value = "";
			document.getElementById('txt_location_address').value = "";
			document.getElementById('txt_location_address2').value = "";
			document.getElementById('txt_location_city').value = "";
			document.getElementById('txt_location_zip').value = "";
			document.getElementById('txt_location_phone').value = "";
		
			document.getElementById('mynewlocations').innerHTML = xmlHttp.responseText;
		}
	}

	xmlHttp.open("GET", myURL, true);
	xmlHttp.send(null);
}


function validateTOS()
{
	if (document.getElementById('chk_terms').checked == false)
	{
		alert("You must read and agree to the provider listing agreement");
		return false;
	}
	else
		return true;
}



function searchByType()
{

	if (document.getElementById('rad_vendor_type1').checked == true)
	{
		myURL = "../SierraController.php?CMD=25&type=R";
		
		document.getElementById('checkonlinemerch').innerHTML = "<p class=\"txt_center no_margin\"><input type=\"checkbox\" name=\"chk_search_online\" id=\"chk_search_online\" class=\"check\" value=\"1\" /><label for=\"chk_search_online\">Include online merchants in search results.</label></p>";
//		document.getElementById('checkonlinemerch').style.display = "block";
	}
	else if (document.getElementById('rad_vendor_type2').checked == true)
	{
		myURL = "../SierraController.php?CMD=25&type=S";
		
		document.getElementById('checkonlinemerch').innerHTML = "";
//		document.getElementById('checkonlinemerch').style.display = "none";
	}
	else
	{
		myURL = "../SierraController.php?CMD=25&type=R";

		document.getElementById('checkonlinemerch').innerHTML = "<p class=\"txt_center no_margin\"><input type=\"checkbox\" name=\"chk_search_online\" id=\"chk_search_online\" class=\"check\" /><label for=\"chk_search_online\">Include online merchants in search results.</label></p>";
//		document.getElementById('checkonlinemerch').style.display = "block";
	}
	
	
	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null)
	{
		alert("Browser does not support AJAX");
		return;
	} 

	xmlHttp.onreadystatechange = 
	function() 
	{
		if (xmlHttp.readyState == 4)
		{
			document.getElementById('dropdowntype').innerHTML = xmlHttp.responseText;
		}
	}

	xmlHttp.open("GET", myURL, true);
	xmlHttp.send(null);	
}


function OpenProviderSite(myVendor, myWebSite)
{
	myURL = "SierraController.php?CMD=32&v=" + myVendor;


	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null)
	{
		alert("Browser does not support AJAX");
		return;
	} 

	xmlHttp.onreadystatechange = 
	function() 
	{
		if (xmlHttp.readyState == 4)
		{
			window.open(myWebSite, "providersite");
		}
	}

	xmlHttp.open("GET", myURL, true);
	xmlHttp.send(null);

}
