var Content      = new Object();
var Enquiry      = new Object();
var Competition  = new Object();
var EventListing = new Object();

//=====================================================//
function inputValueSearch(self, initialValue, isFocus){
   if(isFocus == true)
   {
      if(self.value == initialValue){
         self.value = '';
      }

   } else {
      if(self.value.replace(/^\s+|\s+$/g,"") == ''){
         self.value = initialValue;
      }
   }
}

//=====================================================//
Content.getContentDetail = function(present_date){

   var divObj = document.getElementById("contentDetail");
   var imgSrc = '<div align="center"><img src="images/loader.gif" border="0" align="center"></div>';
   divObj.innerHTML = imgSrc;

   var url = "index.php?_spAction=contentDetail" + "&present_date=" + present_date +  "&showHTML=0" ;

   var formContent = 'xyz=1';


   XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Content.getContentDetailHandler();});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);

}

//=====================================================//
Content.getContentDetailHandler = function(){

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {
      var responseText = XMLHTTP.xmlHttpObj.responseText;

      var divObj = document.getElementById("contentDetail");
      divObj.innerHTML = responseText;

   }
}

//=====================================================//
Comments.submitComment = function(){

   validate = Validate.validateCommentForm("frmComment");

   if (validate == false) return;

   var commentFormDIV  = document.getElementById("commentBox");

   var commentFormObj  = document.getElementById("frmComment");
   var section_id      = commentFormObj.section_id.value;
   var record_id       = commentFormObj.record_id.value;

   var url = "/index.php?_spAction=submitComment" + "&_room=comment" + "&showHTML=0";

   var formContent = UtilAjax.getFormValues(commentFormObj);

   XMLHTTP.xmlHttpObj  = XMLHTTP.getXMLHTTPObject(function () {Comments.submitCommentHandler(section_id, record_id);});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//=====================================================//
Comments.submitCommentHandler = function(section_id, record_id){
   var commentFormDIV = document.getElementById("commentBox");

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {
      var responseText = UtilString.trimAll(XMLHTTP.xmlHttpObj.responseText, 1);
      if (responseText == "invalidCapcha"){
         alert('please enter the code correctly');

         var url = "/index.php?_spAction=reGenerateCaptha" + "&showHTML=0";

         var formContent = 'xyz=1';
         XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Comments.submitCommentHandler2();});
         XMLHTTP.xmlHttpObj.open("POST", url, true);
         XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
         XMLHTTP.xmlHttpObj.send(formContent);

      } else if (responseText == "success"){
         // commentFormDIV.innerHTML = "Thank you for your comment. It has been submitted for review and will appear here once it has been approved.";
         commentFormDIV.innerHTML = "Thank you for your comment.";
      }
   }
}

//=====================================================//
Comments.submitCommentHandler2 = function(){
   var capthaObj = document.getElementById("captchaImg");

   capthaObj.innerHTML = "";

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var dateObj = new Date();
      var responseText = UtilString.trimAll(XMLHTTP.xmlHttpObj.responseText, 1) + "?x=" + dateObj.getTime();
      capthaObj.innerHTML = "<img src=" + responseText + ">";
   }
}

//=====================================================//
Enquiry.submitEnquiry = function(){

   validate = Validate.validateEnquiryForm("frmEnquiry");

   if (validate == false) return;

   var enquiryFormDIV  = document.getElementById("enquiryBox");

   var enquiryFormObj  = document.getElementById("frmEnquiry");


   var url = "/index.php?_spAction=submitEnquiry" + "&_room=content" + "&showHTML=0";

   var formContent = UtilAjax.getFormValues(enquiryFormObj);

   XMLHTTP.xmlHttpObj  = XMLHTTP.getXMLHTTPObject(function () {Enquiry.submitEnquiryHandler();});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//=====================================================//
Enquiry.submitEnquiryHandler = function(){
   var enquiryFormDIV = document.getElementById("enquiryBox");

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {
      var responseText = UtilString.trimAll(XMLHTTP.xmlHttpObj.responseText, 1);
      if (responseText == "invalidCapcha"){
         alert('please enter the code correctly');

         var url = "/index.php?_spAction=reGenerateCaptha" + "&showHTML=0";

         var formContent = 'xyz=1';
         XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Enquiry.submitEnquiryHandler2();});
         XMLHTTP.xmlHttpObj.open("POST", url, true);
         XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
         XMLHTTP.xmlHttpObj.send(formContent);

      } else if (responseText == "success"){
         enquiryFormDIV.innerHTML = "Thank you for your message. We will endeavour to address your correspondence soon.";
      }
   }
}

//=====================================================//
Enquiry.submitEnquiryHandler2 = function(){
   var capthaObj = document.getElementById("captchaImg");

   capthaObj.innerHTML = "";

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var dateObj = new Date();
      var responseText = UtilString.trimAll(XMLHTTP.xmlHttpObj.responseText, 1) + "?x=" + dateObj.getTime();
      capthaObj.innerHTML = "<img src=" + responseText + ">";
   }
}

