/**
 * @author ccortes
 */
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

$(document).ready(function() {
  $("#aspnetForm").validate({
    rules: {
      fname: {required:true},
      lname: {required:true},
      facilityname: {required:true},
      job: {required:true},
      /*phoneNumber: {required:true,maxlength: 10,minlength: 10,digits: true},*/
      email: {required:true,email: true},
      /*city: {required:true},*/
      comments: {maxlength: 1000},
      /*state: {required:true},*/
      zipcode: {required:true,maxlength: 10,digits: true}
    },
    messages: {
      fname: {required:'Please enter the correct first name.'},
      lname: {required:'Please enter the correct last name.'},
      facilityname: {required:'Please enter the correct facility name.'},
      job: {required:'Please enter the correct job.'},
      /*phoneNumber: {required:'Please enter the correct phone number.',maxlength:'Maximum 10 digits.',minlength:'Minimum 10 digits.'},*/
      email: {required:'Please enter the correct email address.',email: "Please enter a valid email."},
      /*city: {required:'Please enter the city.'},*/
      comments: {maxlength:'Maximum 1000 characteres.'},
      /*state: {required:'Please enter the state.'},*/
      zipcode: {required:'Please enter the correct zip code.',maxlength:'Maximum 10 digits.',digits:'Please enter only digits.'}
    },
    submitHandler: function(form) {
            
      /*******************XML format************************/
      var soapMessage = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
                          '<soap:Body>' +
                              '<DownloadCourseEntry xmlns="http://tempuri.org/">' +
                                  '<parentId>' + $("#downloadFile input[name=idParent]").val() + '</parentId>' +
                                  '<firstName>' + $("#downloadFile input[name=fname]").val() + '</firstName>' +
                                  '<lastName>' + $("#downloadFile input[name=lname]").val() + '</lastName>' +
                                  '<facilityName>' + $("#downloadFile input[name=facilityname]").val() + '</facilityName>' +
                                  '<jobTitle>' + $("#downloadFile input[name=job]").val() + '</jobTitle>' +
                                  '<phoneNumber>' + ''/*$("#downloadFile input[name=phoneNumber]").val()*/ + '</phoneNumber>' +
                                  '<email>' + $("#downloadFile input[name=email]").val() + '</email>' +
                                  '<city>' + ''/*$("#downloadFile input[name=city]").val()*/ + '</city>' +
                                  '<state>' + ''/*$("#downloadFile input[name=state]").val()*/ + '</state>' +
                                  '<zipCode>' + $("#downloadFile input[name=zipcode]").val() + '</zipCode>' +
                                  '<comments>' + $("#downloadFile textarea[name=comments]").val() + '</comments>' +
                                  '<addYouToMailListCheckBox>' + (($('#downloadFile input[type=checkbox]').is(':checked')) ? "True" : "False") + '</addYouToMailListCheckBox>' +
                                  '<fileNameDownloaded>' + $("#downloadFileDialog a").attr("href") + '</fileNameDownloaded>' + 
                                  '<emailProductPlatform>' + $("#downloadFile input[name=emailProductPlatform]").val() + '</emailProductPlatform>' +
                              '</DownloadCourseEntry>' +
                          '</soap:Body>' +
                        '</soap:Envelope>';
                        
      var loc = '/ws/KCHCServices.asmx?op=DownloadCourseEntry';
                        
      $.ajax({
            type: "POST",
            url: loc,
            data: soapMessage,
            success: function(message) {
              $("#downloadFile").hide();
              $("#downloadFileDialog").show();
              createCookie("kchcDwnFile", "1", 90);
              //triggerGATrackEvent('Links', 'Downloads: Resources', $("#downloadFileDialog a").attr("href"));
            },
            contentType: "text/xml; charset=utf-8",
            dataType: "xml",
            error: function(errormessage) {
                    alert("error: " + errormessage.responseText);
            }
      });
  
      return false;
    }
  });    
});

