// ** ********************************************************************************* ** //
// **																					                                          ** //
// ** calendar.js                                                                       ** //
// ** Created by: Tibor(tibor@planetsg.com) 							                              ** //
// **																					                                          ** //
// ** Last Change by: Tibor(tibor@planetsg.com) 							                          ** //
// ** Last Change Date: March, 30. 2009. 									    			                    ** //
// **																					                                          ** //
// ** ********************************************************************************* ** //


var calResponder = {
         onCreate: function() { 
         
         $('ajaxloadinfo').style.display = 'block';
         Ajax.activeRequestCount++;

        },
    
        onComplete: function() {
 
         $('ajaxloadinfo').style.display = 'none';
          Ajax.activeRequestCount--;
        }
    }

 Ajax.Responders.register(calResponder);
 
 
var otherLocationText = '';

var InitCalendar = function() {

    if($('calparam') && PartyTypeDD()) {
    //if($('calparam')) {
    DrawCalendar();
    }
}

var DrawCalendar = function() {

    var zpk = $('regKey').value;
    var param = $('calparam').value;    
    //alert(param);
    param = param.split(",");
    
    var party = $('partyparam').value;    
    party = party.split(",");
    
 
      var url="Action/Get_Party_Date.php";
      var pars = 'job=1&d=' + param[0] + '&m=' + param[1] + '&y=' + param[2] + '&bkey=' + party[0] + '&zpk=' + zpk + '&sid=' + Math.random();

  		var prtAjax = new Ajax.Request(url, {
  		parameters: pars,
      onCreate: ShowProcess('caldate'),
      onSuccess: function(response) {

                  if(response.responseText.length > 0) {
                  $('caldate').innerHTML = response.responseText;
                  
                  var allNodes = $$("#caldate .calcellsel");
                  
                  if(allNodes != null && allNodes != '' && allNodes != 'undefined' && party[1].length == 0) {
                  for(i = 0; i < allNodes.length; i++) {
                     allNodes[i].setAttribute("class" , "calcell calcellfree");
                  }
                  }
                  
                                    
                  if(party[0].length > 0 && param[0] != '0') { 
                  InitTime();
                  }
                  
                  if(otherLocationText.length == 0)  otherLocationText = $('otherlocinfo').innerHTML;

                  } else {
                  $('caldate').innerHTML = '';
                  }
                                
            } ,
      onFailure: function(){
                  StopProcess('caldate');
                  }     
      
        }
      ); 

}


var ChangeMonth = function(d,m,y,bid) {
// alert("init");
    if($('calparam')) {

      var zpk = $('regKey').value;
    
      var dateid = '99';
      var url="Action/Get_Party_Date.php";
      if(d  == '') d = '0';

      var pars = 'job=1&d='+ d + '&m=' + m + '&y=' + y + '&bkey=' + bid + '&zpk=' + zpk + '&sid=' + Math.random();

  		var prtAjax = new Ajax.Request(url, {
  		parameters: pars,
      onCreate: ShowProcess('caldate'),
      onSuccess: function(response) {

                  if(response.responseText.length > 0) {
                  $('caldate').innerHTML = response.responseText;
                  $('caltime').innerHTML = '';
                  } else {
                  $('caldate').innerHTML = '';
                  }
                  
                  $('calparam').value = d+','+m+','+y;
                  
                  AddOLtext();
                                
            } ,
      onFailure: function(){
                  StopProcess('caldate');
                  }     
      
        }
      ); 
      
    }
}

var InitTime = function() {

    var zpk = $('regKey').value;
    var param = $('calparam').value;
   //alert(param);
    param = param.split(",");
    
    var party = $('partyparam').value;    
    party = party.split(",");
    
    if(party[1] == '') party[1] = 'id0';
    
      var url="Action/Get_Party_Date.php";
      var pars = 'job=2&tkey=' + party[1] + '&bkey=' + party[0] + '&d='+ param[0] + '&m=' + param[1] + '&y=' + param[2] + '&zpk=' + zpk + '&sid=' + Math.random();

  		var prtAjax = new Ajax.Request(url, {
  		parameters: pars,
      onCreate: ShowProcess('caltime'),
      onSuccess: function(response) {

                  if(response.responseText.length > 0) {
                  $('caltime').innerHTML = response.responseText;
                  
                  if(param[0] != '' && param[0] != '0') {
                  var cel = 'cc'+param[0];
                  $(cel).setAttribute("class" , "calcell calcellsel"); 
                  }
                  
                  } else {
                  $('caltime').innerHTML = '';
                  }
                                
            } ,
      onFailure: function(){
                  StopProcess('caltime');
                  }     
      
        }
      );           
}