//=====================================================//
EventListing.submitEventListing = function(){

   validate = Validate.validateSubmitAnEventForm("frmSubmitEvent");
   if (validate == false) return;

   var formDIV  = document.getElementById("submitEventListingBox");
   var formObj  = document.getElementById("frmSubmitEvent");

   var url = "/index.php?_spAction=submitAnEvent" + "&_room=content" + "&showHTML=0";
   var formContent = UtilAjax.getFormValues(formObj);

   XMLHTTP.xmlHttpObj  = XMLHTTP.getXMLHTTPObject(function () {EventListing.submitEventHandler();});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//=====================================================//
EventListing.submitEventHandler = function(){
   var formDIV = document.getElementById("submitEventListingBox");

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {
      var responseText = UtilString.trimAll(XMLHTTP.xmlHttpObj.responseText, 1);
      if (responseText == "invalidCapcha"){
         alert('please enter the code correctly');

         var url = "/index.php?_spAction=reGenerateCaptha" + "&showHTML=0";

         var formContent = 'xyz=1';
         XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {EventListing.submitEventHandler2();});
         XMLHTTP.xmlHttpObj.open("POST", url, true);
         XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
         XMLHTTP.xmlHttpObj.send(formContent);

      } else if (responseText == "success"){
         formDIV.innerHTML = "Thank you for your message. We will endeavour to address your correspondence soon.";
      }
   }
}

//=====================================================//
EventListing.submitEventHandler2 = function(){
   var capthaObj = document.getElementById("captchaImg");

   capthaObj.innerHTML = "";

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var dateObj = new Date();
      var responseText = UtilString.trimAll(XMLHTTP.xmlHttpObj.responseText, 1) + "?x=" + dateObj.getTime();
      capthaObj.innerHTML = "<img src=" + responseText + ">";
   }
}
//=====================================================//
Competition.submitCompetition = function(){

   validate = Validate.validateCompetitionForm("frmCompetition");

   if (validate == false) return;

   var competitionFormDIV  = document.getElementById("competitionBox");

   var competitionFormObj  = document.getElementById("frmCompetition");
   var record_id       = competitionFormObj.record_id.value;

   var url = "/index.php?_spAction=submitCompetition" + "&_room=content" + "&showHTML=0";

   var formContent = UtilAjax.getFormValues(competitionFormObj);

   XMLHTTP.xmlHttpObj  = XMLHTTP.getXMLHTTPObject(function () {Competition.submitCompetitionHandler(record_id);});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//=====================================================//
Competition.submitCompetitionHandler = function(record_id){
   var competitionFormDIV = document.getElementById("competitionBox");

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {
      var responseText = UtilString.trimAll(XMLHTTP.xmlHttpObj.responseText, 1);
      if (responseText == "invalidCapcha"){
         alert('please enter the code correctly');

         var url = "/index.php?_spAction=reGenerateCaptha" + "&showHTML=0";

         var formContent = 'xyz=1';
         XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Competition.submitCompetitionHandler2();});
         XMLHTTP.xmlHttpObj.open("POST", url, true);
         XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
         XMLHTTP.xmlHttpObj.send(formContent);

      } else if (responseText == "success"){
         competitionFormDIV.innerHTML = "Thank you for entering this competition. We will notify competition winners by email.";
      }
   }
}

//=====================================================//
Competition.submitCompetitionHandler2 = function(){
   var capthaObj = document.getElementById("captchaImg");

   capthaObj.innerHTML = "";

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var dateObj = new Date();
      var responseText = UtilString.trimAll(XMLHTTP.xmlHttpObj.responseText, 1) + "?x=" + dateObj.getTime();
      capthaObj.innerHTML = "<img src=" + responseText + ">";
   }
}
//=====================================================//

//=====================================================//
Poll.submitPollAnswer = function(pollID, pollHistoryID){

   var pollResultObj = document.getElementById("pollResult");
   pollResultObj.innerHTML = "Processing... Please wait";

   var url = "/index.php?_spAction=submitPollAnswer" +
             "&poll_id=" + pollID + "&_room=poll" + "&poll_history_id="+ pollHistoryID + "&showHTML=0" ;

   var formContent = 'xyz=1';

   XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Poll.submitPollAnswerHandler();});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}


//*********************************************************//
$(function(){

   $('#productItems input').click(function(){ 
      $('#productItems div').removeClass('selectedProduct'); 
      $(this).parent().addClass('selectedProduct');
      
      if ( !$(this).attr('hasCoupon')){
          $('#couponMessage').html('');
      }
      
   });

   $('input[hasCoupon=1]').live('click', function(){
      couponProductID = $(this).val();
      $.nyroModalManual({
        url: '/index.php?_room=member&_spAction=enterCouponNo&showHTML=0',
        modal: true,
        minWidth: 200,
        minHeight: 90,
        width:200,
        height:90,
        resizeable: true
      })
      
   });

   $('a#couponConfirm').live('click', function(e){
       e.preventDefault();
       var coupon_no  = $('#coupon_no').val();
       
       var url = '/index.php?_room=member&_spAction=enterCouponNoSubmit&showHTML=0';
       $.get(url, {coupon_no:coupon_no, product_id:couponProductID}, function (data) {
         hasValue = data.hasValue;
         
         if (hasValue == 0){
            $('#couponError').html('invalid coupon no.');
         } else {
            $('#coupon_no_h').val(coupon_no);
            $.nyroModalRemove();
            $('#couponMessage').html('Coupon Used: <b>' + coupon_no + '</b>');
            $('#couponMessage').css({'color':'green', 'font-weight':'bold'});
         }
         
       }, 'json');
   });

   $('a#couponCancel').live('click', function(e){
      e.preventDefault();
      $.nyroModalRemove();
      $('input:radio[name=product_id]:first').attr('checked', 'checked');
      $('#productItems div').removeClass('selectedProduct');
      $('input:radio[name=product_id]:first').parent().addClass('selectedProduct');
   });

});