var http_request = false;
var go=1; // Set to one here for coupon link.  

// scrollbars must be yes or no.  
function ChildPop(file, w_width, w_height, scrollbars)
{
	var child = window.open(file,'child','height='+w_height+',width='+w_width+',menubar=no,status=no,location=no,toolbar=no,scrollbars='+scrollbars+',resizable=no,dialog=yes');
	/*
	if(!document.all)
	{
		var child = window.open(file,'child','height='+w_height+',width='+w_width+',menubar=no,status=no,location=no,toolbar=no,scrollbars=yes,resizable=no,dialog=yes');
	}
	else	
	{
		//javascript:window.showModalDialog(file,'','dialogHeight:330px;dialogWidth:'+w_width+'px;help:off;status:no;unadorned:yes;');	
		javascript:window.showModalDialog(file,'','dialogHeight:'+w_height+'px;dialogWidth:'+w_width+'px;help:off;status:no;unadorned:yes;');	
	}
	*/
} // end function ChildPop()

//////////////////////////////////////////////////////////////////////////
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=no');
}
else{
	javascript:window.showModalDialog(url,'','dialogHeight:400px;dialogWidth:600px;help:off;status:yes;unadorned:no;');
}
return 2;
}

//////////////////////////////////////////////////////////////////////
function getElem(id){
	if(document.getElementById)
		return document.getElementById(id);
	else if (document.all)
		return document.all[id];
}
//////////////////////////////////////////////////////////////////////
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

/* 
*  Copyright 2006-2007 Dynamic Site Solutions.
*  Free use of this script is permitted for non-commercial applications,
*  subject to the requirement that this comment block be kept and not be
*  altered.  The data and executable parts of the script may be changed
*  as needed.  Dynamic Site Solutions makes no warranty regarding fitness
*  of use or correct function of the script.  Terms for use of this script
*  in commercial applications may be negotiated; for this, or for other
*  questions, contact "license-info@dynamicsitesolutions.com".
*
*  Script by: Dynamic Site Solutions -- http://www.dynamicsitesolutions.com/
*  Last Updated: 2007-06-17
*/

//IE5+/Win, Firefox, Netscape 6+, Opera 7+, Safari, Konqueror 3, IE5/Mac, iCab 3

function addFav2(){
    makePOSTRequest('Scripts/clickTracker.php','type=bookmark',0);

    var ua=navigator.userAgent.toLowerCase();
    var isKonq=(ua.indexOf('konqueror')!=-1);
    var isSafari=(ua.indexOf('webkit')!=-1);
    var isMac=(ua.indexOf('mac')!=-1);
    var buttonStr=isMac?'Command/Cmd':'CTRL';

    if(window.external && (!document.createTextNode ||
      (typeof(window.external.AddFavorite)=='unknown'))) {
        // IE4/Win generates an error when you
        // execute "typeof(window.external.AddFavorite)"
        // In IE7 the page must be from a web server, not directly from a local 
        // file system, otherwise, you will get a permission denied error.
        window.external.AddFavorite(url, title); // IE/Win
    } else if(isKonq) {
      alert('You need to press CTRL + B to bookmark our site.');
    } else if(window.opera) {
      void(0); // do nothing here (Opera 7+)
    } else if(window.home || isSafari) { // Firefox, Netscape, Safari, iCab
      alert('You need to press '+buttonStr+' + D to bookmark our site.');
    } else if(!window.print || isMac) { // IE5/Mac and Safari 1.0
      alert('You need to press Command/Cmd + D to bookmark our site.');    
    } else {
      alert('In order to bookmark this site you need to do so manually '+
        'through your browser.');
    }	
}


//////////////////////////////////////////////////////////////////////
// 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('/admin/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;
   }
   else if (from == 2)
   {
   	http_request.onreadystatechange = putAlexaRank;
   }
   else if (from == 3)
   {
   	http_request.onreadystatechange = putCouponEnabled;
   }
   else if(from == 4)
   {
	http_request.onreadystatechange = showUserForm;
   }
   else if(from == 5)
   {
   	http_request.onreadystatechange = showContactForm;
   }
   else if (from != "")
   {
   	http_request.onreadystatechange = from;   
   }
   else
   {
   	http_request.onreadystatechange = onreadystatedonothing;
   }
   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 onreadystatedonothing() {
   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 to show edit user information

function showUserForm() {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         result = http_request.responseText;
//	alert(result);
         var userForm = getElem('userform');
 	userForm.innerHTML ='<div><b> EDITING USER INFORMATION</b> <a href="Javascript.void(0);" onclick="close_form(); return false;">X </a></div>' +  result;
	userForm.style.display='block';
	var tmpTop = getScrollingPosition();
	tmpTop = tmpTop[1];
	tmpTop = tmpTop + 300;
	userForm.style.top= tmpTop+'px';
      } else {
         alert('There was a problem with the request.');
      }
   }
}

//////////////////////////////////////////////////////////////////////
//function to close the user form

function close_form() {

         var userForm = getElem('userform');
         userForm.innerHTML ='';
        userForm.style.display='none';
}

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

//function to show edit Contact information

function showContactForm() {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         result = http_request.responseText;
//	alert(result);
         var userForm = getElem('contactform');
         userForm.innerHTML ='<div><b> EDITING CONTACT INFORMATION</b> <a href="Javascript.void(0);" onclick="close_contact_form(); return false;">X </a></div>' +  result;
	userForm.style.display='block';
	var tmpTop = getScrollingPosition();
	tmpTop = tmpTop[1];
	tmpTop = tmpTop + 300;
	userForm.style.top= tmpTop+'px';
      } else {
         alert('There was a problem with the request.');
      }
   }
}

//////////////////////////////////////////////////////////////////////
//function to close the Contact form

function close_contact_form() {

         var userForm = getElem('contactform');
         userForm.innerHTML ='';
        userForm.style.display='none';
}

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


// Function that (should) get the top of the window with scrollHeight
// To make sure popup divs show within visible context.
function getScrollingPosition()
{
	var position = [0, 0];
	if (typeof window.pageYOffset != 'undefined')
	{
		position = [
			window.pageXOffset,
			window.pageYOffset
		];
	}
	else if (typeof document.documentElement.scrollTop
	!= 'undefined' && document.documentElement.scrollTop > 0)
	{
		position = [
			document.documentElement.scrollLeft,
			document.documentElement.scrollTop
		];
	}
	else if (typeof document.body.scrollTop != 'undefined')
	{
		position = [
			document.body.scrollLeft,
			document.body.scrollTop
		];
	}
	return position;
}

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



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';
	}
}

function showPTPPopup(){
	var pop_content = '<div id="popup" style="display:block;visibility:visible;position:absolute;left:40%;top:20%;z-index:4;background-color:#FFFFFF;" class="popup">'
			+ '<h2><a href="Javascript:hidePTPPopup();" target="_self"><img src="'+CDN_BASE_URL+'images/close.jpg"></a>What is This?</h2>'
			+ '<div class="popupContent">'
			+ '<p>'
			+ 'Didit is beta testing We-Care.com\'s new product and allowing a portion of what you spend online to be donated to the American Cancer Society at no cost to you.'
			+ '<br /><br />'
			+ 'If you have any questions, please contact Bill Snyder. If you find any technical issues, please contact Bryan Cockerham or email '
			+ '<a href="mailto:PTPsupport@we-care.com">PTPsupport@we-care.com</a>.'
			+ '</p>'
			+ '</div>'
			+ '</div>';
	document.write(pop_content);
}
function hidePTPPopup(){
	getElem('popup').style.display='none';
	getElem('popup').style.visibility='hidden';
}
