
/**
 * @author aamaratunga
 */

function getTimeFromServer() {
  new Ajax.Request('/extra/synch:beat', { 
    requestHeaders: { Accept: 'application/json' },
    method:'post', 
    onSuccess: function(transport) {
      var json = transport.responseText.evalJSON(true);
      clock.seconds = json.secondsRemaining; 
    }
  });
}

function getSiteTimeFromServer() {
  new Ajax.Request('/extra/synch:beat', { 
    requestHeaders: { Accept: 'application/json' },
    method:'post', 
    onSuccess: function(transport) {
      var json = transport.responseText.evalJSON(true);
      siteClock.seconds = json.secondsRemaining; 
    }
  });
}

function wantToReconnect() {
  new Ajax.Request('/extra/connectionerror:reconnect', { 
    requestHeaders: { Accept: 'application/json' },
    method:'post'
  });
}

function checkReconnect() {
  new Ajax.Request('/extra/connectionerror:check', { 
    requestHeaders: { Accept: 'application/json' },
    method:'post', 
    onSuccess: function(transport) {
      var json = transport.responseText.evalJSON(true);
      if (json.reconnect == '1') {
       if(typeof(reconnectPe) != 'undefined') { reconnectPe.stop(); }
       updateBodyWithSessionState();
      } 
    }
  });
}

function reconnect(pe) { 
  ++reconnectCallCount;
  if ($('pageHeaderImage').alt != 'Connection Error') {
    log('pe chg hd stop '+reconnectCallCount); 
    pe.stop();
  }
  else if (reconnectCallCount == 7) { 
    log('pe count 7 stop '+reconnectCallCount); 
    pe.stop(); 
    stillConnectionError();
  }
  else if (clock.isNotCloseToRatePeriodMarker(20)) { 
    log('pe reconnect '+reconnectCallCount); 
    checkReconnect(); 
  } 
  else {
    log('pe after 20 stop '+reconnectCallCount); 
    pe.stop(); 
    stillConnectionError(); 
  } 
} 


function nicknameAvailable(nickname){
  new Ajax.Request('/extra/synch:nicknameavailable/'+nickname, { 
    requestHeaders: { Accept: 'application/json' },
    method:'post', 
    onSuccess: function(transport) {         
      var json = transport.responseText.evalJSON(true);
      showNicknameAvailability(json.available,nickname);
    }
  });
}

function sendLog(msg) {
  new Ajax.Request('/extra/synch:log/'+msg, { 
    requestHeaders: { Accept: 'application/json' },
    method:'post'
  });
}

function stillConnectionError() {
  updateBody('/extra/connectionerror:stillconnectionerror');	
}

function connectionStatusError(type) {
	updateBody('/extra/skypesignouterror:error/'+type);
}

function checkout() {
  new Ajax.Request('/extra/datingcomplete:checkout', {    
    method:'post'
  });	
}

function continueSpeedDating() {
  updateMainBody('/extra/datingcomplete:continuespeeddating');
}

function invokeSaveDate() {
  new Ajax.Request('/extra/speeddate:savedate', {    
    method:'post'
  });	
}

function invokeSaveNoMatchDate() {
  new Ajax.Request('/extra/nomatch:savedate', {    
    method:'post'
  }); 
}

function updateBodyWithConnectionError() {
  updateBody('/extra/connectionerror');	
}

function updateBodyWithSessionState() {
  updateBody('/extra/synch:state');
}

function getBlockDateDialog(dateId,pos) {
	updateDiv('/deletedate/'+dateId+'/'+pos,'block_'+pos);	
}

function submitForm(_url, _formStr, _div) {
  _url = _url+'.form?'+_formStr;	
  updateDiv(_url, _div);
}


function updateBody(_url) {
  updateDiv(_url, 'body');
}

function updateMainBody(_url) {
  updateDiv(_url, 'mainbody');
}

function updateDiv(_url,_div) {
  new Ajax.Updater(_div,   _url, { method: 'post', evalScripts: true });
}

function isDoneReconnecting() {
	if (reconnectCallCount == 3)
		return true;
	return false;	
}

function checkCallStatus(ratePeriodMarker) {
	if (skypeHandler.isCallConnected())
	  clock.invokeWhen(ratePeriodMarker, function() { updateBodyWithSessionState() }); 
	else
	  updateBodyWithConnectionError();
}

function resendActivationMail(code) {
  new Ajax.Request('/extra/synch:resend/'+code, { 
    requestHeaders: { Accept: 'application/json' },
    method:'post',
    onSuccess: function(transport) {         
      var json = transport.responseText.evalJSON(true);
      if (json.sent == 1)
			  $('resend_sent').show();
    }		
  });
}

function refreshStats(url) {
  new Ajax.Request(url, { 
    requestHeaders: { Accept: 'application/json' },
    method:'post',
    onSuccess: function(transport) {
      var json = transport.responseText.evalJSON(true);
      updateStats(json);
    }   
  });
}