// 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 sendFeedback(_webroot,_formId,_btn,_txt) {
	var frm = document.getElementById(_formId);
	var btn = document.getElementById(_btn);
	var infoDiv = document.getElementById(_txt);
	btn.style.visibility = "hidden";
	
	var req = new ajaxObject(_webroot+'ajax/ajax.sidebarForm.php');
	req.callback = function(_resp) { sidebarResponse(_resp, frm, btn, infoDiv, feedbackHide); }
	req.update('subject='+frm.subject.value+'&template='+frm.template.value+'&recipient='+frm.recipient.value+'&name='+frm.name.value+'&email='+frm.email.value+'&message='+frm.message.value, 'POST');
}

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

function updateStoreRank(_storeId,_rank) {
	var req = new ajaxObject('../ajax/ajax.merchantRanking.php');
	//req.callback = function(_resp) { alert(_resp); }
	//alert('storeId='+_storeId+'&rank='+_rank);
	req.update('storeId='+_storeId+'&rank='+_rank, 'POST');
}

function updateAffiliateSelect(_storeId, _selectName, _selectedLink) {
	var req = new ajaxObject('../ajax/ajax.affiliateLink.php');
	req.callback = function(_resp) { onAffiliateSelectResponse(_resp); }
	var stg = 'storeId='+_storeId+'&selectName='+_selectName;
	if(_selectedLink) stg += '&selectedLink='+_selectedLink;
	req.update(stg, '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 updateFSDBannerSelect(_storeId, _selectName, _selectedLink) {
	var req = new ajaxObject('../ajax/ajax.bannerSelect.php');
	req.callback = function(_resp) { onFSDAffiliateSelectResponse(_resp); }
	req.update('storeId='+_storeId+'&selectName='+_selectName+'&selectedLink='+_selectedLink, 'POST');
}

function updateCommentType(_type, _selectName, _selectedId, _useNone) {
	var req = new ajaxObject('../ajax/ajax.commentTypeSelect.php');
	req.callback = function(_resp) { onCategorySelectResponse(_resp); }
	req.update('type='+_type+'&selectName='+_selectName+'&selectedId='+_selectedId+'&useNone='+_useNone, 'POST');
}

function sidebarResponse(_resp, _frm, _btn, _txt, _hideObj) {
	var respAry = _resp.split("|");

	// display message
	_txt.innerHTML = respAry[1];
	var fdInfo = new fade(_txt);
	if(_btn) var fdBtn = new fade(_btn);
	
	if(respAry[0] == "success") {
		// remove message from form
		fdInfo.onComplete = function() {
			if(_frm.message) _frm.message.value = "";
			if(_frm.fName) _frm.fName.value = "";
			if(_frm.fEmail) _frm.fEmail.value = "";
			if(_hideObj) _hideObj.doToggle();
			_txt.innerHTML = "";
			//fdInfo.stop();
			//fdInfo.fadeOut(1);
			if(_btn) fdBtn.fadeIn(1.5);
			if(_btn) _btn.style.visibility = "visible";
			//fdInfo.onComplete = null;
		}
	}else{
		// remove message from form
		fdInfo.onComplete = function() {
			fdInfo.stop();
			fdInfo.fadeOut(1);
			if(_btn) fdBtn.fadeIn(1.5);
			if(_btn) _btn.style.visibility = "visible";
			fdInfo.onComplete = null;
		}		
	}
	fdInfo.fadeIn(3);
	_txt.style.visibility = "visible";
}