// JavaScript Document
var http;
var idToDisplayMsg;
var urlForAjax;
var possibleFormElements=new Array("input","textarea","select","file");
var globalTableCount=1;


function ratesite(value,siteId){
	urlForAjax = "ajaxPages/ratesite.php?rate_value="+value+"&siteId="+siteId+"&mathRandomNum='"+Math.random()+"'";	
	idToDisplayMsg="rating_content";
		callAjaxFunction_GETMethod(urlForAjax);		
	}


function getHTTPObject() {
	var xmlhttp=false; //Clear our fetching variable
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object.
        } catch (e) {
                try {
                        xmlhttp = new
                        ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
            } catch (E) {
                xmlhttp = false;
                        }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
        }
  	return xmlhttp;
}

function showSiteDetails(valueOption){
	//if(valueOption=='nolist'){
	//	document.getElementById('showAddNewSpeciesID').innerHTML=''
	///	document.getElementById('showAddNewSpeciesIDClickNew').style.display='block';
	//	else}
	 if(valueOption!='0'){
		//document.getElementById('showAddNewSpeciesIDClickNew').style.display='none';
		idToDisplayMsg="showAddNewSpeciesID";	
		urlForAjax = "ajaxPages/sendIdShowFields.php?checkParameter=showDiveDetail&siteId="+valueOption+"&mathRandomNum='"+Math.random()+"'";			
		callAjaxFunction_GETMethod(urlForAjax);		
		}
	}
function addSpeciesDetails(speciesId,optiontype,logbookId){	
  
		if(speciesId!=0){
			document.getElementById('showAddSpeciesDetails').style.display='block';
			idToDisplayMsg="showAddSpeciesDetails";	
			urlForAjax = "ajaxPages/sendIdShowFields.php?checkParameter=showSpeciesDetail&optiontype="+optiontype+"&speciesId="+speciesId+"&logbookId="+logbookId+"&imgPath="+1+"&mathRandomNum='"+Math.random()+"'";			
			callAjaxFunction_GETMethod(urlForAjax);
		}
		else{
			$('showAddSpeciesDetails').style.display='none';
			}
	}
function addSpeciesDetailsTabs(speciesId,optiontype,logbookId,styleTab,cts,tabId){	
    
		if(speciesId!=0){
			document.getElementById('showAddSpeciesDetails').style.display='block';
			
			for(i=0; i<cts; i++)
			  {
			if(i==tabId)
			   {
			document.getElementById('tabStyle'+i).style.backgroundColor='#8ab064';
			   }else
			   {
			document.getElementById('tabStyle'+i).style.backgroundColor='#4669A4';	   
				   }
			  }
			idToDisplayMsg="showAddSpeciesDetails";	
			urlForAjax = "ajaxPages/sendIdShowFields.php?checkParameter=showSpeciesDetail&optiontype="+optiontype+"&speciesId="+speciesId+"&logbookId="+logbookId+"&mathRandomNum='"+Math.random()+"'";			
			callAjaxFunction_GETMethod(urlForAjax);
		}
		else{
			$('showAddSpeciesDetails').style.display='none';
			}
	}	

function showIdentificationDetails(identificationID,name){	
   // alert(identificationID);
	idToDisplayMsg="show"+name+"DetailsId";	
	urlForAjax = "ajaxPages/search.php?checkParameter=showSpeciesDetail&identificationID="+identificationID+"&mathRandomNum='"+Math.random()+"'";			
	callAjaxFunction_GETMethod(urlForAjax);
	}

function chkSpeciesExist(parentid){
	idToDisplayMsg="showSpeciesExistMsg";
	Svalue=document.getElementById('txtScintName').value;
	urlForAjax = "ajaxPages/search.php?checkParameter=SpeciesExist&parentid="+parentid+"&Svalue="+Svalue+"&mathRandomNum='"+Math.random()+"'";			
	callAjaxFunction_GETMethod(urlForAjax);

	}
