var host = window.location.protocol+ "//www.acpsem.org.au/";

var clientIndex; var clientValue;
var specialtyIndex; var specialtyValue;
var categoryIndex; var categoryValue;
var startDate; var confirmDate; var expiryDate;
var comment;
var contactInfo;

function showDetails()
{
	document.registerForm.contactinfo.value = 'Address1:  \nAddress2:  \nAddress3:  \nSuburb:  \nCity:  \nState:  \nPost Code:  \nCountry:  \nPhone Work:  \nFax:  \nPhone Home:  \nMobile:  \nEmail: ';
}

function addRegister()
{
	if( checkRegistration() == true )
	{
		//Date is in (YYYY-MM-DD) MySql Date Format
		var URL = host+"php/test/mpregister/register.php?";
		var params = "cliID="+clientValue+"&specialty="+specialtyValue+"&category="+categoryValue+"&start="+startDate+"&confirm="+confirmDate+"&expiry="+expiryDate+"&comment="+comment+"&cInfo="+contactInfo+"&update=false";
		AjaxFunction(URL+params,false);
		//alert("Valid "+URL+params);
	}
}

function updateRegister()
{
	if( checkRegistration() == true )
	{
		//Date is in (YYYY-MM-DD) MySql Date Format
		var URL = host+"php/test/mpregister/registergg.php?";
		var params = "cliID="+clientValue+"&specialty="+specialtyValue+"&category="+categoryValue+"&start="+startDate+"&confirm="+confirmDate+"&expiry="+expiryDate+"&comment="+comment+"&cInfo="+contactInfo+"&update=true";
		AjaxFunction(URL+params,false);
		//alert("Valid "+URL+params);
	}
}

function getDetails()
{

		// 
        clientIndex = document.registerForm.clientsList.selectedIndex;
	clientValue = document.registerForm.clientsList.options[clientIndex].value;
		var URL = host+"php/test/mpregister/getdetails.php?";
		var params = "cliID="+clientValue;
		AjaxFunction(URL+params,false);
		//alert("Valid "+URL+params);

}

function checkRegistration()
{
	clientIndex = document.registerForm.clientsList.selectedIndex;
	clientValue = document.registerForm.clientsList.options[clientIndex].value;

	specialtyIndex = document.registerForm.specialtiesList.selectedIndex;
	specialtyValue = document.registerForm.specialtiesList.options[specialtyIndex].value;

	categoryIndex = document.registerForm.registrationCategoryList.selectedIndex;
	categoryValue = document.registerForm.registrationCategoryList.options[categoryIndex].value;

	startDate = document.registerForm.sdate.value;     //Date is in (YYYY-MM-DD) MySql Date Format
	confirmDate = document.registerForm.cdate.value;
	expiryDate = document.registerForm.edate.value;

	comment = document.registerForm.comment.value;
	contactInfo = document.registerForm.contactinfo.value;

	if(clientValue == 0)   //empty Client
	{
		alert("Please select a Client");
		return false;
	}
	else
	{
		if(specialtyValue == 0)   //empty Specialty
		{
			alert("Please select a Specialty");
			return false;
		}
		else
		{
			if(categoryValue == 0)   //empty Category
			{
				alert("Please select a Category");
				return false;
			}
			else
			{
				if( startDate == "" )    //empty Applied Date
				{
					alert("Please enter the Applied Date");
					return false;
				}
				else     //Empty Confirmed Date and Expiry Date
					return true;
			}
		}
	}
	
	//return true;
	//alert(clientValue+" "+specialtyValue+" "+categoryValue+" "+
	//startDate+"-"+startMonth+"-"+startYear+" "+confirmDate+"-"+confirmMonth+"-"+confirmYear+" "+expiryDate+"-"+expiryMonth+"-"+expiryYear+" "+comment+" "+contactInfo);
}

function search()
{
	var str = document.searchRegisterForm.rname.value;       //input name
	var stateIndex = document.searchRegisterForm.statesList.selectedIndex;
	var state = document.searchRegisterForm.statesList.options[stateIndex].value;
	
	var specialtyIndex = document.searchRegisterForm.specialtiesList.selectedIndex;
	var specialty = document.searchRegisterForm.specialtiesList.options[specialtyIndex].value;
	
	//alert(str+" "+state+" "+specialty);
	
	//determine if the search input is NULL
	//if (str.length <= 1)
	//{ 
	//	document.getElementById("searchRegisterResult").innerHTML="";
		//document.getElementById("searchRegisterResult").style.border="0px";
	//}
	//else
	//{
		var URL = host+"php/test/mpregister/search.php?";
		var params = "js=1&q="+str;
		
		if( state!='0' )
			params += "&state="+state;
			
		if( specialty!='0' )
			params += "&sid="+specialty;
		
		AjaxFunction(URL+params,true);
	//}
}

//This function will make an AJAX call with the given URL to be called in the parameter
//search is boolean value indicates to update certain section in the html when the search result returned 
function AjaxFunction(myURL,search)
{
	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;
			}
		}
	}

	// Link to display function activated when result returned
	xmlHttp.onreadystatechange=function()
	{
		// Check to make sure result came through, 4=complete
		if (xmlHttp.readyState == 4)
		{
			if(search == true)   //indicate search functionality and updates the corresponding <div> section
				document.getElementById("searchRegisterResult").innerHTML=xmlHttp.responseText;
			else
				alert(xmlHttp.responseText);
			//document.registerForm.contactinfo.value = xmlHttp.responseText;
		}
	}
	
	// Open the URL request
	xmlHttp.open("GET",myURL,true);
	// Send request
	xmlHttp.send(null);
}

