var lastClickWasToExpand = "";
function getXMLHttp()
{
  var xmlHttp

  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}

function updateChatMessages(chatId)
{
	if (!objectExists("messages"))
	{
		return
	}
	
	var xmlHttp = getXMLHttp();
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			HandleResponse(xmlHttp.responseText, 1);
		}
	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=updateChatMessages" + "&chatId=" + chatId + "&random=" + escape(Math.random()), true); 
	xmlHttp.send(null);
}
function submitNewChatMessage(message)
{
  	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 2);
    	}
  	}
	
	chatId = thisChatId;
 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=submitNewChatMessage" + "&chatId=" + chatId +  "&message=" + encodeURIComponent(message) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}
function startNewChat(message, email, stateId, regionId, jobTypeId, contractorsIds)
{
	
  	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 3);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=startNewChat" +  "&message=" + encodeURIComponent(message) + "&email=" + encodeURIComponent(email) + "&stateId=" + encodeURIComponent(stateId) + "&regionId=" + encodeURIComponent(regionId) + "&jobTypeId=" + encodeURIComponent(jobTypeId) + "&contractorsIds=" + encodeURIComponent(contractorsIds) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	
}

function updateChatDetails(chatId)
{
	if (!objectExists("displayChatDetails"))
	{
		return
	}
  	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 4);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=updateChatDetails" + "&chatId=" + chatId + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}

	/*
function getRegionsInState()
{
	stateId = $$("state");
	document.getElementById("regionList" + currentStateId).style.display = "none";
	document.getElementById("regionsFor" + currentStateId).style.display = "none";	
	document.getElementById("regionList" + stateId).style.display = "inline";
	document.getElementById("regionsFor" + stateId).style.display = "inline";
	
	currentStateId = stateId;
	return;

	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 5);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=getRegions" + "&stateId=" + stateId + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	
	document.getElementById("regionList").innerHTML = "<strong>Region:</strong><em>loading...</em>";
}
*/
function searchForContractorsChangeUrl()
{
	
	var jobType = getTextOfSelectedOption("jobType");
	
	var state = getTextOfSelectedOption("state");
	
	var region = getTextOfSelectedOption("region" + stateId);
  
	window.location.href = '/search.php?jobType=' + jobType + "&state=" + state + "&region=" + region;
}
function searchForContractors()
{
	
	
	stateId = $$("state");
	regionId = $$("region" + stateId);
	jobTypeId = $$("jobType");
	elements = document.getElementById("orderDirection");

if (elements)
	{
		orderBy = $$("ordering");
		increasing = $$("orderDirection");
	}
	else
	{
		orderBy = 0;
		increasing = 0;
	}
	orderBy = orderBy * increasing;
	
	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 6);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=search" + "&stateId=" + stateId + "&regionId=" + regionId + "&jobTypeId=" + jobTypeId + "&ordering=" + orderBy + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}

function getUpdatedChatsProjectsDiv()
{
  	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 7);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=getUpdatedProjects" + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}

function changeChatsProjectAssignment(projectId)
{
	/*var selection = document.changeProjectAssignmentForm.projectSelection;
projectId = -1;
for (i=0; i<selection.length; i++)
{
  if (selection[i].selected == true)
	  projectId = selection[i].value;

}
if (projectId == -1)
	return;*/

		
	chatId = thisChatId;

		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 8);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=changeChatsProjectAssignment" +  "&chatId=" + escape(chatId) +  "&projectId=" + escape(projectId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}

function editProjectName(projectId, newProjectName)
{

		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 8);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=editProjectName" +  "&newProjectName=" + escape(newProjectName) +  "&projectId=" + escape(projectId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}
function removeFolder(folderId, whatToCallIt)
{
	var ans = confirm("Are you sure that you want to remove this " + whatToCallIt + "?");
	if (ans == false)
		return;

		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 8);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=removeFolder" +  "&folderId=" + escape(folderId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}