function showSearchCriteria(value){
	idToDisplayMsg="showSearchCriteriaDisplay";				
	urlForAjax = "ajaxPages/search.php?checkParameter=advSearch&srcOf="+value+"&mathRandomNum='"+Math.random()+"'";
	callAjaxFunction_GETMethod(urlForAjax);
}
function removeTableSlot(name,parentNum){
	parentNum=parseInt(document.getElementById('txtTableCount_'+name).value);
	document.getElementById('removeTable_'+name+'_'+parentNum).innerHTML="";
	document.getElementById('txtTableCount_'+name).value=parseInt(document.getElementById('txtTableCount_'+name).value)-1;
}

function addTableSlot(name,parentNum){
	parentNum=document.getElementById('txtTableCount_'+name).value=parseInt(document.getElementById('txtTableCount_'+name).value)+1;
	idToDisplayMsg="idtoreplace_"+name+"_"+(parentNum-1);
	tblIdName=name;
	urlForAjax="ajaxPages/appendTable.php?name="+name+"&parentNum="+parentNum+"&mathRandomNum="+Math.random();
	callAjaxFunction_GETMethod2(urlForAjax);	
}

function callAjaxFunction_GETMethod2(url){
	http=null;
	http = getHTTPObject();	
	http.open("GET", url , true);	
	http.onreadystatechange = stateChanged2;	
	http.send(null);	

}
function stateChanged2(){		
		if( http.readyState == 4){
			var up = document.getElementById('table_slots_'+tblIdName);
			document.getElementById(idToDisplayMsg).innerHTML='';
			var dv = document.createElement(idToDisplayMsg);
			dv.innerHTML=http.responseText;
			up.appendChild(dv);
			up = null;
			dv = null;
		}		
}

function showFieldsFromDropDown(value,id){
	idToDisplayMsg=id;		
	if(value!=0){
		document.getElementById(idToDisplayMsg).style.display='';				
		urlForAjax = "ajaxPages/sendIdShowFields.php?value="+value+"&mathRandomNum='"+Math.random()+"'";			
		callAjaxFunction_GETMethod(urlForAjax);
	}else{
		document.getElementById(idToDisplayMsg).style.display='none';
	}
}

function showClub(diverId,clubId,type,id,srcId,postId){
		 idToDisplayMsg=id;
	/*if(value!=0){*/
		document.getElementById(idToDisplayMsg).style.display='';				
		//alert(type);
		if(type=="clubPost")
		{
		urlForAjax = "ajaxPages/clubPost.php?diverId="+diverId+"&clubId="+clubId+"&type="+type+"&mode="+srcId+"&postId="+postId;
		//'document.getElementById('postBl').style='content-tab-active';
		//srcId.className='content-tab-active';
		}
		if(type=="messageBoard")
		{
		urlForAjax = "ajaxPages/eventMessage.php?diverId="+diverId+"&eventId="+clubId+"&type="+type;
		//'document.getElementById('postBl').style='content-tab-active';
		//srcId.className='content-tab-active';
		}
		else if(type !="clubPost")
		{
		urlForAjax = "ajaxPages/clubs.php?diverId="+diverId+"&clubId="+clubId+"&type="+type;			
		}
		callAjaxFunction_GETMethod(urlForAjax);
	/*}else{
		document.getElementById(idToDisplayMsg).style.display='none';
	}*/
}

function showEntityClub(entityId,clubId,type,id,srcId,postId){
	idToDisplayMsg=id;
	/*if(value!=0){*/
		document.getElementById(idToDisplayMsg).style.display='';
		if(type=="clubEntityPost")
		{
		urlForAjax = "ajaxPages/clubEntityPost.php?diverId="+entityId+"&clubId="+clubId+"&type="+type+"&mode="+srcId+"&postId="+postId;
		//'document.getElementById('postBl').style='content-tab-active';
		//srcId.className='content-tab-active';
		}
		else
		{
		urlForAjax = "ajaxPages/entityNotice.php?diverId="+entityId+"&clubId="+clubId+"&type="+type;			
		}
		callAjaxFunction_GETMethod(urlForAjax);
	/*}else{
		document.getElementById(idToDisplayMsg).style.display='none';
	}*/
}
function showCompanyProfile(CompanyId,type,id,desc,span_id)
{
		 idToDisplayMsg=id;
	/*if(value!=0){	  $class=" background-color:#4669A4;";
	  $class2=" color:#94e100;";*/	  
	   
		document.getElementById(idToDisplayMsg).style.display='';
		document.getElementById(span_id).style.backgroundColor='#8ab064';	
		document.getElementById(span_id).style.color='#fff';
		for(i=1; i<=4; i++)
		{
			if(span_id!='c'+i)
			{
				document.getElementById('c'+i).style.backgroundColor='#4669A4';	
				document.getElementById('c'+i).style.color='#94e100';	
			}
			
		}
		if(type=="company_description")
		urlForAjax = "ajaxPages/company_description.php?CompanyId="+CompanyId+"&type="+type+"&desc="+desc;			
		else
		urlForAjax = "ajaxPages/"+type+".php?CompanyId="+CompanyId+"&type="+type+"&desc="+desc;			
		callAjaxFunction_GETMethod(urlForAjax);
	/*}else{
		document.getElementById(idToDisplayMsg).style.display='none';
	}*/
}

