// JavaScript Document
var httpRequest=false;
function sendRequest(url,updateSection)
{ 
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	//alert(sPage);
	//if(sPage !='search.php' && sPage !='index.php' && sPage !='contact-form.php' && sPage !='ques-ans.php' && sPage !='patient-register.php')
	//scrollto('a_top',2); //move to top
	
	var msxmlhttp = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
	for (var i = 0; i < msxmlhttp.length; i++) 
	{
		try 
		{ 
			httpRequest = new ActiveXObject(msxmlhttp[i]); 
		}
		catch (e) { httpRequest =false;}
		if(!httpRequest && typeof XMLHttpRequest != "undefined")
		{
			try 
			{ 
				httpRequest = new XMLHttpRequest(); 
			}
			catch (e) { httpRequest = false;}
		}
				
	}
	
	httpRequest.open("GET", url, true); 
	httpRequest.onreadystatechange = function() {processRequest(updateSection); };
	httpRequest.send(null);
}

function processRequest(updateSection) 
{ 
    if(httpRequest.readyState == 4) 
    { 
		if(httpRequest.status == 200) 
        {
           	//Update the HTML 
			var temp_msg=httpRequest.responseText
			var msg_arr = temp_msg.split("|");
			var opr = msg_arr[0]; // operation done
			var msg = msg_arr[1]; // matter from ajax.php
			//alert(msg);
			//alert(temp_msg);
			if(opr ==1 || opr ==11)
			{
				// treatment id & name sep '::' 
			   var sel_treat = document.getElementById('treatmentID'); 
			   if(msg == '') {  
				 sel_treat.options.length = 0; 
				 sel_treat.options[sel_treat.options.length] = new Option('Select a treatment...', ''); 
				 
			   }else{ 
				 // treatmentID 
				 sel_treat.options.length = 0; 
				 sel_treat.options[sel_treat.options.length] = new Option('Select a treatment...', ''); 
				 
				 var treat_opt = msg.split('@');
				 				 
				 for(var i=0;i<treat_opt.length;i++) {
				   if( treat_opt[i] != '') { 
					 var treat_opt_sub = treat_opt[i].split(':');
					 var treat_optsubid = treat_opt_sub[0];
					 var treat_optsubval = treat_opt_sub[1];
					 if(treat_optsubval != '' ) {
					   sel_treat.options[sel_treat.options.length] = new Option(treat_optsubval, treat_optsubid); 
					 }
				   }
							   
				 }
				 // update options dynamically ...  
				  //document.getElementById('td_treatment').innerHTML = msg;
			   }

			}
			else if(document.getElementById(updateSection)){
				document.getElementById(updateSection).innerHTML=msg;			
			}
        } 
        else 
        { 
            alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText); 
        }
    }         
} 

// showing treatment under treatment_area
function get_treatment_list(id,type){
	if(type !='admin')
		var url = "ajax.php?treat_area_id="+id+"&opr=1";
	else
		var url = "../ajax.php?treat_area_id="+id+"&opr=1"; // for admin before-after-list.php
	
	sendRequest(url,"td_treatment");
}

// showing treatment under treatment_area
function get_photo_treatment_list(id,type){
	if(type !='admin')
		var url = "ajax.php?treat_area_id="+id+"&opr=11";
	else
		var url = "../ajax.php?treat_area_id="+id+"&opr=11"; // for admin before-after-list.php
	
	sendRequest(url,"td_treatment");
}

// showing treatment under treatment_area in admin panel
function get_admin_treatment_list(id){
	var url = "../ajax.php?treat_area_id="+id+"&opr=1";
	sendRequest(url,"td_treatment");
}

// showing search page content
function getMemberDetail(memID) {
	scrollto('a_top',2); //move to top
	document.getElementById('td_mem_detail').innerHTML = "<BR><BR><BR><span class='org-text' align='left'><b>Loading...</b></span><br><img src='image/loading.gif' width='220' height='19'/>";
	var url = "ajax.php?memID="+memID+"&opr=2";
	sendRequest(url,"td_mem_detail");
}

//showing treatment page content
//Removed due to URL Rewriting
/*function getTreatmentDetail(tid,tarea_id) {
	document.getElementById('td_mem_detail').innerHTML = "<BR><BR><BR><span class='org-text' align='left'><b>Loading...</b></span><br><img src='image/loading.gif' width='220' height='19'/>";
	var url = "ajax.php?tid="+tid+"&tarea_id="+tarea_id+"&opr=3";
	sendRequest(url,"td_mem_detail");
}

//showing Health Provider page content
//Removed due to URL Rewriting
function getHealthProviderContent(pid){
	document.getElementById('td_mem_detail').innerHTML = "<BR><BR><BR><span class='org-text' align='left'><b>Loading...</b></span><br><img src='image/loading.gif' width='220' height='19'/>";
	var url = "ajax.php?pid="+pid+"&opr=4";
	sendRequest(url,"td_mem_detail");
}*/

