// Ajax course contact JavaScript file: 
// Author: David Butcher, MSc., BSc., MIEEE,
// version 1.0
	
// create standard XMLHttpRequest holder
var xmlHttp = '';
xmlHttp = null;
xmlHttp = createXmlHttpRequestObject();


var type= "";
var currency ="";
var show_courses = getElementbyID('show_courses');
var disp_div ='';


// creats an XMLHttpRequest instance
function createXmlHttpRequestObject(){

	//var xmlHttp = null;
	//browser dependent creation try a native object first
	try{    
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHttp");
		
		
		// try each element
		for(var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++)
		{
			try{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
 			}
			catch (e){			
			
				xmlHttp= new ActiveXObject("Microsoft.XMLHttp");
			}
		}
	}

	//return the created object or display error
	if(!xmlHttp)
		alert("Error creating the XMLHttpRequest Object. myAjax.js");
	else
	{    
		return xmlHttp;
	}	
}

//called to read files from server
function getDetails(type,curr){
	//alert("in Process: " + type);	
	
 	if(xmlHttp){
		// yes so connect to server
		
		try{
			xmlHttp.async = false;
				
			switch(type){
				case 'paypal':
					xmlHttp.open("GET", "paypal.php?currency=" + curr, true);
					break;
				case 'clickbank':
					xmlHttp.open("GET", "clickbank.php?currency=" + curr, true);

					break;
				case 'mail':
					xmlHttp.open("GET", "mail.php?currency=" + curr, true);
					break;	
				default:
					xmlHttp.open("GET", "paypal.php?currency=" + curr, true);	
			}		
			//alert(type);

			xmlHttp.onreadystatechange = handleRequestStateChange;			
			xmlHttp.send(null);			
		}
		// catch any errors
		catch (e){			
			xmlHttp.abort();
			alert("Can't connect to server. process myAjax. " + e.toString())
		}
	}
	else
		alert("no xmlHttp object in process");
}


function handleRequestStateChange(){

 
 	//alert("ReadyState = " + xmlHttp.readyState);
   	//alert("Status = " + xmlHttp.status.toString());
	if(xmlHttp.readyState == 4){
		//myStaffDetails.innerHTML += "Request status: 4 (ready) <br />";		
		//check status is OK
		if(xmlHttp.status == 200){			
			try{				
				//read message
				response = xmlHttp.responseText;
				//get reference to display div
				var dispDiv = document.getElementById('show_courses');
				//display message				
				dispDiv.innerHTML = response;				
			}
			catch (e){
				//if fail display error
				alert("Error reading the reponse: myajax handleRequestStateChange<br />" + e.toString());
			}
		}
		else{
			// status is not ok
			alert("Error there was a problem reading the data:\n" + xmlHttp.statusText);
		}
	}
}


function getType(){
  type = 'paypal';
  currency = 'USD';  
  for (var i=0; i < document.pay_method.pay_by.length; i++){		
	 	
		if (document.pay_method.pay_by[i].checked)
		{
			type = document.pay_method.pay_by[i].value;
			
		}
	
   }	
   for (var i=0; i < document.pay_method.currency.length; i++){		
	 	
		if (document.pay_method.currency[i].checked)
		{
			currency = document.pay_method.currency[i].value;
			
		}
	
   }
   	
   getDetails(type,currency);	
}

