	// Define rype proxy url (should be www.yourdomain.com/rype_proxy.php or similar
	var rype_proxy_url = "http://www.rypenow.com/rype_proxy.php";
	
	// Create an XMLHttpRequest object
	var http = createRequestObject();  
	
	// Define some variables to maintain AJAX state
   	var ajaxaction = "";	
	var inProgress = false; 
	
	// Define global recordsets
	var rstCountries;

	window.onload = function() {
		getRypeData();
	}
	   
   	function getRypeData()
 	{
   		get_countries();	
	}
	
	function validate_form()
	{
		tf = document.frmProject;
		
		if(tf.project_name.value == "")
		{
			alert("Please enter a job name");
			tf.project_name.focus();
			return false;
		}
		
		if(tf.cname.value == "")
		{
			alert("Please enter the customer's name");
			tf.cname.focus();
			return false;
		}	
		
		if(tf.cemail.value == "")
		{
			alert("Please enter the customer's email address");
			tf.cemail.focus();
			return false;
		}	
		
		if(tf.ccompany.value == "")
		{
			alert("Please enter the customer's company name");
			tf.ccompany.focus();
			return false;
		}			
						
		
		if(tf.status.selectedIndex == 0)
		{
			alert("Please select a status");
			tf.status.focus();
			return false;
		}
		
		return true;							
	}
	
	function create_account()
	{	
		var tf = document.frmAccount;			
		
		var d = new Date();
		var today = d.getDate() + "/" + ((d.getMonth() * 1) + 1) + "/" + d.getFullYear();
		
		if($("#state").css("display") == "block")
			var state = tf.state.options[tf.state.selectedIndex].text;
		else
			var state = tf.state_other.value;
					
		var xml = '<account>';
		
		xml += '<username>' + tf.username.value + '</username>'; 
		xml += '<password>' + tf.password.value + '</password>'; 
		xml += '<company_name>' + tf.company_name.value + '</company_name>';
		xml += '<country>' + tf.country.options[tf.country.selectedIndex].value + '</country>';
      xml += '<address1>' + tf.address1.value + '</address1>'; 
      xml += '<address2>' + tf.address2.value + '</address2>';
      xml += '<suburb>' + tf.suburb.value + '</suburb>';
      xml += '<state>' + state + '</state>';
      xml += '<postcode>' + tf.postcode.value + '</postcode>';                             
      xml += '<first_name>' + tf.first_name.value + '</first_name>';
      xml += '<last_name>' + tf.last_name.value + '</last_name>';
		xml += '<email>' + tf.email.value + '</email>';
		
		xml += '</account>';
		
		// Send the request to the server
		var params = "method=account/create&data=" + escape(xml);
		sndReq("create_account", params);
	}
	
	function handle_create_account(response)
	{
		if(response == "OK")
		{
			$("#frmAccount").css("display", "none");
			//$("#message").html("Thank you!  Your Rype account has been created successfully and you can now login using Rype Desktop.");	
			$("#message").html("Thank you!  Please wait....");
			parent.window.location = 'http://www.rypenow.com/get-started/30-day-free-trial/download-now/';
		}
		else
		{
			alert(response);	
		}
	}
	
	function handle_add_project(response)
	{
		if(response == "")
		{
			alert("The server did not return a valid response");
			return;
		}
		
		var pos = response.indexOf("OK");
		if(pos == -1)
		{
			alert("There was an error whilst trying to add the project to Rype.  The server returned: " + response);
			return;
		}
		
		var elements = response.split(" ");
		
		if(elements.length != 2)
		{
			alert("The server returned an invalid response");
			return;
		}		
		
		var new_project_id = elements[1];
		
		alert("The project was added successfully to Rype.  It's ID is: " + new_project_id);				
	}
	
	function get_countries()
	{
		// Build the param string to send to Rype and invoke the method to get the countries list.
		var params = "method=geography/get_countries&order_by=country&order_dir=asc&output_type=json";
		sndReq("get_countries", params);	
	}	

	function handle_get_countries(response)
	{
		var rst = eval('(' + response + ')');
		var num_recs = rst.rows.length;
		rstCountries = rst;
		
		var objSelect = document.getElementById("country");
		if(!objSelect)
			return;
			
		// Clear the select list
		objSelect.options.length = 0;
		
		// Now add the client records back in
		objSelect.options[0] = new Option("Choose", "", true, false);
		
		for(x = 1; x <= num_recs; x++)
		{
			objSelect.options[x] = new Option(rst.rows[x-1].country, rst.rows[x-1].country, false, false);
		}
		
		// Now load the custom fields
		//get_customfields();	
	}
	
	function loadStates()
	{
		var objSelect = document.getElementById("country");
		if(!objSelect)
			return;
			
		if(objSelect.selectedIndex > 0)
		{
			var country = objSelect.options[objSelect.selectedIndex].value;
			
			// Switch the captions to the appropriate terminology for the country
			var state_caption = rstCountries.rows[objSelect.selectedIndex - 1].state_caption;
			var postcode_caption = rstCountries.rows[objSelect.selectedIndex - 1].postcode_caption;
			
			document.getElementById("state_caption").innerHTML = state_caption; 
			document.getElementById("postcode_caption").innerHTML = postcode_caption; 

			// Build the param string to send to Rype and invoke the method to get the states list.
			var params = "method=geography/get_states&country=" + escape(country) + "&order_by=state&order_dir=asc&output_type=json";
			sndReq("get_states", params);				
		}			
	}
	
	function handle_get_states(response)
	{
		if(response == "NO RESULTS")
		{
			$("#state").css("display", "none");
			$("#state_other").css("display", "block");
			$("#state_other").attr("disabled", "");
		}
		else
		{
			$("#state_other").css("display", "none");			
			$("#state").css("display", "block");
			$("#state_other").attr("disabled", "disabled");
			
			var rst = eval('(' + response + ')');
			var num_recs = rst.rows.length;
			
			var objSelect = document.getElementById("state");
			if(!objSelect)
				return;
				
			// Clear the select list
			objSelect.options.length = 0;
			
			// Now add the client records back in
			//objSelect.options[0] = new Option("Choose", "", true, false);
			
			for(x = 0; x < num_recs; x++)
			{
				objSelect.options[x] = new Option(rst.rows[x].state, rst.rows[x].state_id, false, false);
			}
		}
	}
	
	function check_username()
	{
		var obj = document.getElementById("username");
		if(!obj)
			return;
			
		if((obj.value == "") || (obj.value.length < 3))
			alert("Please enter a username to check.  Your username must be at least 3 characters long.");
			
		// Build the param string to send to Rype and invoke the method to check the username.
		var params = "method=account/check_username&un=" + escape(obj.value);
		sndReq("check_username", params);				
		
	}	
	
	function handle_check_username(response)
	{
		objSpan = document.getElementById("check_message");
		if(!objSpan)
			return;
						
		if(response == "OK")
		{	
			objSpan.innerHTML = "Perfect! This username is available.";
			objSpan.setAttribute("class", "OK");	
		}
		else
		{
			objSpan.innerHTML = "Sorry, this username has been taken.";
			objSpan.setAttribute("class", "TAKEN");	 	
		}
	}
	
   function createRequestObject() 
   {
       var ro;
       var browser = navigator.appName;
       
       if(browser == "Microsoft Internet Explorer")
       {
           ro = new ActiveXObject("Microsoft.XMLHTTP");
       }
       else
       {
           ro = new XMLHttpRequest();
       }
       
       return ro;
   }

   function sndReq(action, params) 
   {
   	ajaxaction = action;
   	
   	showRypeLoader();
   	
   	http.open('POST', rype_proxy_url, true);
   	http.onreadystatechange = handleResponse;
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");       
      http.send(params);

		inProgress = true;  
   }
   
   function showRypeLoader()
   {
   	var objLoader = document.getElementById("rype-loader");
   	if(!objLoader)
   		return;

   	objLoader.style.display = "block";     		
	}
	
   function hideRypeLoader()
   {
   	var objLoader = document.getElementById("rype-loader");
   	if(!objLoader)
   		return;
   	
   	objLoader.style.display = "none";      		
	}	

   function handleResponse() 
   {
      if(http.readyState == 4)
      {
      	inProgress = false;
      	hideRypeLoader();
      	
         var response = http.responseText;
         
         if(response != "")
         {
         	if(response.substr(0,1) == "#")
         		alert("Warning: " + response.substr(1,response.length - 1));
				else
				{
		         if(ajaxaction == "get_countries")
		            handle_get_countries(response);
		         else if(ajaxaction == "get_states")
		            handle_get_states(response);
		         else if(ajaxaction == "check_username")
		            handle_check_username(response);
		         else if(ajaxaction == "create_account")
		            handle_create_account(response);			            			            			            	            		         	       	         		         		         			         		         								
					else
						alert("Undefined action: " + ajaxaction);
				}
			}
      }
   }	
