//global vars
var xmlHttp;
var div_id = 0;
var sDiv_count = 0;
var user_id = "";
var script_id = "";


function GetXmlHttpObject()
{
var xmlHttp=null;

  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }catch (e){
     // Internet Explorer
     try
     {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     }catch (e){
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
   }
 return xmlHttp;
}


function requestShortScriptEmail(index,id, sDiv_id){
	sDiv_count = sDiv_id;
	user_id = id;
	script_id = index;
	alert("Request received. You will be emailed the script shortly. You must reserve a script and contact the writer(s) before you can begin developing it.");
	var url="requestShort.php?index="+index+"&id="+id+"&time="+new Date().getTime();
	//get xmlhttp object
	xmlHttp=GetXmlHttpObject();
	
if (xmlHttp==null){
  alert ("Browser does not support HTTP Request")
  return
} 
  
xmlHttp.onreadystatechange=shortScriptStateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
	
}


function shortScriptStateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
	var id = "request_"+sDiv_count;
	document.getElementById(id).innerHTML = "<a href=\"javascript:requestShortScriptEmail('"+script_id+"','"+user_id+"','"+sDiv_count+"' )\"><h2 class=\"browse_link_unsave\">Request</h2></a>";
 }
}

function searchScripts(){

 var searchStr = document.scriptSearchForm.searchStr.value;
 if(searchStr == ""){
    location.href="index.php";
 }

 var category = document.scriptSearchForm.category.value;
 var img = "<img src=\"images/loading.gif\" width=\"50px\" height=\"50px\" />";

document.getElementById("scriptSection").innerHTML=img;

//get xmlhttp object
xmlHttp=GetXmlHttpObject();


if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 

var url="getScripts.php"
url=url+"?searchStr="+searchStr+"&category="+category+"&time="+new Date().getTime();
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)

} 

function stateChanged() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("scriptSection").innerHTML=xmlHttp.responseText
 } 
}



function saveStateChanged(){

	//<a href="javascript:saveScript('<?echo $_SESSION['email']?>', <?echo $row['id']?>, <?echo $row_index?>)" title="Save"><h2 class="browse_link">Save</h2></a>
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){
		var id = "save_"+div_id;
		document.getElementById(id).innerHTML = "<a href=\"javascript:unSaveScript('"+user_id+"', "+script_id+", "+div_id+")\"><h2 class=\"browse_link_unsave\">Unsave</h2></a>";
		//xmlHttp.responseText;
		//"<h2 class='browse_link'>Unsave</h2>";
		
	}
}

function unSaveStateChanged(){

	if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){
		var id = "save_"+div_id;
		document.getElementById(id).innerHTML = "<a href=\"javascript:saveScript('"+user_id+"', "+script_id+", "+div_id+")\"><h2 class=\"browse_link\">Save</h2></a>";
		
	}
}

function saveScript(uid, sid, div){
	div_id = div;
	user_id = uid;
	script_id = sid;
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp == null){
		alert("Browser does not support HTTP Request");
		return;
	}

	var url = "handleSave.php";
	url = url+"?user_id="+user_id+"&script_id="+script_id+"&time="+new Date().getTime();
	//alert("url: "+url);
		xmlHttp.onreadystatechange=saveStateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}

function unSaveScript(uid, sid, div){
	div_id = div;
	user_id = uid;
	script_id = sid;
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp == null){
		alert("Browser does not support HTTP Request");
		return;
	}

	var url = "handleUnsave.php";
	url = url+"?user_id="+user_id+"&script_id="+script_id+"&time="+new Date().getTime();
	//alert("url: "+url);
		xmlHttp.onreadystatechange=unSaveStateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