function addAProject(newProjectName)
{
	if (newProjectName == "")
	{
		alert('Oops. You must enter a name for this new project.');
		return;
	}
	
		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 8);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=addAProject" +  "&newProjectName=" + escape(newProjectName) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}

function checkForNewMessageAlerts(chatId)
{
			
		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 9);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=checkForNewMessageAlerts" +  "&chatId=" + escape(chatId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}
function emailLinkToResumeChat()
{
		chatId = thisChatId;
		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 10);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=emailLinkToResumeChat" +  "&chatId=" + escape(chatId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}

function pingAsAlive()
{
		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 11);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=pingAsAlive" + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}
function updateBidDiv()
{
	var listing = $$('listings');
	var listingMonths = $$('listingMonths');
	
	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 12);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=updateBidDiv" + "&listing=" + listing + "&listingMonths=" + listingMonths + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	
	//show it loading in case it takes awhile
	var divh = document.getElementById('bidsForCurrentListing').offsetHeight; 
	updateHtml("bidsForCurrentListing", "<h2 style='text-align:center;vertical-align:middle'>Loading...</h2>");
	document.getElementById('bidsForCurrentListing').style.height = divh +"px";
}
function updateBid()
{
	hideConfirmBid();
	var listing = $$('listings');
	var listingMonths = $$('listingMonths') ;
	var bidAmount = $$('bidAmount') * 1;
	var previousMaxBid = $$('previousMaxBid') * 1;
	var xmlHttp = getXMLHttp(); 
	
	/*if (document.getElementById('biddingTou').checked  == false)
	{	
		alert("You must accept the Bidding Terms of Use in order to place a bid");
		return
	}*/
	
	if (isNaN(bidAmount))
	{
		alert("Oops. You must enter a number for your bid amount!");
		document.getElementById('bidAmount').value = "";
		return
	}
	if (previousMaxBid < 0)
	{
		//this is there first bid.
		var minBid = $$('minBid') * 1;
		if (bidAmount < minBid)
		{
			alert("You must enter a bid amount greater than the min bid or greater than the lowest spot. Please enter a bid of at least $" + minBid);
			return;
		}
	}
	if (bidAmount <= previousMaxBid)
	{
		alert('You must enter a bid amount greater than your current max bid of $'+  previousMaxBid + '. You cannot lower your bid.');
		return;
	}
	if (bidAmount != Math.floor(bidAmount))
	{	
		alert('You must enter a dollar amount only. No cents allowed.');
		document.getElementById('bidAmount').value = Math.floor(bidAmount);		
		return;
	}
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 12);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=updateBid" + "&bidAmount=" + bidAmount + "&listing=" + listing + "&listingMonths=" + listingMonths + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	
	//show it loading in case it takes awhile
	var divh = document.getElementById('bidsForCurrentListing').offsetHeight; 
	updateHtml("bidsForCurrentListing", "<h2 style='text-align:center;vertical-align:middle'>Posting Your Bid...</h2>");
	document.getElementById('bidsForCurrentListing').style.height = divh +"px";

}


function contractorAddRegion(regionId)
{

		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 13);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=contractorAddRegion" +  "&regionId=" + escape(regionId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}
function contractorRemoveRegion(regionId)
{

		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 13);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=contractorRemoveRegion" +  "&regionId=" + escape(regionId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}
function contractorRemoveState(stateId)
{

		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 13.2);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=contractorRemoveState" +  "&stateId=" + escape(stateId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}

function contractorAddJobType(jobTypeId)
{

		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 13);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=contractorAddJobType" +  "&jobTypeId=" + escape(jobTypeId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}
function contractorRemoveJobType(jobTypeId)
{

		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 13);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=contractorRemoveJobType" +  "&jobTypeId=" + escape(jobTypeId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}