function showEvent(DiverId,type,id,tag,span_id,eventid)
{
		
		 idToDisplayMsg=id;
	   
		document.getElementById(idToDisplayMsg).style.display='';
		document.getElementById(span_id).style.backgroundColor='#8ab064';	
		document.getElementById(span_id).style.color='#fff';
		for(i=2; i<=5; i++)
		{
			if(span_id!='c'+i)
			{
				document.getElementById('c'+i).style.backgroundColor='#4669A4';	
				document.getElementById('c'+i).style.color='#94e100';	
			}
			
		}
		urlForAjax = "ajaxPages/"+type+".php?Id="+DiverId+"&type="+type+"&tag="+tag+"&event="+eventid;			
		callAjaxFunction_GETMethod(urlForAjax);
}

function allowEditEvent(diverId,type,id,tag,span_id,eventid)
{
		 idToDisplayMsg=id;	   
		urlForAjax = "../ajaxPages/"+type+".php?diver="+diverId+"&type="+type+"&tag="+tag+"&event="+eventid;			
		callAjaxFunction_GETMethod(urlForAjax);
}

function postComment(imageId){
	if($('txtComment').value==''){
		alert("Nothing to post");
		return false;
		}
	else{
		var comment=$('txtComment').value;		
		idToDisplayMsg="td_body_content";		
		urlForAjax="ajaxPages/makeComment.php?comment="+comment+"&imageId="+imageId+"&mathRandomNum="+Math.random();
		callAjaxFunction_GETMethod(urlForAjax);	
		}
	}
/*function callAjaxFunctionToPostData(frmName,url){
	//alert(frmName);
	http=null;
	http = getHTTPObject();
	var postValues='';	
	postValues=getFormElementsAndValues(frmName);	
	http.open("POST", url , true);
	http.onreadystatechange = stateChanged;
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
	http.send(postValues);	
}

function getFormElementsAndValues(form_name){	
	var out = new Array();
	var formInputs='';
	var frmElementName='';
	var elementName=''
	var postData='';
	var radioCheck='';
	var radioButton;
	var radioCheckedValue;
	var frmElementType;
	//alert(possibleFormElements.length);
	for(var j=0;j<possibleFormElements.length;j++){
		elementName=possibleFormElements[j];		
		formInputs = eval('document.'+form_name+'.getElementsByTagName("'+elementName+'")');		
	
		for (var i = 0; i < formInputs.length; i++){							
				frmElementName=formInputs.item(i).name;						
				frmElementType=formInputs.item(i).type;	
				
				if(frmElementType=='radio'){
			radioButton=eval("document."+form_name+"."+frmElementName);
radioCheckedValue=validateRadioButton(radioButton);						
					value=eval("document."+form_name+"."+frmElementName+"["+radioCheckedValue+"].value");

					}				
				else if(frmElementType=='checkbox'){					
					if(eval("document.getElementById('"+frmElementName+"').checked"))
						value=value=eval("document.getElementById('"+frmElementName+"').value");
				}
				
				if(frmElementType!='radio' && frmElementType!='checkbox'){	
					value=eval("document.getElementById('"+frmElementName+"').value");
				}				
				
				var subBtn=frmElementName.toLowerCase();
				var btnBtn=frmElementName.toLowerCase();
				
				if(subBtn!='submit' || btnBtn!='button'){					
					if(postData=='')
						postData=frmElementName+"="+value;
					else{
						var exitsOrNot=eval("postData.indexOf('"+frmElementName+"=')");						
						if(frmElementType=='radio'){							
							
							radioCheck=eval("document."+form_name+"."+frmElementName+"["+radioCheckedValue+"].checked");							
						}
						else{
							radioCheck=true;
						}
						if(exitsOrNot==-1 && radioCheck)
							postData+="&"+frmElementName+"="+value;
						}
				
				}		
		}//enf of i-for
	}//end of j-for		
	return (postData);

	}*/
