/**
 * Copyright (c) www.mkMob.com. All Rights Reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * mkMob Client Core Class.
 *
 * @author  Akeno Barrett
 * @version $Id mkmob.js v1.0, 20-Mar-2010, 3:59 PM $
 * @package mkMob
 */
 
/**
 * Construct the object
 */
var MKMOB = new Object();

/**
 * Displays a dialog that fetches its content from the server
 */
MKMOB.showDialog = function(title,URI)
{
   Dialog.setTitle(title);
   Dialog.setFooter();
   Dialog.setURI(URI);
   Dialog.show();
}

/**
 * Displays a dialog that has its content set on the client
 */
MKMOB.showLocalDialog = function(title, content)
{
   Dialog.setTitle(title);
   Dialog.setFooter();
   Dialog.showLocal(content);
}

/**
 * Sets a random captcha image
 */
MKMOB.resetCaptcha = function() {
   var src = 'captcha.php?'+(new Date().getTime());
   document.getElementById('captcha').src=src;
}

/**
 * Sends a confirmation link to reset a user's password
 */
MKMOB.sendPasswordConfirmation = function() {
   var email = document.getElementById('reset-email').value;
   var captcha = document.getElementById('reset-captcha').value;
   if(document.getElementById('dialog-success-box').style.display == 'block') document.getElementById('dialog-success-box').style.display = 'none'; 
   if(document.getElementById('dialog-error-box').style.display == 'block') document.getElementById('dialog-error-box').style.display = 'none';
   this.showDialogLoading();
   $.post('/ajax/reset_password.php', {email:email,captcha:captcha}, function(data) {
      if(data.length > 0) {
         if(data == '1') {
            MKMOB.hideDialogLoading();
            Dialog.displaySuccess('An email has been sent to '+email+' with a confirmation link. Follow the instructions in the email to reset your password.');
            document.getElementById('reset-email').value = '';
            document.getElementById('reset-captcha').value = '';
            MKMOB.resetCaptcha();
         } else {
            MKMOB.hideDialogLoading();
            Dialog.displayError(data);
         }
      } else {
         MKMOB.hideDialogLoading();
         Dialog.displayError('The server did not return a response.');
      }
   });  
}

/**
 * Displays loading image in the dialog title
 */
MKMOB.showDialogLoading = function() {
   document.getElementById('dialog-submit-button').disabled=true;
   document.getElementById('dialog-loading').style.visibility = 'visible';
}

/**
 * Hides loading image in the dialog title
 */
MKMOB.hideDialogLoading = function() {
   document.getElementById('dialog-loading').style.visibility = 'hidden';
   document.getElementById('dialog-submit-button').disabled=false;
}

MKMOB.addNewUser = function() {
   var email = document.getElementById('registration-email').value;
   var password = document.getElementById('registration-password').value;
   var confirmed_password = document.getElementById('registration-password_2').value;
   var fname = document.getElementById('registration-firstname').value;
   var lname = document.getElementById('registration-lastname').value;
   var phone = document.getElementById('registration-phone').value;
   var company = document.getElementById('registration-company').value;
   var adBudget = document.getElementById('registration-adbudget').value;
   var pageViews = document.getElementById('registration-page-views').value;
   var referer = document.getElementById('registration-referer').value;
   var accntType = document.getElementById('registration-account-type').value;
   var captcha = document.getElementById('reset-captcha').value;
   if(document.getElementById('dialog-success-box').style.display == 'block') document.getElementById('dialog-success-box').style.display = 'none'; 
   if(document.getElementById('dialog-error-box').style.display == 'block') document.getElementById('dialog-error-box').style.display = 'none';
   this.showDialogLoading();
   $.post('/ajax/registration.php',{email:email,password:password,confirmed_password:confirmed_password,firstname:fname,lastname:lname,phone:phone,company:company,budget:adBudget,page_views:pageViews,referer:referer,account_type:accntType,captcha:captcha}, function(data) {
      if(data.length>0) {
         if(data == '1') {
            MKMOB.hideDialogLoading();
            document.forms['dialog-register'].reset();
            //MKMOB.showLocalDialog('Create an Account', '<div id="dialog-success-box" class="success-box" style="display:none;font-size:11px;"></div>');
	      Dialog.displaySuccess('<img src="/imgs/complete.png" alt="*" />&nbsp;&nbsp;Your account was created. Please check your messages, we\'ve sent you an email with a confirmation link to confirm your email address. If you do not see an email contact us at support@mkmob.com');
            MKMOB.resetCaptcha();
         } else {
            MKMOB.hideDialogLoading();
            Dialog.displayError(data);
         }
      } else {
         MKMOB.hideDialogLoading();
         Dialog.displayError('The server did not return a response.');
      }
   });
}