var CheckTime = function(d,m,y,bid) {

      var zpk = $('regKey').value;
      var url="Action/Get_Party_Date.php";
      var pars = 'job=2&tkey=id0&bkey=' + bid + '&d=' + d + '&m=' + m + '&y=' + y + '&zpk=' + zpk + '&sid=' + Math.random();

  		var prtAjax = new Ajax.Request(url, {
  		parameters: pars,
      onCreate: ShowProcess('caltime'),
      onSuccess: function(response) {



                  if(response.responseText.length > 0) {
                  $('caltime').innerHTML = response.responseText;
                  $('calparam').value = d + ',' + m + ',' + y;
                  
                  var allNodes = $$("#caldate .calcellsel");
                  for(i = 0; i < allNodes.length; i++) {
                     allNodes[i].setAttribute("class" , "calcell calcellfree");
                  }
                                    
                  var cel = 'cc'+d;
                  $(cel).setAttribute("class" , "calcell calcellsel"); 

                  } else {
                  $('caltime').innerHTML = '';
                  }
                  
                  AddOLtext();
                               
            } ,
      onFailure: function(){
                  StopProcess('caltime');
                  }     
      
        }
      );           
}

var CheckOtherLocation = function(id) {

    var param = $('calparam').value;

    param = param.split(",");
    
    var party = $('partyparam').value;    
    party = party.split(",");

      var url="Action/Get_Party_Date.php";
      var pars = 'job=3&lid=' + id + '&bkey=' + party[0] + '&d='+ param[0] + '&m=' + param[1] + '&y=' + param[2] + '&sid=' + Math.random();
   //alert(pars);
  		var prtAjax = new Ajax.Request(url, {
  		parameters: pars,
      onCreate: ShowProcess('otherlocinfo'),
      onSuccess: function(response) {

                  if(response.responseText.length > 0) {
                  $('otherlocinfo').innerHTML = response.responseText;
                  } else {
                  $('otherlocinfo').innerHTML = otherLocationText;
                  }
                      
            } ,
      onFailure: function(){
                  StopProcess('otherlocinfo');
                  }     
      
        }
      );           
}

/*
var LoadCalendar = function(cat,lid,mid,bid,d,m,y) {

    //alert('index.php?cat='+cat+'&locID='+lid+'&mid='+mid+'&bid='+bid+'&d='+d+'&m='+m+'&y='+y+'&chd=1&rType=Party')
    window.location.href='index.php?cat='+cat+'&locID='+lid+'&mid='+mid+'&bid='+bid+'&d='+d+'&m='+m+'&y='+y+'&chd=1&rType=Party';
}
*/

var LoadCalendar = function() {

  if($('followlinkparam')) {
  
  var followparam = $('followlinkparam').value;
  
  follow = followparam.split(",");
  var wcat = follow[0];
  var cat = follow[1];
  var lid = follow[2];
  var mid = follow[3];
  var bid = follow[4];
  var d = follow[5];
  var m = follow[6];
  var y = follow[7];

  var tloc = new String(top.location);

    if(tloc.indexOf("cms") != -1) {
    return false;
    } else {
    
    //window.location.href='index.php?cat='+wcat+'&locID='+lid+'&mid='+mid+'&bid='+bid+'&d='+d+'&m='+m+'&y='+y+'&chd=1&rType=Party';
    window.location.href='index.php?cat='+wcat+'&locID='+lid+'&bid='+bid+'&d='+d+'&m='+m+'&y='+y+'&chd=1&rType=Party';
    }
}

}

var ResetParam = function() {

    var theForm = ReturnFormName();
    $('partyparam').value = '';
    $('calparam').value = '';
    theForm.PartyTypeID.options[0].selected = true;
   // ResetCheckbox();
    theForm.submit();
}

var ResetTime = function() {

    var param = $('calparam').value;
    param = param.split(",");
    var newparam = "0," + param[1] + "," + param[2];
    //$('calparam').value = newparam;              
    var theForm = ReturnFormName(); 
    //ResetCheckbox();
    theForm.submit(); 
}

var ResetCheckbox = function() {

  var allNodes = $$('#partyoption input');
    //alert(allNodes.length);
  if(allNodes != null || allNodes != '' || allNodes != 'undefined') {
  for(i = 0; i < allNodes.length; i++) {

    if(allNodes[i].type == 'checkbox') {

         allNodes[i].checked = false;
       }
   }
  }

}

var ReturnFormName = function() {

  return document.forms['SelectParty'];

}


var AddOLtext = function() {
    $('otherlocinfo').innerHTML = otherLocationText;       
}

var PartyTypeDD = function() {

    var theForm = ReturnFormName();

  if(theForm.PartyTypeID.options[theForm.PartyTypeID.selectedIndex].value != '') return true;
  return false;

}

var ShowProcess = function(id) {
  $(id).innerHTML = "<img src='System/Image/spinner.gif' />";
}

