function displayhide(div) {	
 if(document.getElementById(div).style.display == 'none')
	  document.getElementById(div).style.display = '' ;
 else 
 	  document.getElementById(div).style.display = 'none' ;
}

function showHideDiv(lenght, div, id) 
{		
	for(var i = 0; i < lenght; i++) {
		var strdiv = div + i;	
		if(i == id) 
		{
			document.getElementById(strdiv).style.display = '' ;													
		}
		else {	
			document.getElementById(strdiv).style.display = 'none' ;				
		}
	}	
}

function MyCarpool() {
	this.listCarpool = null;
	this.listMatches = null;
	this.matchCat = null;
	//this.loadListCarpool();
}

MyCarpool.prototype.loadMyCarpool = function(carpoolkey) {
	// load content
	loadForm("LoadMyCarpool");
	// show carpool departure & destination on the map
	this.loadMatches(carpoolkey);
	// load match
}

MyCarpool.prototype.loadListCarpool = function(isLoadFirstCarpool, iCarpoolKey) {	
	$('#processing').css({display:''});	
	loadActionCallBack("LoadCarpoolName", function(resp) {
		if(resp) {		
			$('#processing').css({display:'none'}); //Lnnam change  20/06/2008		
			if(resp.carpool && resp.carpool.length > 0) {				
				$('#idOuterMenuMyCarpool').css({display:''});
				if(resp.carpool.length == 1) {
					$('#idMenuMyCarpool').click(function() {
						//objLayout.changeSidebarDefault();
						objMyCarpool.matchCat = null;
						objMyCarpool.loadMyCarpool(resp.carpool[0].key);
						changeLeftMenu('idMenuMyCarpool');
					});
				}
				else {	
					$('#idMenuMyCarpool').unbind("click");
					$('#idMenuMyCarpool').click(function(event) {  					    						
						dropdownmenu(document.getElementById("idMenuMyCarpool"), event, objMyCarpool.listCarpool, '150px');							
					});					
				}
				
				objMyCarpool.listCarpool = new Array();
				for(var i = 0; i < resp.carpool.length; i++) {
					objMyCarpool.listCarpool[i]="<a href='##' onclick=\"changeLeftMenu('idMenuMyCarpool');objMyCarpool.matchCat = null;objMyCarpool.loadMyCarpool(" + resp.carpool[i].key + ");return false;\">" + resp.carpool[i].name + "</a>";					
				}								
				if(isLoadFirstCarpool) {					
					objMyCarpool.loadMyCarpool(iCarpoolKey? iCarpoolKey: resp.carpool[0].key);
					changeLeftMenu('idMenuMyCarpool');
				}				
			}
			else {
				$('#idOuterMenuMyCarpool').css({display:'none'});
			}
		}												 
	});	
}

MyCarpool.prototype.loadMatches = function(carpoolkey) {
	if(!ajaxObj) {
		ajaxObj = new XMLHTTP("lib/mybic_server.php");
	}	
	ajaxObj.format = "JSON";
	ajaxObj.method = "POST";
	ajaxObj.call("action=LoadMatch&carpoolkey=" + carpoolkey + "", function (resp) {
		if(resp) {				
			$("#processing-loadmatch").css({display:'none'}); 
			objMyCarpool.listMatches = resp;
			if(objMyCarpool.matchCat && document.getElementById(objMyCarpool.matchCat) != null) {
				objMyCarpool.showMatches(objMyCarpool.matchCat, true);
				showhide_mycarpool(objMyCarpool.matchCat);
			}
			else {
				objMyCarpool.showMatches("contentMatches", true);
				showhide_mycarpool("contentMatches");
			}
			
			if(resp.flagCarpoolFull == 'Y') {
				$('#idCarpoolFull').html("<a href='##' id='idFlagCarpoolFull' style='color:#FFFFFF;'>make active</a>");
				$('#idFlagCarpoolFull').click(function() {
					objMyCarpool.setCarpoolFull('N');
				});
			}
			$('#idCarpoolName').html(resp.carpoolName);
		}
		else { alert('error');}
	});
}

