// JavaScript Document
function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        //that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}

function ajaxCall(_page ,_stg) {
	var req = new ajaxObject(WEBROOT+'ajax/'+_page);
	//req.callback = function(_resp) { alert(_resp); };
	req.update(_stg, 'POST');
	return req;
}

function updateAffiliateSelect(_merchantId, _selectedLink) {
	var req = new ajaxObject('../ajax/ajax.affiliateLink.php');
	req.callback = function(_resp) { onAffiliateSelectResponse(_resp); }
	req.update('merchantId='+_merchantId+'&selectedLink='+_selectedLink, 'POST');
}

function updateBannerSelect(_storeId, _selectName, _selectedLink) {
	var req = new ajaxObject('../ajax/ajax.bannerSelect.php');
	req.callback = function(_resp) { onAffiliateSelectResponse(_resp); }
	req.update('storeId='+_storeId+'&selectName='+_selectName+'&selectedLink='+_selectedLink, 'POST');
}
function showComments(_offerId, _index) {
	var obj = document.getElementById('comment'+_index);
	if(obj.commentsHide) obj.commentsHide.doToggle();
	else{
		var req = ajaxCall('ajax.offerComments.php' ,'offerId='+_offerId+'&index='+_index);
		req.callback = function(_resp) { onShowCommentsResponse(_resp, _index); };
	}
}
function onShowCommentsResponse(_resp, _index) {
	var obj = document.getElementById('comment'+_index);
	obj.style.display = "block";
	obj.innerHTML = _resp;
	obj.commentsHide = new toggleShowHide('comment'+_index, true);
	obj.commentsHide.doToggle();
	// make form elements work properly
	new imageSwap("commentButton"+_index, WEBROOT+"images/submit-2.gif");
	new autoBlur('commentFrm'+_index, 'name');
	new autoBlur('commentFrm'+_index, 'message');
}

function addAjaxComment(_frmId, _index) {
	var frm = document.getElementById(_frmId);
	if(frm.message.value && frm.message.value != "message" && frm.name.value != "name") {
		var stg = "offer_id="+frm.offer_id.value+"&name="+frm.name.value+"&message="+frm.message.value+"&subject="+frm.subject.value;
		if(frm.type.value) stg += "&type="+frm.type.value;
		var req = ajaxCall(frm.postPage.value,stg);
		req.callback = function(_resp) { onAddAjaxComment(_resp, _index); };
	}else alert('You need to enter a message before submitting the form');
}

function onAddAjaxComment(_resp, _index) {
	alert(_resp);
	var obj = document.getElementById('comment'+_index);
	obj.commentsHide.doToggle(); // close comments for this coupon
}

// COUPON FILTERING
function couponFilter(_pageNum, _typeId, _page, _fullSlug, _catId) {
	var t = this;
	var __pageNum = _pageNum;
	var __typeId = _typeId;
	var __page = _page;
	var __fullSlug = _fullSlug;
	var __catId = _catId;

	t.doFilter = function(_hours) {
		var stg = "hours="+_hours+"&pageNum="+__pageNum+"&typeId="+__typeId+"&fullSlug="+__fullSlug;
		if(__catId) stg += "&categoryId="+__catId;
		var req = ajaxCall(_page,stg);
		req.callback = function(_resp) { t.onResponse(_resp); };
	}
	
	t.onResponse = function(_resp) {
		document.getElementById('couponList').innerHTML = _resp;
	}
}

function couponVote(_offerId, _index) {
	// make ajax call
	var stg = "offer_id="+_offerId;
	var req = ajaxCall('ajax.vote.php',stg);
	// adjust visuals
	var vc = document.getElementById('voteCount'+_offerId+_index);
	var vb = document.getElementById('voteButton'+_offerId+_index);
	vc.innerHTML = parseFloat(vc.innerHTML) + 1;
	vb.innerHTML = "<img src=\""+WEBROOT+"images/vote3.gif\" />";
}

function closeCouponCancel() {
	var oh = document.getElementById('overlayHolder');
	oh.innerHTML = "";
}

function showCouponCancel(_offerId, _index) {
	var stg = "offer_id="+_offerId+"&index="+_index;
	var req = ajaxCall('ajax.overlayVoteCancel.php',stg);
	var oh = document.getElementById('overlayHolder');
	req.callback = function(_resp) { 
		oh.innerHTML = _resp; 
		new imageSwap("overlayFrm", WEBROOT+"images/send2.gif");
		new autoBlur('otherForm');
	}
}

function couponCancelInput(_offerId, _index, _inputId) {
	var inp = document.getElementById(_inputId);
	if(inp.value && inp.value != "Other") couponCancelSelect(_offerId, _index, inp.value);
	else alert("Please enter a reason for your disapproval of this coupon.");
}

function couponCancelSelect(_offerId, _index, _msg) {
	closeCouponCancel();
	// make ajax call
	var stg = "offer_id="+_offerId+"&message="+_msg;
	var req = ajaxCall('ajax.voteCancel.php',stg);
	// adjust visuals
	var vb = document.getElementById('voteButton'+_offerId+_index);
	vb.innerHTML = "";
	var coupon = document.getElementById('coupon'+_offerId+_index);
	coupon.className = "coupon cancelled";
}

function addShareCoupon(_frmId) {
	var frm = document.getElementById(_frmId);
	// do error checking
	var checkReq = new checkRequired(frm, 'merchant,description', 'Store Name,Discount');
	if(!checkReq.ok()) {
		alert('Error in the following fields:\n'+checkReq.message().replace(/<br>/g,""));
		return;
	}
	// make ajax call
	var stg = "name="+frm.name.value+"&merchant="+frm.merchant.value+"&description="+frm.description.value+"&end_year="+frm.end_year.value+"&end_month="+frm.end_month.value+"&end_day="+frm.end_day.value;
	if(frm.code.value) stg += "&code="+frm.code.value;
	if(frm.subject.value) stg += "&subject="+frm.subject.value;
	if(frm.link_bypass.value && frm.link_bypass.value != "http://") stg += "&link_bypass="+frm.link_bypass.value;
	if(frm.type_id.value) stg += "&type_id="+frm.type_id.value;
	var req = ajaxCall('ajax.shareCoupon.php',stg);
	req.callback = function (_resp) { alert(_resp); }
	// empty fields
	frm.name.value = "";
	frm.merchant.value = "";
	frm.description.value = "";
	frm.code.value = "";
	frm.end_year.value = "yyyy";
	frm.end_month.value = "mm";
	frm.end_day.value = "dd";
	
	return false;
}