var StopProcess = function(id) {
  $(id).innerHTML = "Data not available.";
}


var ValidateParty = function() {

    var theForm = ReturnFormName();
    var cRadio = 0;
      
  if(theForm.PartyTypeID.options[theForm.PartyTypeID.selectedIndex].value == '') {
  alert('Please select Party.');
  return false;
  }

  var param = $('calparam').value;

  var pars = param.split(",");
  if(pars[0] == 0 || pars[0] == '' || pars.length != 3) {
  alert('Please select Date.');
  return false;
  }

  var allNodes = $$('#caltime input');

  if(allNodes == null || allNodes == '' || allNodes == 'undefined') {
  alert('Please select Date and Time.');
  return false;
  }
  
  for(i = 0; i < allNodes.length; i++) {
    if(allNodes[i].type == 'radio' && allNodes[i].checked == true) {
        cRadio++;
        //alert(allNodes[i].value);
        var tPar = allNodes[i].value;
        tPar = tPar.split("|");
        $('timeparam').value = tPar[0];
        $('calendarid').value = tPar[1];
        }
  }
  
  if(cRadio == 0) {
  alert('Please select Time.');
  return false;
  }


  if(theForm.GuestNo.value == '') {
  if(theForm.elements['mainPartyID'].value == '1') {
  alert('The number of participants is missing.');
  }
  if(theForm.elements['mainPartyID'].value == '0') {
  alert('The number of children is missing.');
  }
  return false;
  }


        var cNum = parseInt(theForm.GuestNo.value);
        var cNumMin = parseInt(theForm.GuestNoMin.value);
        var cNumMax = parseInt(theForm.GuestNoMax.value);
        var freeLeft = '';
      if(theForm.elements['mainPartyID'].value == '1') {

              var allNodes = $$('.caltimediv input');

                  for(i = 0; i < allNodes.length; i++) {
                     if(allNodes[i].checked === true) {
                     i++;
                     var freeLeft = allNodes[i].value;
                     freeLeft = parseInt(freeLeft);
                     i=allNodes.length;
                     }
                  }
//alert(cNum+'::'+freeLeft+'::'+cNumMin+'::'+cNumMax);

        if(cNum > cNumMax || cNum < cNumMin || (freeLeft != '' && cNum > freeLeft)) {
        alert('Check Number of Participants field, and quantity of available space in Times section!');
        return false;
        }

        if(theForm.elements['PartyOption[0]'].checked == true) {
        
            if(theForm.elements['laserOptionNum[0]'].value == '') {
            alert('Missing quanity for pizza.');
            return false;
            }
        }
        
        if(theForm.elements['PartyOption[1]'].checked == true) {

            if(theForm.elements['laserOptionNum[1]'].value == '') {
            alert('Missing quanity for pizza.');
            return false;
            }
        }

        if(theForm.elements['PartyOption[2]'].checked == true) {

         if(theForm.elements['laserOptionNum[2]'].options[theForm.elements['laserOptionNum[2]'].selectedIndex].value == '') {
            alert('Select tokens from drop down.');
            return false;
            }
        }
        
      }
      
      if(theForm.elements['mainPartyID'].value == '0') {

        if(cNum > cNumMax) {
        alert('The entered number of children is greater than the maximum allowed.');
        return false;
        }
      }

  if(theForm.elements['mainPartyID'].value !='') return true;
  return false;
}

var EmailPreview = function(ifile,width,height)
  {

    var cnt = $('rvspcontact').value;
    var phn = $('rvspphone').value;
    var eml = $('rvspmail').value;
    var urltoopen = "Action/Invitation_Email_Preview.php?"+ifile+"&cnt="+cnt+"&phn="+phn+"&eml="+eml;
    //alert(urltoopen);
    window.open(urltoopen,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, innerWidth=0, innerHeight=0, width=" + width + ", height=" + height);
  }

var CheckSelection = function() {

  var cRadio = 0;
  var allNodes = $$('#radioemailtemplate input');

  if(allNodes == null || allNodes == '' || allNodes == 'undefined') {
  alert('Please select Email Template.');
  return false;
  }
  
  for(i = 0; i < allNodes.length; i++) {
    if(allNodes[i].type == 'radio' && allNodes[i].checked == true) {
        cRadio++;
        }
  }

  if(cRadio == 0) {
  alert('Please select Email Template.');
  return false;
  }
  
  return true;
}

var ClearOption = function(k) {

    if(k == '0' || k == '1') {
     var pizza = "pizzanum_"+k;
     $(pizza).value = '';
    }

    if(k == '2') {
    $('laserOptionNum[2]').options[0].selected = true;
    }

}

var SetTokenCheckBox = function() {

   if($('laserOptionNum[2]').options[0].selected == true) $('PartyOption[2]').checked = false;

}