MyCarpool.prototype.showMatches = function(matchCategory,isLoad) {
	var depLatLng = this.listMatches.depLatLng.split(',');		  		
	var desLatLng = this.listMatches.desLatLng.split(',');		  												
	var idContentMatches = null;

	objMyCarpool.matchCat = matchCategory;
	
	$("#idMatchesHeading").html('<a href="##" class="color_white_bold" onclick="showhide_mycarpool(\'contentMatches\');objMyCarpool.showMatches(\'contentMatches\', false);" class="topright_menu">Matches</a>');
	$("#idBuddiesHeading").html('<a href="##" class="color_white_bold" onclick="showhide_mycarpool(\'contentMycarPoolBuddies\');objMyCarpool.showMatches(\'contentMycarPoolBuddies\', false);" class="topright_menu">My Commute Associates</a>');
	$("#idRemovedBuddiesHeading").html('<a href="##" class="color_white_bold" onclick="showhide_mycarpool(\'contentRemovedCarpoolbuddies\');objMyCarpool.showMatches(\'contentRemovedCarpoolbuddies\', false);" class="topright_menu">Removed Commutes Associates</a>');
	$("#idDiscardHeading").html('<a href="##" class="color_white_bold" onclick="showhide_mycarpool(\'contentDiscardMatches\');objMyCarpool.showMatches(\'contentDiscardMatches\', false);" class="topright_menu">Discard Matches</a>');
	$("#idBlockHeading").html('<a href="##" class="color_white_bold" onclick="showhide_mycarpool(\'contentBlockMatches\');objMyCarpool.showMatches(\'contentBlockMatches\', false);" class="topright_menu">Blocked Users</a>');		
	if(document.getElementById(matchCategory) == null) {
		return;
	}
	
	$('#idLbMatcheDetails').css({display:'none'});	//Lnnam change 28/08/2008
	$('#idLine').css({display:'none'});	//Lnnam change 28/08/2008
	
	switch(matchCategory) {
		case "contentMatches":
			idContentMatches = document.getElementById("contentMatches");		
			arrMatches = this.listMatches.Matches;
			$("#idMatchesHeading").html("<span class='fontHeader12'>Matches</span>");
			$('#idLbMatcheDetails').css({display:''}); //Lnnam change 28/08/2008
			$('#idLine').css({display:''});	//Lnnam change 28/08/2008
			break;
		case "contentMycarPoolBuddies":
			idContentMatches = document.getElementById("contentMycarPoolBuddies");				
			arrMatches = this.listMatches.Buddies;
			$("#idBuddiesHeading").html("<span class='fontHeader12'>My Commute Associates</span>");			
			break;			
		case "contentRemovedCarpoolbuddies":
			idContentMatches = document.getElementById("contentRemovedCarpoolbuddies");				
			arrMatches = this.listMatches.RemovedBuddies;
			$("#idRemovedBuddiesHeading").html("<span class='fontHeader12'>Removed Commute Associates</span>");			
			break;		
		case "contentDiscardMatches":
			idContentMatches = document.getElementById("contentDiscardMatches");				
			arrMatches = this.listMatches.DiscardMatches;
			$("#idDiscardHeading").html("<span class='fontHeader12'>Discard Matches</span>");			
			break;		
	    case "contentBlockMatches":
			idContentMatches = document.getElementById("contentBlockMatches");				
			arrMatches = this.listMatches.BlockMatches;
			var strMessage = '<table width="95%" border="0">';
			for(var i = 0; i < arrMatches.length; i++) {				
			    if(arrMatches[i].FeedbackonPersonKey && arrMatches[i].ScreenName) {
					strMessage += "<tr><td width='80%'>" + arrMatches[i].ScreenName + "</td><td width='20%' align='right'><a href='##' onclick='objMyCarpool.unBlockUser(" + arrMatches[i].FeedbackonPersonKey + ");' >Unblock</a></td></tr>";
				}
			}						
			strMessage += "</table>";						
			idContentMatches.innerHTML = strMessage;
			$("#idBlockHeading").html("<span class='fontHeader12'>Blocked Users</span>");	
			
			objCPMap.map.clearOverlays();
			objCPMap.showDepDesLatLng(new GLatLng(depLatLng[0],depLatLng[1]),new GLatLng(desLatLng[0],desLatLng[1]),"Dep" + this.listMatches.driverPassengerEither,"Des" + this.listMatches.driverPassengerEither,"idCarpoolDetail");			
			return;
			break;					
	}
	//$('#contentMatches, #contentMycarPoolBuddies, #contentRemovedCarpoolbuddies').unbind();
	objCPMap.map.clearOverlays();	
	idContentMatches.innerHTML = ""; 

	for(var i = 0; i < arrMatches.length; i++) {
		var strMessage = '';
		if(arrMatches[i].MessagesSent || arrMatches[i].MessagesReceived) {
			strMessage += "<div class='title'>(" + arrMatches[i].MessagesSent;
			if(arrMatches[i].MessagesReceived)												
				strMessage += (arrMatches[i].MessagesSent ? ", " : "") + arrMatches[i].MessagesReceived;
			strMessage += ")</div>";				
		}
		if(i == 0) idContentMatches.innerHTML = "<br/>";
		idContentMatches.innerHTML += "<div class='listMatch'>"
									  + "<div id='outer_" + matchCategory + "_ViewDetail" +  (i+1) + "'>" 
									  + (i+1) + ". "									  
									  + arrMatches[i].ScreenName + " " 
									  + arrMatches[i].DriverPassengerEither + " " 
									  + (arrMatches[i].Rating ? ", " : "") + " " + arrMatches[i].Rating + "</div>"
									  + strMessage
									  + "<div><a href='##' id='" + matchCategory + "_ViewDetail" +  (i+1) + "' class='color_green'>more info</a></div></div>";
	}		
	
	objCPMap.showDepDesLatLng(new GLatLng(depLatLng[0],depLatLng[1]),new GLatLng(desLatLng[0],desLatLng[1]),"Dep" + this.listMatches.driverPassengerEither,"Des" + this.listMatches.driverPassengerEither,"idCarpoolDetail");

	for(var i = 0; i < arrMatches.length; i++) {
		objCPMap.showPairOfMatches(matchCategory + "_Icon" +  (i+1), matchCategory + "_ViewDetail" +  (i+1), new GLatLng(arrMatches[i].PickupLat, arrMatches[i].PickupLong), new GLatLng(arrMatches[i].DesLat, arrMatches[i].DesLong), "Match" + arrMatches[i].DriverPassengerEither, (i+1), arrMatches[i].CarpoolKeyMatch, arrMatches[i], arrMatches[i].MatchCategoryDesc, arrMatches[i].ScreenName, arrMatches[i].Rating, arrMatches[i], "LoadMatchDetail");							
	}
}