MKMOB.sendFeedback = function() {
   var area = document.getElementById('dialog-feedback-subject').value;
   var feedback = document.getElementById('dialog-feedback-box').value;
   if(document.getElementById('dialog-success-box').style.display == 'block') document.getElementById('dialog-success-box').style.display = 'none'; 
   if(document.getElementById('dialog-error-box').style.display == 'block') document.getElementById('dialog-error-box').style.display = 'none';
   this.showDialogLoading();
   $.post('/ajax/send_feedback.php', {area:area,feedback:feedback}, function(d) {
      if(d.length>0) {
         if(d == '1') {
            MKMOB.hideDialogLoading();
            document.getElementById('dialog-feedback-box').value = '';
            Dialog.displaySuccess('<img src="/imgs/complete.png" alt="*" />&nbsp;&nbsp;Thank you for taking the time to send this feedback. We appreciate it.');
         } else {
            MKMOB.hideDialogLoading();
            Dialog.displayError(d);
         }   
      } else {
         MKMOB.hideDialogLoading();
         Dialog.displayError('The server did not return a response.');
      }   
   });
}

MKMOB.displayAffCode = function(affId) {
   var HTML = "<div id='aff_code' name='aff_code' style='display:block;'><p class='para'>If you want to refer users to us as your affiliate, you must put this link on your site. "+
                     "Ensure that users see the link. The more users you refer, the more revenue you will earn!</p>"+
                     "<p class='para'></p><p class='para'>Your unique referral URL is:<br /><input onclick='this.select();' style='width:400px;' type='text' value='http://www.mkmob.com/?aff="+affId+"' /></p>"+
                     "<p class='para'><table cellspacing='0' cellpadding='0' width='100%'><tr><td>HTML Code:</td></tr>"+
                     "<tr><td><textarea onclick='this.select();' style='width:400px;height:80px;'>&lt;a href=\"http://www.mkmob.com/?aff="+affId+"\"&gt;Serve mobile ads, earn $$$ on your wapsite&lt;/a&gt;</textarea></td></tr></table>"+
                     "</p><p class='para'></p><p class='para'>Good luck!!</p></div>";
   this.showLocalDialog('Affiliate Link Code', HTML);
}

MKMOB.saveAffPageText = function() {
   var text = document.getElementById('dialog-aff-text').value;
   if(document.getElementById('dialog-success-box').style.display == 'block') document.getElementById('dialog-success-box').style.display = 'none'; 
   if(document.getElementById('dialog-error-box').style.display == 'block') document.getElementById('dialog-error-box').style.display = 'none';
   this.showDialogLoading();
   $.post('/ajax/save_affiliate_page_text.php', {text:text}, function(d) {
      if(d.length>0) {
         if(d == '1') {
            MKMOB.hideDialogLoading();
            document.forms['affiliate_page_text_form'].reset();
            Dialog.displaySuccess('<img src="/imgs/complete.png" /> Message has been saved.');
         } else {
            MKMOB.hideDialogLoading();
            Dialog.displayError(d);
         }   
      } else {
         MKMOB.hideDialogLoading();
         Dialog.displayError('The server did not return a response.');
      }   
   });
}

MKMOB.requesAffPayment = function() {
   var amount = document.getElementById('aff_payment_request_amount').value;
   if(document.getElementById('dialog-success-box').style.display == 'block') document.getElementById('dialog-success-box').style.display = 'none'; 
   if(document.getElementById('dialog-error-box').style.display == 'block') document.getElementById('dialog-error-box').style.display = 'none';
   this.showDialogLoading();
   $.post('/ajax/request_affiliate_payment.php', {amount:amount}, function(d) {
      if(d.length>0) {
         if(d == '1') {
            MKMOB.hideDialogLoading();
            document.getElementById('aff_payment_request_amount').value = '';
            Dialog.displaySuccess('<img src="/imgs/complete.png" /> Your payment request has been made.');
         } else {
            MKMOB.hideDialogLoading();
            Dialog.displayError(d);
         }   
      } else {
         MKMOB.hideDialogLoading();
         Dialog.displayError('The server did not return a response.');
      }   
   });
}

MKMOB.setSiteSubCat = function(selCat) {
   if(selCat != '') {
      //IE fix
      Array.prototype.indexOf = function(item) {
          for(var i = 0; i < this.length; i++) {
              if(item == this[i]) {
                  return i;
              }    
          }
          return -1;
      }
      var arrCategories = new Array('Entertainment','Downloads','Gambling','Dating','Communities','Portals','Education','Products','Information','Other');
      var iIndex = arrCategories.indexOf(selCat);
      if(iIndex != -1) {
         arrCategories.splice(iIndex, 1);
         var subCat = null;
         for(var i = 0; i < arrCategories.length; i++) {
            subCat += '<option value="'+arrCategories[i]+'">'+arrCategories[i]+'</option>';
         }
         $('#sub_cat_select_container').html('<select multiple="allow multiple selections" size="4" id="sub_cat_select" style="width:250px;">'+subCat+'</select>');
         if($('#sub_cat').css('display') == 'none') $('#sub_cat').slideDown();
      }
   }
}