//UPDATE PROFILE
function contractorUpdateProfile()
{

	updateHtml('updatingText', 'Updating...');
	
	var newShortDesc = $$('shortDescription');
	var inBusinessSince = $$('inBusinessSince');
	var basedIn = $$('basedIn');
	obj = document.getElementById('givesFreeEstimates');
	if (obj.checked == true)
		var givesFreeEstimates = 1;
	else
		var givesFreeEstimates = 0;
		
	obj = document.getElementById('isLicensed');
	if (obj.checked == true)
		var isLicensed = 1;
	else
		var isLicensed = 0;
	
	obj = document.getElementById('isInsured');
	if (obj.checked == true)
		var isInsured = 1;
	else
		var isInsured = 0;
		
	var insuranceInformation = $$('insuranceInformation');
	var extendedDetails = $$('extendedDetails');
	
		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 14);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=contractorUpdateProfile" +  "&a=" + escape(newShortDesc) +  "&b=" + escape(inBusinessSince) + "&c=" + escape(basedIn) + "&d=" + escape(givesFreeEstimates) + "&e=" + escape(isLicensed) + "&f=" + escape(isInsured) + "&g=" + escape(insuranceInformation) + "&h=" + escape(extendedDetails) +"&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);	
}
function contractorUpdateProfile2()
{

	//updateHtml('updatingText', 'Updating...');
	
	var newShortDesc = $$('shortDescription');
	var firstName = $$('firstName');
	var lastName = $$('lastName');
	
	var inBusinessSince = $$('inBusinessSince');
	var basedIn = $$('basedIn');
	obj = document.getElementById('givesFreeEstimatesYes');
	if (obj.checked == true)
		var givesFreeEstimates = 1;
	else
		var givesFreeEstimates = 0;
		
	var estimateCosts = $$('estimatesCost');
	var estimateInfo = $$('estimateInfo');
	
		
	obj = document.getElementById('isInsured');
	if (obj.checked == true)
		var isInsured = 1;
	else
		var isInsured = 0;
	
	var insuranceInformation = $$('insuranceInformation');

	
	obj = document.getElementById('isLicensed');
	if (obj.checked == true)
		var isLicensed = 1;
	else
		var isLicensed = 0;	
	
	var licenseInfo = $$('licenseInfo');

	var specialty = $$('specialty');
	
	var address1 = $$('address1');
	var address2 = $$('address2');
	var city = $$('city');
	var state = $$('state');
	var zip = $$('zip');
	
	var businessPhone = $$('businessPhone');
	var mobilePhone = $$('mobilePhone');
	var businessEmail = $$('businessEmail');
	var websiteAddress = $$('websiteAddress');
	
	var highlight0 = $$('highlight0');
	var highlight1 = $$('highlight1');
	var highlight2 = $$('highlight2');
	var highlight3 = $$('highlight3');
	var highlight4 = $$('highlight4');
	var highlight5 = $$('highlight5');
	var highlight6 = $$('highlight6');
	var highlight7 = $$('highlight7');
	var highlight8 = $$('highlight8');
	var highlight9 = $$('highlight9');
	
	
	var highlights = highlight0 + ",*," + highlight1 + ",*," + highlight2 + ",*," + highlight3 + ",*," + highlight4 + ",*," + highlight5 + ",*," + highlight6 + ",*," + highlight7 + ",*," + highlight8 + ",*," + highlight9 + ",*,";
	
	var extendedDetails = $$('extendedDetails');
	
		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 22);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=contractorUpdateProfile2" +  "&a=" + escape(newShortDesc) +  "&b=" + escape(inBusinessSince) + "&c=" + escape(basedIn) + "&d=" + escape(givesFreeEstimates) + "&e=" + escape(isLicensed) + "&f=" + escape(isInsured) + "&g=" + escape(insuranceInformation) + "&h=" + escape(firstName) + "&i=" + escape(lastName) + "&j=" + escape(estimateCosts) + "&k=" + escape(licenseInfo) + "&l=" + escape(specialty) + "&m=" + escape(address1)  + "&n=" + escape(address2) + "&o=" + escape(city) + "&p=" + escape(state) + "&q=" + escape(zip) + "&r=" + escape(businessPhone) + "&s=" + escape(mobilePhone) + "&t=" + escape(businessEmail) + "&u=" + escape(websiteAddress) + "&v=" + escape(highlights) + "&w=" + escape(extendedDetails) + "&x=" + escape(estimateInfo)  +"&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);	
}
function realtorUpdateProfile()
{

	//updateHtml('updatingText', 'Updating...');
	
	var newShortDesc = $$('shortDescription');
	var firstName = $$('firstName');
	var lastName = $$('lastName');
	
	var inBusinessSince = $$('inBusinessSince');
	var basedIn = $$('basedIn');	
	
	//commission
	var commissionBuying = $$('commissionBuying');
	var commissionSelling = $$('commissionSelling');
	
	//availability
	obj = document.getElementById('availDays');
	if (obj.checked == true)
		var availDays = 1;
	else
		var availDays = 0;
	obj = document.getElementById('availEvenings');
	if (obj.checked == true)
		var availEvenings = 1;
	else
		var availEvenings = 0;
	obj = document.getElementById('availSat');
	if (obj.checked == true)
		var availSat = 1;
	else
		var availSat = 0;
	obj = document.getElementById('availSun');
	if (obj.checked == true)
		var availSun = 1;
	else
		var availSun = 0;
	var availInfo = $$('availInfo');	
	
	//credentials
	var languages = $$('languages');	
	var education = $$('education');	
	var background = $$('background');	
	var memberships = $$('memberships');	
	
	//licenses
	var licenseNumber = $$('licenseNumber');	
	
	//Services
	obj = document.getElementById('isBuying');
	if (obj.checked == true)
		var isBuying = 1;
	else
		var isBuying = 0;
	obj = document.getElementById('isSelling');
	if (obj.checked == true)
		var isSelling = 1;
	else
		var isSelling = 0;	
	var specialty = $$('specialty');
	
	//Alternate Contact Info	
	var address1 = $$('address1');
	var address2 = $$('address2');
	var city = $$('city');
	var state = $$('state');
	var zip = $$('zip');
	
	var businessPhone = $$('businessPhone');
	var mobilePhone = $$('mobilePhone');
	var businessEmail = $$('businessEmail');
	var websiteAddress = $$('websiteAddress');
	
	//About Me
	var highlight0 = $$('highlight0');
	var highlight1 = $$('highlight1');
	var highlight2 = $$('highlight2');
	var highlight3 = $$('highlight3');
	var highlight4 = $$('highlight4');
	var highlight5 = $$('highlight5');
	var highlight6 = $$('highlight6');
	var highlight7 = $$('highlight7');
	var highlight8 = $$('highlight8');
	var highlight9 = $$('highlight9');	
	
	var highlights = highlight0 + ",*," + highlight1 + ",*," + highlight2 + ",*," + highlight3 + ",*," + highlight4 + ",*," + highlight5 + ",*," + highlight6 + ",*," + highlight7 + ",*," + highlight8 + ",*," + highlight9 + ",*,";
	
	var extendedDetails = $$('extendedDetails');
	
		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 22);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=realtorUpdateProfile" +  "&a=" + escape(newShortDesc) +  "&b=" + escape(inBusinessSince) + "&c=" + escape(basedIn) + "&d=" + escape(commissionBuying) + "&e=" + escape(commissionSelling) + "&f=" + escape(availDays) + "&g=" + escape(availEvenings) + "&h=" + escape(firstName) + "&i=" + escape(lastName) + "&j=" + escape(availSat) + "&k=" + escape(availSun) + "&l=" + escape(languages) + "&m=" + escape(education)  + "&n=" + escape(background) + "&o=" + escape(memberships) + "&p=" + escape(licenseNumber) + "&q=" + escape(isBuying) + "&r=" + escape(isSelling) + "&s=" + escape(specialty) + "&t=" + escape(address1) + "&u=" + escape(address2) + "&v=" + escape(city) + "&w=" + escape(state) + "&x=" + escape(zip) + "&y=" + escape(businessPhone) + "&z=" + escape(mobilePhone) + "&aa=" + escape(businessEmail) + "&bb=" + escape(websiteAddress) + "&cc=" + escape(highlights) + "&dd=" + escape(extendedDetails) + "&ee=" + escape(availInfo) +"&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);	
}
function startNewHelpChat(message, email, topic)
{
  	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 15);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=startNewHelpChat" +  "&message=" + encodeURIComponent(message) + "&email=" + encodeURIComponent(email) + "&topic=" + encodeURIComponent(topic) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}