function appendEmail4Message(hideId,aptext,apemail,apimage){

	document.getElementById(hideId).style.display="none";
	document.getElementById('searchlist').style.display="none";
	document.getElementById('showName').style.display="";
	document.getElementById('toEmailHidden').value=apemail;	
	
	document.getElementById('showName').innerHTML="<table width='100'><tr><td valign='top'><img src='phpthumb/phpthumb.php?src=../images/diverImages/"+apimage+"&w=80&h=80&aoe=1'></td><td valign='top'><img src='images/skinimages/delete.gif' align='right' onclick=\"ShowHide2Div('"+hideId+"','showName')\" style='cursor:pointer'></td></tr><tr><td class='txtnormal' align='left' colspan='2'>"+document.getElementById(aptext).value+"</td></tr></table>";	
	//document.getElementById(hideId).onkeyup=list_friend();
}

function list_friend(id){	
	idToDisplayMsg="searchlist";	
	
	document.getElementById('toEmailHidden').value=id;
	document.getElementById(idToDisplayMsg).style.display='';
	urlForAjax="ajaxPages/search.php?checkParameter=buddyEmail&emailTxt="+id+"&mathRandomNum="+Math.random();		
	callAjaxFunction_GETMethod(urlForAjax);	
	document.getElementById('buddyListSearch').style.display='none';
}

function homePage(containerId,url)
{
var xmlhttp = getHTTPObject();
var file = url;
xmlhttp.open('GET', file, true); 
xmlhttp.onreadystatechange=function() {
	   		if (xmlhttp.readyState==4) { 
		var content = xmlhttp.responseText;
			
			if(content){
				
				document.getElementById(containerId).style.display='block';
				document.getElementById(containerId).innerHTML = content; 
				
				
			}
	}else
	{
		document.getElementById(containerId).innerHTML = "<img src='images/loader.gif'>"; 
		}
}
xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}



function list_friend_commercial(id){	
	idToDisplayMsg="searchlist";	
	
	document.getElementById('toEmailHidden').value=id;
	document.getElementById(idToDisplayMsg).style.display='';
	urlForAjax="ajaxPages/search.php?checkParameter=commercialEmail&emailTxt="+id+"&mathRandomNum="+Math.random();		
	callAjaxFunction_GETMethod(urlForAjax);	
	document.getElementById('buddyListSearch').style.display='none';
}

function list_friend_event(id){	
	idToDisplayMsg="searchlist";	
	
	document.getElementById('toEmailHidden').value=id;
	document.getElementById(idToDisplayMsg).style.display='';
	urlForAjax="ajaxPages/search.php?checkParameter=evetMemberEmail&emailTxt="+id+"&mathRandomNum="+Math.random();		
	callAjaxFunction_GETMethod(urlForAjax);	
	document.getElementById('buddyListSearch').style.display='none';
}

/*function list_friend(){
	idToDisplayMsg="searchlist";
	document.getElementById(idToDisplayMsg).style.display='';
	var emailTxt=document.getElementById('toEmail').value;
	urlForAjax="ajaxPages/search.php?checkParameter=buddyEmail&emailTxt="+id+"&mathRandomNum="+Math.random();	
	callAjaxFunction_GETMethod(urlForAjax);	
}
*/
function showDropDownBox(idtoshow,value){						
		if(document.getElementById(idtoshow).value!=value){
		document.getElementById("span_"+idtoshow).style.display='';
		}
		else{
			document.getElementById("span_"+idtoshow).style.display='none';
			}
		//document.getElementById(idtoshow).ondblclick=false;
	}
	
