//Makes login request to forum. Parameters:
//username - forum user name
//UUID - unique user ID to validate login status
function vcapi_shared_login(UUID) {
 var options={module: 'votecontrol', action: 'sharedLogin', UUID: UUID};
 var func=function(req) {
  if (req.responseJS.errorCode==0) {
   if (req.responseJS.data.loggedIn==1) {
    mvote_user=req.responseJS.data.userName;
    mvote_is_login=true;
    //Store session id to cookie
    mvote_collector_full_url=mvote_collector_url+';jsessionid='+req.responseJS.data.sessionId;
    d=new Date();
    d.setTime(d.getTime()+mvote_cookie_time);
    document.cookie='marketocracy_sid='+req.responseJS.data.sessionId+';expires='+d.toGMTString()+'; path=/';
    //Update captcha to new session id
    update_captcha();
    after_login(null);
   }
  }
 };
 ajax_request(null,options,func);
}

//Makes login request to forum. Parameters:
//user - forum user name
//password - user password
//callback_func - callback function to receive call resuls. It uses 7 parameters: error_code, error_message, login, email, firstname, lastname, optin.
//error_code==0 - success login, error_code>0 - error (message in error_message), error_code<0 - incorrect login or password
//login - user login
//password - user password
//email - user e-mail
//firstname - user first name
//lastname - user last name
//optin - opt-in flag: 1 - user is subscribed, 0 - user isn't subscribed
function vcapi_login(user, password, callback_func) {
 var options={module: 'votecontrol', action: 'login', username: user, password: password};
 var func=function(req) {
  if (req.responseJS.errorCode==0) {
   mvote_user=req.login_name;
   mvote_is_login=true;
//   after_login(null);
   //Call to partner's code jack
   if (req.responseJS.data.associatedUser!=='') {internal_ipbapi_shared_login(req.responseJS.data.uuid);}
  }
  callback_func(req.responseJS.errorCode, req.responseJS.error, req.login_name, req.responseJS.data.email, req.responseJS.data.firstName, req.responseJS.data.lastName, req.responseJS.data.optin);
 };
 ajax_request(null,options,func);
}

//Makes logout request to forum. Parameter:
//callback_func - callback function to receive call resuls. It uses 2 parameters: error_code, error_message.
//error_code==0 - success, error_code!=0 - error (message in error_message)
function vcapi_logout(callback_func) {
 var options={module: 'votecontrol', action: 'logout'};
 var func=function(req) {
  if (req.responseJS.errorCode==0) {
   user_logout();
//   after_logout(null);
  }
  callback_func(req.responseJS.errorCode, req.responseJS.error);
 };
 ajax_request(null,options,func);
}

//Gets registration captcha image source. Parameter:
//callback_func - callback function to receive call resuls. It uses 2 parameters: is_enabled, captcha_src.
//is_enabled - TRUE if captcha is supported by current registration process
//catcha_src - captcha image path (meaningful if is_enabled==TRUE)
function vcapi_captcha(callback_func) {
 var captcha=new Image();
 captcha.onload=function() {
  callback_func(captcha.width!=1 || captcha.height!=1,captcha.src);
 }
 captcha.src=mvote_captcha;
}

//Makes registration request to forum. Parameters:
//user - forum user name
//firstname - user first name
//lastname - user last name
//password - forum user password
//email - user e-mail
//optin - opt-in flag (true or false), if your server is not used it set to false
//captcha - registration captcha (if not used then empty string)
//callback_func - callback function to receive call resuls. It uses 4 parameters: error_code, error_message, message, is_loggedin.
//error_code==0 - success registration, error_code==-4 - registration is prohibited, any other value of error_code means error
//error_message - contains message in case of error
//message - the message in case of success registration
//is_loggedin - TRUE if user is logged in as result of success registration
function vcapi_register(user, firstname, lastname, password, email, optin, captcha, callback_func) {
 var options={module: 'votecontrol', action: 'register', username: user, password: password, email: email, optin: optin?1:0, firstName: firstname, lastName: lastname, confirmation: email_confirmation};
 if (captcha!='') {options.captcha=captcha;}
 var func=function(req) {
  if (req.responseJS.errorCode==0 && req.responseJS.data.loggedIn==1) {
   mvote_user=req.login_name;
   mvote_is_login=true;
 //  after_login(null);
   //Call to partner's code jack
   if (req.responseJS.data.associatedUser!=='') {internal_ipbapi_shared_login(req.responseJS.data.uuid);}
  }
  //Updates captcha source
//  update_captcha();
  callback_func(req.responseJS.errorCode,req.responseJS.error,req.responseJS.data.message,req.responseJS.data.loggedIn==1);
 };
 ajax_request(null,options,func);
}

//Setups captcha on change listener callback function. Captcha image is changed at each registration request (successful or failed).
//Thus if your registration page contains vote controls you need to know that it makes registration requests and changes captcha, so
//you need to specify appropriate callback via this function. If your registration page doesn't contain vote controls you can develop
//you code without usage of this function, but you should manually update captcha source (by calling vcapi_captcha function) after
//each registration request. Parameter:
//callback_func - listener callback function. It uses 2 parameters: is_enabled, captcha_src.
//is_enabled - TRUE if captcha is supported by current registration process
//catcha_src - captcha image path (meaningful if is_enabled==TRUE)
function vcapi_set_captcha_listener(callback_func) {
 mvote_captcha_listener=function() {
  var captcha=new Image();
  captcha.onload=function() {
   callback_func(captcha.width!=1 || captcha.height!=1,captcha.src);
  }
  captcha.src=mvote_captcha;
 }
}
