var http_request = false;

function makeGETRequest(url, callback_func) {

  if (window.XMLHttpRequest) { // Mozilla, Safari, IE7...
      http_request = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE6 and older
      http_request = new ActiveXObject("Microsoft.XMLHTTP");
  }
  http_request.onreadystatechange = function() {
       if (http_request.readyState == 4) {
           if (http_request.status == 200) eval(callback_func + '()');
			else if (http_request.status != 0){ //some versions of IE can return 0
               alert('There was a problem with the request.(Code: ' + http_request.status + ')');
           }
       }
   }
  http_request.open('GET', url, true);
  http_request.send(null);
}

function makePOSTRequest(url, callback_func, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
  }
  
   http_request.onreadystatechange = function() {
       if (http_request.readyState == 4) {
           if (http_request.status == 200)
		   {
			   eval(callback_func + '()');
		   }
			else if (http_request.status != 0){ //some versions of IE can return 0
               alert('There was a problem with the request.(Code: ' + http_request.status + ')');
           }
       }
   }
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}


// get all form's elements with their values ^.^ generic ;)
function get_form_element(form) {
	var str = "isAjax=ajax";
	for (i=0; i<form.elements.length; i++)
	{
		// checkbox and radio buttons are a little different, deal with them here!
		if ((form.elements[i].type == "checkbox" || form.elements[i].type == "radio") && form.elements[i].checked == false)
		{
			form.elements[i].value = 0;
		}
		str += "&" + form.elements[i].name + "=" + encodeURI(form.elements[i].value);	
	}
	return str;
}

// popup window
function popup(url) { window.open(url,'','width=350,height=300,scrollbars=1'); }

// tracking popup window
function trackingpopup(url)
{
	document.getElementById("tracksuccess").style.display = "block";
	newWindow = window.open(url,'PopUp','width=350,height=300,scrollbars=1');
	newWindow.blur();
}

// store popup window
function storepopup(store, url)
{
	newWindow = window.open(store,'PopUp','width=350,height=300,scrollbars=1');
	newWindow.blur();
	window.location.href= url;

}

function check_login() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		document.getElementById("errordiv").innerHTML = http_request.responseText;
		document.getElementById("errordiv").style.display = "block";
	   if (http_request.responseText == "Login success! Redirect...")
	   {
		   setTimeout("window.location='deals.html'", 1000);
	   }
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function check_login_form() {
  var poststr = "isAjax=ajax&case=" + encodeURI( document.getElementById("case").value ) +
				"&username=" + encodeURI( document.getElementById("username").value ) +
				"&password=" + encodeURI( document.getElementById("password").value ) +
				"&remember=" + encodeURI( document.getElementById("remember").value );
  makePOSTRequest('login.html', 'check_login', poststr);
}

function check_register() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		document.getElementById("errordiv").innerHTML = http_request.responseText;
		document.getElementById("errordiv").style.display = "block";
	   if (http_request.responseText == "Successfully Registered! Redirecting to Login...")
	   {
		   setTimeout("window.location='register.html?error=Please Login to continue.'", 1000);
	   }
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function check_register_form(form) {
	var poststr = get_form_element(form);
  makePOSTRequest('register.html', 'check_register', poststr);
}

function check_adddeals() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		document.getElementById("errordiv").innerHTML = http_request.responseText;
		document.getElementById("errordiv").style.display = "block";
	   if (http_request.responseText == "Successfully Registered! Redirecting...")
	   {
		   // do nothing;
	   }
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function check_adddeal_form(form) {
	var poststr = get_form_element(form);
  makePOSTRequest('account_insertdeal.html', 'check_adddeals', poststr);
}

function get_subcat() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		document.getElementById("subcatdiv").innerHTML = http_request.responseText;
		document.getElementById("subcatdiv").style.display = "inline";
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function get_subcat_form() {
	var cat = document.getElementById("category").value;
	makeGETRequest('account_getsubcat.html?isAjax=ajax&cat=' + cat, 'get_subcat');
}

function expandrebate() {
	document.getElementById("rebate").style.display = "inline";
	document.getElementById("rblink").focus();
}

function check_adddeal() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		document.getElementById("errordiv").innerHTML = http_request.responseText;
		document.getElementById("errordiv").style.display = "block";
	   if (http_request.responseText == "Thank you! Your <b>Deal</b> is successfully Added! Redirecting...")
	   {
		   setTimeout("window.location='account.html'", 1000);
	   }
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function check_adddeal_form(form) {
	var poststr = get_form_element(form);
	makePOSTRequest('account.html', 'check_adddeal', poststr);
	return false;
}

function check_updatedeal() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		document.getElementById("errordiv").innerHTML = http_request.responseText;
		document.getElementById("errordiv").style.display = "block";
	   if (http_request.responseText == "Okay! Your <b>Deal</b> is updated! Redirecting...")
	   {
		   setTimeout("window.location='account.html'", 1000);
	   }
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function check_updatedeal_form(form) {
	var poststr = get_form_element(form);
	poststr += "&description=" + encodeURI( document.getElementById("description").value );
	alert(poststr);
	makePOSTRequest('account.html', 'check_updatedeal', poststr);
	return false;
}

function get_link() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		if (http_request.responseText == "ok")
		{
			document.getElementById("errorlinkdiv").style.display = "none";
		}
		else
		{
			document.getElementById("errorlinkdiv").innerHTML = http_request.responseText + "<br />";
			document.getElementById("errorlinkdiv").style.display = "inline";
			document.getElementById("errorlinkdiv").focus();
		}
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function get_ls() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		document.getElementById("lsresult").innerHTML = http_request.responseText;
		document.getElementById("lsresult").style.display = "inline";
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function get_amazon() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		document.getElementById("amazonresult").innerHTML = http_request.responseText;
		document.getElementById("amazonresult").style.display = "inline";
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function get_search_result(lslink, amazonlink) {
	makeGETRequest(lslink, 'get_ls');
	//makeGETRequest(amazonlink, 'get_amazon');
}

function get_woot() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		document.getElementById("wootdiv").innerHTML = http_request.responseText;
		document.getElementById("wootdiv").style.display = "inline";
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function get_woot_form() {
	makeGETRequest('woot_grab.html?isAjax=ajax', 'get_woot');
}

function get_deal() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		document.getElementById("dealdiv").innerHTML = http_request.responseText;
		document.getElementById("dealdiv").style.display = "inline";
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function get_deal_form(durl) {
	makeGETRequest(durl, 'get_deal');
}

function get_dealamazon() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		document.getElementById("amazondealdiv").innerHTML = http_request.responseText;
		document.getElementById("amazondealdiv").style.display = "inline";
      } else {
          alert('There was a problem with the request.');
      }
  }
}

function get_dealamazon_form(durl) {
	makeGETRequest(durl, 'get_dealamazon');
}

function check_link() {
	var link = document.getElementById("url").value;
	makeGETRequest('account_checklink.html?isAjax=ajax&url=' + link, 'get_link');
}

function callwootnow()
{
	get_woot_form('woot_grab.html?isAjax=ajax');
	setTimeout("callwootnow()", 5000);
}

function toggleBox(divID, state)
{
   if (state == "1") document.getElementById(divID).style.display = "block";
   else document.getElementById(divID).style.display = "none";
}