// showing destination page content
//Removed due to URL Rewriting
/*function getDestinationDetail(dest_id,dzone_id) {
	document.getElementById('td_mem_detail').innerHTML = "<BR><BR><BR><span class='org-text' align='left'><b>Loading...</b></span><br><img src='image/loading.gif' width='220' height='19'/>";
	var url = "ajax.php?dest_id="+dest_id+"&dzone_id="+dzone_id+"&opr=5";
	sendRequest(url,"td_mem_detail");
}
*/
//showing additional treatment in contact-form
function getAddTreatment(mem_id, treat_area_id, td_id) {
	var F = document.frm_contact;

	var mainTarea_id = document.getElementById('tarea_id')[document.getElementById('tarea_id').selectedIndex].value;
	var addTarea_id = document.getElementById('addTreatAreaID')[document.getElementById('addTreatAreaID').selectedIndex].value;
	//alert("td_id: "+td_id+"  and "+mainTarea_id+" = "+treat_area_id);
	if(td_id =='additional' && treat_area_id == mainTarea_id) {
		alert('Please select another treatment area. The treatments under this treatment area are already shown above.');
		F.addTreatAreaID.options[0].selected = true;
		document.getElementById(td_id).innerHTML = '';
	}
	else{
		document.getElementById(td_id).innerHTML = "<span class='org-text' align='left'>&nbsp;&nbsp;<b>Loading...</b></span><br><img src='image/loading.gif' width='220' height='19'/>";
		var url = "ajax.php?mem_id="+mem_id+"&tarea_id="+treat_area_id+"&treat_type="+td_id+"&opr=6";
		sendRequest(url,td_id);
	}
	
	// If user wants to make secondary treatment as a main treatment
	if(td_id =='treatment' && treat_area_id == addTarea_id) {
		F.addTreatAreaID.options[0].selected = true;
		document.getElementById('additional').innerHTML = '';
	}
}

//opr 7 => function for showing number of questions in each treatment in ques-ans.php

// showing treatment provided by member in askDoctor.php
function get_member_treatment_list(id,memID){
	var url = "ajax.php?treat_area_id="+id+"&memID="+memID+"&opr=8"; // for admin before-after-list.php
	document.getElementById('td_treatment').innerHTML = "<img src='image/loading.gif' width='220' height='19'/>";
	sendRequest(url,"td_treatment");
}

// showing member before and after photo in healthcare providers tab.
function get_memberBeforeAfterPhoto(id,memID){
	var url = "ajax.php?treat_id="+id+"&memID="+memID+"&opr=9"; // for admin before-after-list.php
	document.getElementById('bef-aft-td').innerHTML = "<span class='org-text' align='left'>&nbsp;&nbsp;<b>Loading...</b></span><br><img src='image/loading.gif' width='220' height='19'/>";
	sendRequest(url,"bef-aft-td");
}

// showing member question and answer in healthcare providers tab.
function get_memberQuesAns(id,memID){
	var tid='';
	if(id!='xx') tid = id;
	var url = "ajax.php?treat_id="+tid+"&memID="+memID+"&opr=10"; // for admin before-after-list.php
	document.getElementById('ques-td').innerHTML = "<br><span class='org-text' align='left'>&nbsp;&nbsp;<b>Loading...</b></span><br><img src='image/loading.gif' width='220' height='19'/>";
	sendRequest(url,"ques-td");
}



// function for smooth scrolling
function getScrollY() {
	
        var scrOfY = 0;

        if( typeof( window.pageYOffset ) == 'number' ) {

                //Netscape compliant
                scrOfY = window.pageYOffset;

        } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {

                //DOM compliant
                scrOfY = document.body.scrollTop;

        } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {

                //IE6 standards compliant mode
                scrOfY = document.documentElement.scrollTop;

        }

        return scrOfY;

}

function scrollto(target_id,speed){

        if(target_id == '') return;

        if(!document.getElementById(target_id)) return;

        var currentypos = getScrollY();

        targetdiv = document.getElementById(target_id);

        var desty = targetdiv.offsetTop;

        var thisNode = targetdiv;

        while (thisNode.offsetParent && (thisNode.offsetParent != document.body)) {

                thisNode = thisNode.offsetParent;
                desty += thisNode.offsetTop;

        }

        // bring you to just above
        desty -= 12;

        if(desty < currentypos){
                for ( I=currentypos; I > desty; I-= speed ){
                        parent.scroll(1,I);
                }
        }else{

                for ( I=currentypos; I < desty; I+= speed ){
                        parent.scroll(1,I);
                }
        }
}

// function for smooth scrolling closed