var http_request = false;

//////////////////////////////////////////////////////////////////////////
function launchWindow(url){
//<!-- firefox then ie, methods are different for showing child window -->
if (!document.all) { oWindow = window.open(url,
'We-Care', 'width=600,height=400,resizable=yes,status=yes,scrollbars=yes,dialog=yes');}
else{
javascript:window.showModalDialog(url,'',
'dialogHeight:400px;dialogWidth:600px;help:off;status:yes;unadorned:no;');
}
}


//////////////////////////////////////////////////////////////////////
function addFav()
{
  if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(document.title, location.href, "");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( location.href, document.title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
} // End function AddFavorite



//////////////////////////////////////////////////////////////////////
// This is the function that is actually 
//  Called from the store page 
function showDesc(oStore)
{
  var poststr = "store=" + encodeURI( oStore );
  makePOSTRequest('/getDescAjax.php', poststr, 1);
  return true;
}

//////////////////////////////////////////////////////////////////////
// This is the function that is  
//  Called from the admin page to get alexa rank 
function getRank()
{
	var oDomain = document.getElementById('domain');
	var oURL = oDomain.value;
	alert ('Please wait.  Getting rank info for ' + oURL);
	if (oURL == "")
	{
		alert("'Domain' must contain a valid URL to look up!");
		return false; 
	}
  var poststr = "url=" + encodeURI( oURL );
  makePOSTRequest('/getAlexaRank.php', poststr, 2);
}

//////////////////////////////////////////////////////////////////////

function makePOSTRequest(url, parameters, from) 
{
   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;
   }
   if (from == 1){
   	http_request.onreadystatechange = alertContents;
   }
   if (from == 2){
   	http_request.onreadystatechange = putAlexaRank;
   }
   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);
}

//////////////////////////////////////////////////////////////////////
function alertContents() {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         //alert(http_request.responseText);
         result = http_request.responseText;
         var oDivDesc = document.getElementById('companyOverview');
   	 oDivDesc.innerHTML = result;
      } else {
         alert('There was a problem with the request.');
      }
   }
}

//////////////////////////////////////////////////////////////////////
function putAlexaRank() {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         //alert(http_request.responseText);
         result = http_request.responseText;
         if (result > 0 && result < 999999999)
         {
         	var txtRank = document.getElementById('rank');
   	 		 	if (txtRank.value == result){
   	 		 		alert('Value is unchanged.');
   	 		 		return true;
   	 		 	}
   	 		 	txtRank.value = result;
   	 		 	alert('Don\'t forget to save so that changes commit!');
   	 		 }
   	 		 else{
   	 		 	alert('Error getting rank. '+ result);
   	 		 }
      } else {
         alert('There was a problem with the request.');
      }
   }
}



//////////////////////////////////////////////////////////////////////
function changeStoreID(store_id) {
	if (store_id.value != ""){
		document.getElementById('another').value = store_id.value;
		document.getElementById('').value = 'Add Another ' +  store_id.value + ' Coupon';
	}
}