function closeChat(chatId)
{
  	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 16);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=closeChat" +  "&chatId=" + encodeURIComponent(chatId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}

function flagUser(chatId)
{
	var reason = $$('flagUserReason');
	
	
  	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 16);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=flagUser" +  "&chatId=" + encodeURIComponent(chatId) +  "&reason=" + encodeURIComponent(reason) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}

function setAwayStatus(bool)
{
	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 17);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=setAwayStatus" +  "&bool=" + encodeURIComponent(bool) +  "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}
function createNewAwayMessage()
{
	var message = $$('awayMessageText');
	var elem = document.getElementById('createNewAwayMessageButton');
	if (elem)
	{
			elem.value = 'Adding...';
			elem.disabled = true;
	}
	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 18);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=createNewAwayMessage" +  "&message=" + encodeURIComponent(message) +  "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}

function changeAwayMessage()
{
	var id = $$('currentAwayMessage');
	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 18);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=changeAwayMessage" +  "&id=" + encodeURIComponent(id) +  "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
}

function contractorUpdateCoupon(couponId)
{

	//updateHtml('updatingText', 'Updating...');
	
	var title = $$('title');
	var desc = $$('desc');
	var finePrint = $$('finePrint');
	
	var expirationDate = $$('DPC_calendar1b_MM-DD-YYYY');
	
	
		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 16);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=contractorUpdateCoupon" +  "&couponId=" + encodeURIComponent(couponId) +  "&title=" + escape(title)  +  "&desc=" + escape(desc) +  "&finePrint=" + escape(finePrint) +  "&expirationDate=" + escape(expirationDate) +"&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);	
}
function updatePhotoDescriptions()
{

	//updateHtml('updatingText', 'Updating...');
	var elem = document.getElementById("photoDesc1");
	var descString = "";
	var count = 2;
	while(elem)
	{
		descString = descString + elem.value + "*,*";
		elem = document.getElementById("photoDesc" + count);
		count++;
	}
	document.getElementById('descriptionSavedMsg').innerHTML='<span style="color:red">Saving...</span>';
	
	
		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 19);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=updatePhotoDescriptions" +  "&descString=" + escape(descString)  +"&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);	
}