function showTxtBox(value,idtoshow,idOfField){		
	if(value=='new'){	
		document.getElementById('id'+idtoshow).innerHTML="<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td class='frmLabel'>Common Name</td><td class='txtnormal'><input type='text' name='txtCommonName"+idtoshow+"' id='txtCommonName"+idtoshow+"'></td></tr><tr><td class='frmLabel'>Scientific Name</td><td class='txtnormal'><input type='text' name='txtScintName"+idtoshow+"' id='txtScintName"+idtoshow+"'></td></tr><tr><td class='frmLabel'>Family</td><td class='txtnormal'><input type='text' name='txtFamily"+idtoshow+"' id='txtFamily"+idtoshow+"'></td></tr><tr><td class='frmLabel'>Genus</td><td class='txtnormal'><input type='text' name='txtGenus"+idtoshow+"' id='txtGenus"+idtoshow+"'></td></tr><tr><td class='frmLabel'>Species</td><td><input type='text' name='txtSpecies"+idtoshow+"' id='txtSpecies"+idtoshow+"' class='txtnormal'></td></tr><tr><td class='frmLabel'>Habitat</td><td><textarea name='txtHabitat"+idtoshow+"' cols='40' rows='7' id='txtHabitat"+idtoshow+"' class='txtnormal'></textarea></td></tr></table>";			
	}
	else{
		document.getElementById('id'+idtoshow).innerHTML="";		
		}	
}
function showTxtArea(idtoshow){	

		var value=document.getElementById(idtoshow).innerHTML;
		
		document.getElementById(idtoshow).ondblclick=false;		
		document.getElementById(idtoshow).innerHTML="<textarea name='txt"+idtoshow+"' cols='45' rows='5' wrap='virtual' id='txt"+idtoshow+"'>"+value+"</textarea>&nbsp;&nbsp;<a href='javascript:update_field(\"txt\",\"txt"+idtoshow+"\",\""+idtoshow+"\")'>Save</a>";	
		
	}
	
function update_field(type,idOfNewValueField,fieldName){	
	idToDisplayMsg=fieldName;	
	if(type=='dropdown'){
		idToDisplayMsg="td_"+fieldName;
	}
	
	var updatedValue=document.getElementById(idOfNewValueField).value;		
	url="ajaxPages/update_profile.php?fieldName="+fieldName+"&value="+updatedValue+"&choice="+fieldName+"&mathRandomNum='"+Math.random()+"'";	
	callAjaxFunction_GETMethod(url);
	/*onSuccessArray =[];	
	addOnSuccess(function(){
		document.getElementById('chkBox'+no+idOfData).checked=false;
		document.getElementById('data'+no+idOfData).innerHTML=document.getElementById('txtdata'+no+idOfData).value;						  
						  })*/

}

function chkUserNameAvailability(){
	if($('username').value=='' || $('username').value.length<=3){
		$('err_username').innerHTML="Username must be at least 3 char";
		return false;
		}
	else{
	idToDisplayMsg="err_username";
	var uname=document.getElementById('username').value;
	urlForAjax="ajaxPages/check.php?checkParameter=username&uname="+uname+"&mathRandomNum="+Math.random();;
	callAjaxFunction_GETMethod(urlForAjax);	
	}
}
function callAjaxFunction_GETMethod(url){
	http=null;
	http = getHTTPObject();	
	http.open("GET", url , true);	
	http.onreadystatechange = stateChanged;	
	http.send(null);	

}
function stateChanged(){
		if( http.readyState == 4){
			document.getElementById(idToDisplayMsg).innerHTML=http.responseText;
		}
		else{
			document.getElementById(idToDisplayMsg).innerHTML="working.....";
			}
}
function getHTTPObject() {
	var xmlhttp=null;
 	if(window.XMLHttpRequest){
    	xmlhttp = new XMLHttpRequest();
  	}
  	else if (window.ActiveXObject){
    	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	if (!xmlhttp){
        	xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
    }
  	return xmlhttp;
}