MyCarpool.prototype.unBlockUser = function(intFbonPersonKey) {
	ajaxObj.call("action=UnBlockUsers&intFeedbackonPersonKey="+ intFbonPersonKey, function(resp) {
		objMyCarpool.loadMatches(resp);
	});	
}

MyCarpool.prototype.blockUser = function(intCarpoolKey) {
	objMyCarpool.loadMatches(intCarpoolKey);
}




MyCarpool.prototype.updateMatchCategory = function(typeAction) {
	document.getElementById('idMatchDetail').innerHTML="<br><br><br><br><div align='center' class='processing'><img src='images/loading_1.gif' width='18' height='20'>proccessing...</div>";		
	switch(typeAction) {
		case "idCarpoolBuddy":			
			objCPMap.map.removeOverlay(objCPMap.ojbDelMarker);			
			ajaxObj.call("action=updateMatchCategory&intMatchCategoryKey=2&carpoolKeyMatch="+ objCPMap.jsonMatch.CarpoolKeyMatch +"", function(resp) {
					objMyCarpool.loadMatches(resp);
			});		
			break;
		case "idDiscardMatch":
			objCPMap.map.removeOverlay(objCPMap.ojbDelMarker);			
			ajaxObj.call("action=updateMatchCategory&intMatchCategoryKey=3&carpoolKeyMatch="+ objCPMap.jsonMatch.CarpoolKeyMatch +"", function(resp) {
					objMyCarpool.loadMatches(resp);
			});		
			break;
		case "idRemoveBuddy":
			objCPMap.map.removeOverlay(objCPMap.ojbDelMarker);			
			ajaxObj.call("action=updateMatchCategory&intMatchCategoryKey=4&carpoolKeyMatch="+ objCPMap.jsonMatch.CarpoolKeyMatch +"", function(resp) {
					objMyCarpool.loadMatches(resp);
			});		
			break;			
		case "ibBlockUser":
			objCPMap.map.removeOverlay(objCPMap.ojbDelMarker);			
			ajaxObj.call("action=updateMatchCategory&intMatchCategoryKey=5&carpoolKeyMatch="+ objCPMap.jsonMatch.CarpoolKeyMatch +"", function(resp) {
					loadForm("LoadMyCarpool");					
					objMyCarpool.loadMatches(resp);
			});		
			break;
	}	
}