function postReviewToServer(text,stars,firstName,city,state, userIdReviewed)
{


	document.getElementById('review_submit').value='Submitting...';
	document.getElementById('review_submit').disabled = 'true';
	
		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 20);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=postReview" +  "&text=" + encodeURIComponent(text)  +  "&stars=" + escape(stars)  +  "&firstName=" + escape(firstName)  +  "&city=" + escape(city)  +  "&state=" + escape(state) +  "&userIdReviewed=" + escape(userIdReviewed)     +"&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);	
}


function proAddCity(cityId)
{

		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 21);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=proAddCity" +  "&cityId=" + escape(cityId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}
function proRemoveCity(cityId)
{

		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 21);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=proRemoveCity" +  "&cityId=" + escape(cityId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}
function proRemoveState(stateId)
{

		var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 21);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=proRemoveState" +  "&stateId=" + escape(stateId) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	grayOut(true); 
}


function submitNewCitySuggestion()
{
	var newCity = $$('newCity');
	var state = $$('state');
	
	var xmlHttp = getXMLHttp(); 
  	xmlHttp.onreadystatechange = function()
  	{
		if(xmlHttp.readyState == 4)
    	{
      		HandleResponse(xmlHttp.responseText, 23);
    	}
  	}

 	xmlHttp.open("GET", "/ajax_receiver.php?" + "type=submitNewCitySuggestion" +  "&newCity=" + encodeURIComponent(newCity) +  "&state=" + encodeURIComponent(state) + "&random=" + escape(Math.random()), true); 
 	xmlHttp.send(null);
	
	document.getElementById('submitNewCity').value='Sending...';
	document.getElementById('submitNewCity').disabled = 'true';
	updateHtml('newCityMsg', '');
	
}
							
