/** Author: Daniel Stephenson
  *  Date: --/--/2007
  *  Filename: ajaxFuncs.js
  *  Function: ajax js for site
  *  History: 
*/


/* This function contacts server side using regular ajax */

function ajaxFunction(returnFunc, url, params, posttype) {
	var ajaxRequest;    // Main ajax var

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// IE
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}

  // function to receive data from server
  ajaxRequest.onreadystatechange = function() {//Call a function when the state changes.
	  if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
		  //alert(ajaxRequest.responseText);
		  
		  result = ajaxRequest.responseText;

		  if(returnFunc == "ajaxSnippetCodeHighlight"){ ajaxSnippetCodeHighlight("", "RECEIVE", result, "", "") }
		  if(returnFunc == "ajaxPasteCodeHighlight"){ ajaxPasteCodeHighlight("", "RECEIVE", result, "", "") }
		  if(returnFunc == "ajaxRatePost"){ ajaxRatePost("", "RECEIVE", result, "", "") }
	  }
  }

  if(posttype == "GET"){
    ajaxRequest.open("GET", url+"?"+params, true);
    ajaxRequest.send(null);
  }else if(posttype == "POST"){
    ajaxRequest.open("POST", url, true);

    // send header info with request
    ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxRequest.setRequestHeader("Content-length", params.length);
    ajaxRequest.setRequestHeader("Connection", "close");

    ajaxRequest.send(params);
  }
}

/* ajaxSnippetCodeHighlight - get a posts pagedata with code/syntax highlighted */

function ajaxSnippetCodeHighlight(base_path, action, returnValue, postid, codetype) {
	if(action == "SEND"){
		//ajaxLoading on
		document.getElementById("codeHighlight").style.display = 'none';
		document.getElementById("ajaxLoading").style.display = 'block';
		
    var params = "ajaxsnippethighlight=1" +
                 "&postid=" + postid +
                 "&codetype=" + codetype;

    ajaxFunction("ajaxSnippetCodeHighlight", base_path+"process.php", params, "GET");
  }
  else if(action == "RECEIVE"){
		//ajaxLoading on
		document.getElementById("codeHighlight").style.display = 'block';
		document.getElementById("ajaxLoading").style.display = 'none';
  	
    document.getElementById("codeHighlight").innerHTML = returnValue;
  }
}

/* ajaxPasteCodeHighlight - get a pastes pagedata with code/syntax highlighted */

function ajaxPasteCodeHighlight(base_path, action, returnValue, postid, codetype) {
	if(action == "SEND"){
		//ajaxLoading on
		document.getElementById("codeHighlight").style.display = 'none';
		document.getElementById("ajaxLoading").style.display = 'block';
		
    var params = "ajaxpastehighlight=1" +
                 "&postid=" + postid +
                 "&codetype=" + codetype;

    ajaxFunction("ajaxPasteCodeHighlight", base_path+"process.php", params, "GET");
  }
  else if(action == "RECEIVE"){
		//ajaxLoading on
		document.getElementById("codeHighlight").style.display = 'block';
		document.getElementById("ajaxLoading").style.display = 'none';
  	
    document.getElementById("codeHighlight").innerHTML = returnValue;
  }
}

/* ajaxRatePost - rate a post and retrieve new rating */

function ajaxRatePost(base_path, action, returnValue, postid, postrating) {
	if(action == "SEND"){
    var params = "ajaxratepost=1" +
                 "&postid=" + postid +
                 "&userrate=" + postrating;

    ajaxFunction("ajaxRatePost", base_path+"process.php", params, "GET");
  }
  else if(action == "RECEIVE"){
    document.getElementById("postRating").innerHTML = returnValue;
    document.getElementById('rstars-enabled').style.display = 'none';
    document.getElementById('rstars-disabled').style.display = 'block';
  }
}

/* This function is called when user selects an ajax file upload */

function ajaxFileUpload(upload_form, upload_field)
{
  upload_form.submit();
  document.getElementById('upload_status').innerHTML = 'uploading file...';
  document.getElementById('ajaxLoading').style.display = 'block';

  upload_field.disabled = true;
  return true;
}