MyCarpool.prototype.setCarpoolFull = function(cFlagCarpoolFull) {
	if(!ajaxObj) {
		ajaxObj = new XMLHTTP("lib/mybic_server.php");
	}	

	ajaxObj.format = "TEXT";
	ajaxObj.method = "POST";
	ajaxObj.call("action=SetCarpoolFull&cFlagCarpoolFull=" + cFlagCarpoolFull + "", function (resp) {
	});
	
	if(cFlagCarpoolFull == 'Y') {
		$('#idCarpoolFull').html("<a href='##' id='idFlagCarpoolFull' style='color:#FFFFFF;'>make active</a>");
		$('#idFlagCarpoolFull').click(function() {
			objMyCarpool.setCarpoolFull('N');
		});
	}
	else {
		$('#idCarpoolFull').html("<a href='##' id='idFlagCarpoolFull' style='color:#FFFFFF;'>make inactive</a>");
		$('#idFlagCarpoolFull').click(function() {
			objMyCarpool.setCarpoolFull('Y');
		});
	}
}

MyCarpool.prototype.deleteCarpool = function() {
	if(!ajaxObj) {
		ajaxObj = new XMLHTTP("lib/mybic_server.php");
	}	
	ajaxObj.format = "TEXT";
	ajaxObj.method = "GET";
	ajaxObj.call('action=DeleteCarpool', function (resp) {
		if(resp) {	
			//window.location = 'index.php';
			document.location.reload();
			//window.location = strDomain;				  		
		} else {
			//load error page
			alert('error');
		}												   
	});	
}

function showhide_mycarpool(spanid1) { 
	
	if(document.getElementById("contentMatches")&&document.getElementById("contentMycarPoolBuddies")&&document.getElementById("contentRemovedCarpoolbuddies")&&document.getElementById("contentDiscardMatches")&&document.getElementById("contentBlockMatches"))//Lnnam fix bugs 23/07/2008
	{
		if(spanid1 != "contentMatches"){document.getElementById("contentMatches").style.display='none';}
		if(spanid1 != "contentMycarPoolBuddies"){document.getElementById("contentMycarPoolBuddies").style.display='none';}
		if(spanid1 != "contentRemovedCarpoolbuddies"){document.getElementById("contentRemovedCarpoolbuddies").style.display='none';}
		if(spanid1 != "contentDiscardMatches"){document.getElementById("contentDiscardMatches").style.display='none';}	
		if(spanid1 != "contentBlockMatches"){document.getElementById("contentBlockMatches").style.display='none';}	
		if (document.getElementById(spanid1).style.display=='none'){
			document.getElementById(spanid1).style.display='';
		}
	}
}