function HandleResponse(response, inWhatWay)
{
	if (inWhatWay == 1)
	{
		if (null != response && response != '')
		{	  
			
			//show the update chat.
			if (!isError(response))
			{
				if (response == 'reload')
					window.location.reload();
				else
				{
					updateHtml("messages", response);
					newMsgSound();
					if (!haveFocus)
					{	//window is minimized or on different tab. Show new message stuff.
									
						//Start blinking the title
						if (newMessageOn == false)
						{
							newMessageOn = true;
							newMsgNotificationHandle = setInterval("newMessageNotification()", 1000);
						}
						
					}
				}
				
			}
		}
	}
	else if(inWhatWay == 2)
	{
		//show the update chat.
		//alert( response);
		if (null != response && response != '')
		{	 
			if (!isError(response))
			{
				updateHtml("messages", response);
			}
			sentMsgSound();
		}
		//re-enable the post button.
		obj = document.getElementById("btnPostMessage");
		if (obj)
		{
			obj.disabled = false;
		}
			
		
	}
	else if(inWhatWay == 3)
	{
		//re-route to chatNow.php page.
		//alert(response);
		if (response == -2)
		{
			window.location = '/account/cannotChat.php';
		}
		else if (response == -1)
		{
			window.location = '/login.php';
		}
		else
		{
		window.location = '/chats/?id=' + response;
		}
	}
 	else if(inWhatWay == 4)
	{
		if (response == "-1")
		{
			window.location = '/chats/'; //will redirect to /company if user is a company.
		}
		else if (null != response && response != '' && !isError(response))
		{	
			updateHtml("displayChatDetails", response);
		}
		
	}
	else if(inWhatWay == 5)
	{
		/*if (null != response && response != '')
		{	
			document.getElementById("regionList").innerHTML = response;
			
			updateSearchText();
		}*/
	}
	else if(inWhatWay == 6)
	{
		if (null != response && response != '')
		{	
			elements = document.getElementById("orderDirection");

			if (elements){
				orderBy = $$("ordering");
				increasing = $$("orderDirection");
			}
			updateHtml("searchResults", response);
			
			if (elements){
					if (increasing < 0)
					{
						document.getElementById("orderDirection2").selected = true;	
						//document.getElementById("orderDirection2").selected = true;		
					}
					else
					{
						document.getElementById("orderDirection1").selected = true;	
					}
					document.getElementById("orderingOption" + orderBy).selected = true;
			}
		}
	}
	else if (inWhatWay == 7)
	{
			if (null != response && response != '' && !isError(response))
			{
				updateHtml("chatsInProject", response);
			}
	}
	else if (inWhatWay == 8)
	{
			window.location.reload();
			if (null != response && response != '')
			{
				//REFRESH STUFF.
				//window.location.reload;
			}
	}
	else if (inWhatWay == 9)
	{
		//if (null != response && response != '')
			//{
				
				if (response == 'noChange')
					return;
				if (isError(response))
					return;
					
				updateHtml("newChatMessageNotificationDiv", response);			
				
				obj = document.getElementById('AccountNewMessageAlert');
				
					if (response == '' || response == null)
					{
						if (obj)
						{
							obj.style.display = 'none';
						}
						if (objectExists("MyChatsMenu"))
						{
							updateHtml("MyChatsMenu", "My Chats");			
						}
						//stop blinking the title
						//newMessageOn = false;
						//clearInterval(newMsgNotificationHandle);
					}
					else
					{
						if (obj)
						{
							obj.style.display = 'inline';
						}
						if (objectExists("MyChatsMenu"))
						{
							updateHtml("MyChatsMenu", "My Chats <span style='color:red;'>(New Message!)<span>");			
						}
						//Start blinking the title
						if (newMessageOn == false)
						{
							newMessageOn = true;
							newMsgNotificationHandle = setInterval("newMessageNotification()", 1000);
						}
					}
			//}
	}
	else if (inWhatWay == 10)
	{
		alert('An email has been sent to your email account with a link. Simply click this link to resume your chat at a later time.\n\nIf you want to login without a link, simply add a password. You can do this in the Account panel.');
		
	}
	else if (inWhatWay == 11)
	{
		if (null != response && response != '' && !isError(response))
		{
			updateHtml("ActiveText", response);
		}
	}
	else if (inWhatWay == 12)
	{
		if (null != response && response != '' && !isError(response))
		{
			updateHtml("bidsForCurrentListing", response);
			document.getElementById('bidsForCurrentListing').style.height = "auto";
		}
	}
	else if(inWhatWay == 13)
	{
		if (null != response && response != '' && !isError(response))
		{
			alert(response);
		}
		else
		{
			window.location = '/company/editListingDetails.php';
		}
	}
	else if(inWhatWay == 13.2)
	{
		if (null != response && response != '' && !isError(response))
		{
			alert(response);
		}
		window.location = '/company/editListingDetails.php';
	
	}
	else if(inWhatWay == 14)
	{
		if (null != response && response != '' && !isError(response))
		{		
			updateHtml("companiesListing", response);
			if (lastClickWasToExpand != "")
				expandProfile(lastClickWasToExpand);
		}
		updateHtml("updatingText", '');
	}
	else if(inWhatWay == 15)
	{
		if (null != response && response != '' && !isError(response))
		{
			response = response.split(",");
		
			whatToDo = response[0];
			value = response[1];
			if (whatToDo == "chats")
			{
				window.location = '/chats/?id=' + value;
			}
			else if (whatToDo == "login")
			{
				window.location = '/login.php';
			}
			else if (whatToDo == "customerLink")
			{
				window.location = '/msg.php?id=startHelpChat'; 
			}
			else
			{
			//window.location = '/chats/';
			}
		}
	}
	else if (inWhatWay == 16)
	{
			window.location.reload();
			if (null != response && response != '')
			{
				//REFRESH STUFF.
				//window.location.reload;
			}
	}
	else if (inWhatWay == 17)
	{
			// we just changed away status. Re-ping.
				pingAsAlive();
			if (null != response && response != '')
			{
				
			}
	}
	else if (inWhatWay == 18)
	{
			
			if (null != response && response != '' &&!isError(response))
			{
				//update away message div
				updateHtml("awayMessage", response);
				hideNewAwayMessageWindow();
			}
	}
	else if (inWhatWay == 19)
	{
			
	document.getElementById('descriptionSavedMsg').innerHTML='<span style="color:green">Your changes have been saved.</span>';
			
	}
	else if (inWhatWay == 20)
	{
		window.location = window.location + "&tab=reviews";
	}
	else if(inWhatWay == 21)
	{
		if (null != response && response != '' && !isError(response))
		{
			alert(response);
		}
		else
		{
			window.location = '/account/editListingDetails.php';
		}
	}
	else if(inWhatWay == 22)
	{
		updateHtml('editingMessage', '<br /><span style="color:green;">Profile Saved!</span>');
	}
	
	else if(inWhatWay == 23)
	{		
		document.getElementById('submitNewCity').value='Submit';
		document.getElementById('submitNewCity').disabled = false;
		document.getElementById('newCity').value = "";
		if (response != "")
			updateHtml('newCityMsg', '<span style="color:red;">'+response+'</span>');
		else
			updateHtml('newCityMsg', '<span style="color:green;">Your suggestion has been submitted!</span>');
		
	}
}
function isError(response)
{
	errorIndex1 = response.indexOf("Fatal error");
	errorIndex2 = response.indexOf("Parse error");
	errorIndex3 = response.indexOf("Warning");
	if ((errorIndex1 >=0) || (errorIndex2 >= 0) || (errorIndex3 >= 0))
	{
		return true;
	}
	else
	{
		return false;
	}
}

