var purchaseUrl = "modules/purchase/purchase.engine.php";

function checkEmail(emailx){
      $.get(purchaseUrl,{action:'checkemail',email:emailx},
      function(data){
        $("#emailCheckResponse").show();
        $("#emailCheckResponse").html(data);
      });
}
function checkPassword(rpassword){
         var password=$("#password").val();
         if(rpassword.length<6){
                $("#passwordCheckResponse").html('<div class="error-box">Password minimal 6 karakter</div>');
         }else{
           if(password!=="" && rpassword!==password){
                $("#passwordCheckResponse").html('<div class="error-box">Kedua Password Tidak Cocok</div>');
           }else{
                $("#passwordCheckResponse").html('<img src="images/ya.png">');
           }
         }
}
function savePurchase(){
        var error=0;

        var el = document.getElementById("purchaseForm").elements;
              for (var i=0;i<el.length; i++){
                    if(el[i].value=="" ){
                            error=1;
                            alert("Silahkan lengkapi semua isian form dulu !")
                            break;
                    }
              }


        if(error==0){
          dataString = $("#purchaseForm").serialize();
          //alert('Data String :: '+dataString)
          $.ajax({
          type: "POST",
          url: purchaseUrl+"?action=savePurchase&cf=1",
          data: dataString,
          dataType : "xml",
          success: function(data) {
               //alert(data)
               $(data).find('checkresults').each(function(){
				var errorstatus = $(this).attr("errorstatus");
				if(errorstatus=='1'){
                                      $("#errormessages ul").html('');
                                      $(this).find("errormessage").each(function(){
                                          var errm  = $(this).find("message").text();
                                         $('<li></li>')
                                         .html(errm + '<br/>')
                                         .appendTo('#errormessages ul');
                                      });
                                      $("#errormessages").animate({ opacity: "show" }, "fast");
                                      $("#errormessages").animate({ opacity: "hide" }, "slow");
                                      $("#errormessages").animate({ opacity: "show" }, "slow");
                                }else{
                                    $.ajax({
                                    type: "POST",
                                    url: purchaseUrl+"?action=savePurchase&cf=0",
                                    data: dataString,
                                    dataType : "html",
                                    success: function(data) {
                                        $("#defaultPageView").html(data);
                                    }
                                });
                               }
		});
          }
          });
          return false;
        }
}
function printInvoice(){
        try{
          var oIframe = document.getElementById("iframePrint");
          var oContent = document.getElementById("invoiceData").innerHTML;
          var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
          if (oDoc.document) oDoc = oDoc.document;
          oDoc.write("<head><title>Invoice Pembelian MyFamily Accounting [metasoft.co.id]</title>");
          oDoc.write("<link type=\"text/css\" href=\"css/reports.css\" rel=\"stylesheet\" />");
          oDoc.write("</head><body onload=\"this.focus(); this.print();\">");
          oDoc.write(oContent + "</body>");
          oDoc.close();
        }catch(e){
          self.print();
        }
}
function saveConfirmation(){
        error=0;
        var x=document.getElementById("confirmationForm");
              for (var i=0;i<x.length;i++){
                    if(x.elements[i].value=="" ){
                            error=1;
                            alert("Silahkan lengkapi semua isian form dulu !")
                            break;
                    }
              }
        if(error<1){
          dataString = $("#confirmationForm").serialize();
          //alert(dataString)
          $.ajax({
          type: "POST",
          url: purchaseUrl+"?action=saveConfirmation&cf=1",
          data: dataString,
          dataType : "xml",
          success: function(data) {
               $(data).find('checkresults').each(function(){
				var errorstatus = $(this).attr("errorstatus");
				if(errorstatus=='1'){
                                      $("#errormessages ul").html('');
                                      $(this).find("errormessage").each(function(){
                                          var errm  = $(this).find("message").text();
                                         $('<li></li>')
                                         .html(errm + '<br/>')
                                         .appendTo('#errormessages ul');
                                      });
                                      $("#errormessages").animate({ opacity: "show" }, "fast");
                                      $("#errormessages").animate({ opacity: "hide" }, "slow");
                                      $("#errormessages").animate({ opacity: "show" }, "slow");
                                }else{
                                    $.ajax({
                                    type: "POST",
                                    url: purchaseUrl+"?action=saveConfirmation&cf=0",
                                    data: dataString,
                                    dataType : "html",
                                    success: function(data) {
                                        $("#defaultPageView").html(data);
                                    }
                                });
                               }
		});

          }
          });
          return false;
        }
}
function viewProfile(){
         $.get(purchaseUrl,{action:'viewProfile'},
          function(data){
            $("#web-container").html('');
            $("#web-container").append('<div id="defaultPageView"></div>');
            $("#defaultPageView").hide();
            $("#defaultPageView").html(data);
            $("#defaultPageView").show('slide','',1500,'');
          });
          
}
function buyerSetting(_save,_cf){
         if(_save==0){
           $.get(purchaseUrl,{action:'buyerSetting',save:_save,cf:_cf},
            function(data){
              $("#web-container").html('');
              $("#web-container").append('<div id="defaultPageView"></div>');
              $("#defaultPageView").hide();
              $("#defaultPageView").html(data);
              $("#defaultPageView").show('slide','',1500,'');
            });
          }else{
              dataString = $("#settingForm").serialize();
              $.ajax({
              type: "POST",
              url: purchaseUrl+"?action=buyerSetting&save=1&cf=1",
              data: dataString,
              dataType : "xml",
              success: function(xmldata) {
                   //alert(xmldata)
                   $(xmldata).find('checkresults').each(function(){
    				var errorstatus = $(this).attr("errorstatus");
    				if(errorstatus=='1'){
                                          $("#errormessages ul").html('');
                                          $(this).find("errormessage").each(function(){
                                              var errm  = $(this).find("message").text();
                                             $('<li></li>')
                                             .html(errm + '<br/>')
                                             .appendTo('#defaultPageView #errormessages ul');
                                          });
                                          $("#errormessages").animate({ opacity: "show" }, "fast");
                                          $("#errormessages").animate({ opacity: "hide" }, "slow");
                                          $("#errormessages").animate({ opacity: "show" }, "slow");
                                    }else{
                                        $.ajax({
                                        type: "POST",
                                        url: purchaseUrl+"?action=buyerSetting&save=1&cf=0",
                                        data: dataString,
                                        dataType : "html",
                                        success: function(data) {
                                            $("#defaultPageView").html(data);
                                        }
                                    });
                                   }
    		   });
               }
              });
              return false;
          }
}
function poinBgB(){
         $.get(purchaseUrl,{action:'poinBgB'},
          function(data){
            $("#web-container").html('');
            $("#web-container").append('<div id="defaultPageView"></div>');
            $("#defaultPageView").hide();
            $("#defaultPageView").html(data);
            $("#defaultPageView").show('slide','',1500,'');
          });
          
}

