function searchuser() {
    var value = document.getElementById("usersearch").value; 
    if(value == ''){
        alert(lang_search);
        return false;
    }else {
        document.getElementById("search").action = baseurl+"/user/page-1/sort-customers_id-desc/finder-"+value;
        document.getElementById("search").submit(); 
    }
}
function clearuser() {
    document.getElementById("search").action = baseurl+"/user";
    document.getElementById("search").submit();
}
function searchorder(id, obj){
    if(id == 'status'){
        var value1 = document.getElementById("status").value;
        var value2 = document.getElementById("payment").value;
        if(value1 == '' && value2 ==''){
            document.getElementById("search").action = baseurl+"/order/page-1/sort-orders_id-desc";
            document.getElementById("search").submit(); 
            return;
        }else {
            if(value1 == ''){
                value1 = 0;;  
            }
            if(value2 == ''){
                value2 = 0;
            }
            var value = value1+"_"+value2; 
        }
    }else {
        var value = document.getElementById(id).value;
    } 
    if(value == ''){
        alert(lang_search);
        return false;
    }else {
        document.getElementById("search").action = baseurl+"/order/page-1/sort-orders_id-desc/type-"+obj+"/finder-"+value;
        document.getElementById("search").submit();   
    }  
}
function searchuserorder(id, obj){
    var cusid = document.getElementById("customers_id").value;
    if(id == 'status'){
        var value1 = document.getElementById("status").value;
        var value2 = document.getElementById("payment").value;
        if(value1 == '' && value2 ==''){
            document.getElementById("search").action = baseurl+"/customer/profile/page-1/sort-orders_id-desc";
            document.getElementById("search").submit(); 
            return;
        }else {
            if(value1 == ''){
                value1 = 0;;  
            }
            if(value2 == ''){
                value2 = 0;
            }
            var value = value1+"_"+value2; 
        }
    }else {
        var value = document.getElementById(id).value;
    } 
    if(value == ''){
        alert(lang_search);
        return false;
    }else { 
        document.getElementById("search").action = baseurl+"/customer/profile/page-1/sort-orders_id-desc/type-"+obj+"/finder-"+value;
        document.getElementById("search").submit();   
    }  
}
function searchclear() {       
    document.getElementById("search").action = baseurl+"/order";
    document.getElementById("search").submit();     
}
function searchuserclear() { 
    var cusid = document.getElementById("customers_id").value;       
    document.getElementById("search").action = baseurl+"/customer/profile";
    document.getElementById("search").submit();     
}
function savecontact(id) {
var name= document.getElementById("customers_name").value;
if(name == ''){
    alert(selectcustomer);
}else {
var answer = confirm(lang_confirm+" "+name+"?")
if(answer){
document.getElementById("savepermanatly").value = 'Saving';
    var url = baseurl+'/user/save?orderid='+id; 
    $.post(url, $("#contactform").serialize(), function(data){
    document.getElementById("savepermanatly").value = lang_save;
    if(data != 1){
        $("#addcontact").html(data);
    }
    });
}   
}     
 } 
 function addcontact(id) {
    document.getElementById("adduserbutton").value = lang_loading+'...'
    var url = baseurl+'/user/contact'; 
    $.ajax({
   type: "GET",
   url: url,
   success: function(msg){
        $("#addcontact").html(msg);
   }
 });       
 } 
 function selectcontact() {
    var id = document.getElementById("contact_id").value;
    if(id != ''){
            var url = baseurl+'/user/selectcontact?id='+id;
            $.getJSON(url, 
            function(data){
                document.getElementById("contact_mobile").value = data['contact_mobile'];    
                document.getElementById("contact_email").value = data['contact_email'];
                if(document.getElementById("contact_internalnote")!=null)
                document.getElementById("contact_internalnote").value = data['contact_internalnote']; 
            }
        );
    }
 }
 function deletejob(id, job, idx) { 
   // document.getElementById("adduserbutton").value = 'Loading...'
    var url = baseurl+'/order/delete?orderid='+id+'&jobid='+job+'&index='+idx; 
    $.ajax({
   type: "GET",
   url: url,
   success: function(msg){
        $("#preview").html(msg);
        if(jobsize!=0){
            jobsize = jobsize-1;
        }
   }
 });       
 }
 function printinvoice() {
    $("#change").hide();
    $("#bill").hide();
    $("#back").hide();
    $("#list").hide();
    $("#print").hide();  
    window.print();
    return false;
 }     
 function searchracipt() {
    var date1 = document.getElementById("from").value;
    var date2 = document.getElementById("to").value; 
    if(date1 != '' && date2 != ''){
    date1 = date1.replace("/", "-");
    date1 = date1.replace("/", "-"); 
    date2 = date2.replace("/", "-");
    date2 = date2.replace("/", "-"); 
        document.getElementById("raciptseach").action = baseurl+"/racipt/page-1/sort-racipt_id/finder-"+date1+"_"+date2;   
        document.getElementById("raciptseach").submit();     
    }else {
        alert(lang_from);
    }
 }
function checkcash() {
    var answer = confirm(lang_pay+"?")
    if(answer){
        return true;
    }else {
        return false;
    }
 }
 function editjob(id, obj, $pay) {
    if(obj.checked){
        $("#job_description_data"+id).hide();
        $("#size_data"+id).hide(); 
        $("#pages_data"+id).hide(); 
        $("#pages_duplex_data"+id).hide(); 
        $("#pages_color_data"+id).hide(); 
        $("#unit_data"+id).hide();
        if($pay != 7){ 
            $("#price_data"+id).hide(); 
        }
        $("#paper_name_data"+id).hide();  
        $("#paper_cover_name_data"+id).hide();  
        $("#finishing_name_data"+id).hide();  
         
        $("#job_description_text"+id).show();
        $("#size_text"+id).show(); 
        $("#pages_text"+id).show(); 
        $("#pages_duplex_text"+id).show(); 
        $("#pages_color_text"+id).show(); 
        $("#unit_text"+id).show();
        if($pay != 7){ 
            $("#price_text"+id).show();
        }
        $("#paper_name_text"+id).show();
        $("#paper_cover_name_text"+id).show();
        $("#finishing_name_text"+id).show();  
    } else {
        document.getElementById("job_description_text"+id).value = document.getElementById("job_description_data"+id).innerHTML; 
        document.getElementById("size_text"+id).value = document.getElementById("size_data"+id).innerHTML;
        document.getElementById("pages_text"+id).value = document.getElementById("pages_data"+id).innerHTML;
        document.getElementById("pages_duplex_text"+id).value = document.getElementById("pages_duplex_data"+id).innerHTML;
        document.getElementById("pages_color_text"+id).value = document.getElementById("pages_color_data"+id).innerHTML;
        document.getElementById("unit_text"+id).value = document.getElementById("unit_data"+id).innerHTML;
        document.getElementById("price_text"+id).value = document.getElementById("price_data"+id).innerHTML;
        document.getElementById("paper_name_text"+id).value = document.getElementById("paper_name_data"+id).innerHTML; 
        document.getElementById("paper_cover_name_text"+id).value = document.getElementById("paper_cover_name_data"+id).innerHTML; 
        document.getElementById("finishing_name_text"+id).value = document.getElementById("finishing_name_data"+id).innerHTML; 
        $("#job_description_data"+id).show();
        $("#size_data"+id).show(); 
        $("#pages_data"+id).show(); 
        $("#pages_duplex_data"+id).show(); 
        $("#pages_color_data"+id).show(); 
        $("#unit_data"+id).show();
        $("#price_data"+id).show();
         $("#paper_name_data"+id).show();  
        $("#paper_cover_name_data"+id).show();  
        $("#finishing_name_data"+id).show(); 
         
        $("#job_description_text"+id).hide();
        $("#size_text"+id).hide(); 
        $("#pages_text"+id).hide(); 
        $("#pages_duplex_text"+id).hide(); 
        $("#pages_color_text"+id).hide(); 
        $("#unit_text"+id).hide();
        $("#price_text"+id).hide(); 
        $("#paper_name_text"+id).hide();
        $("#paper_cover_name_text"+id).hide();
        $("#finishing_name_text"+id).hide();
    }
 } 
 function editpayment(obj) {
        if(obj.checked){ 
            $("#payment_option_price_data").hide();
            $("#payment_option_price_text").show();
        }else {
            document.getElementById("payment_option_price_text").value = document.getElementById("payment_option_price_data").innerHTML; 
            $("#payment_option_price_data").show();
            $("#payment_option_price_text").hide();
        }
    } 
 function editshipping(obj) {
         if(obj.checked){ 
            $("#shipping_price_data").hide();
            $("#shipping_price_text").show();
        }else {
            document.getElementById("shipping_price_text").value = document.getElementById("shipping_price_data").innerHTML; 
            $("#shipping_price_data").show();
            $("#shipping_price_text").hide();
        }
    } 
 function sendemail(id){
        document.getElementById("email").value = lang_send+'...';
        var url = baseurl+'/ajax/mail?offerid='+id; 
        $.ajax({
       type: "GET",
       url: url,
       success: function(msg){
          document.getElementById("email").value = lang_email;  
       }
     });        
    }
 function checkshipping(obj) {   
    var customerid = document.getElementById("customers_id").value;   
     if(customerid == ''){
         var login = document.getElementById("logindata").value; 
         
         if(document.getElementById("delivery_options_id").value != 1){
         if(login == 0){
            alert(askforlogin);
         }else {
            alert(selectcustomer);        
         }
         document.getElementById("delivery_options_id").selectedIndex=0;  
         }  
         
    }else {
        shipping();
    }
    }
 function shipping() {
        var approved = document.getElementById("approved_invoice").value; 
        if(approved == 'Y') {
        var id = document.getElementById("delivery_options_id").value;
        var zip = document.getElementById("customers_delivery_postalcode").value;    
        var weight = document.getElementById("orders_weight_hidden").value;
        var oldship = document.getElementById("shipping_price_text").value;
        var ordersum = document.getElementById("order_sum").value;
        ordersum = ordersum.replace(",","");
        oldship = oldship.replace(",","");
        ordersum = parseInt(ordersum);
        oldship = parseInt(oldship); 
        $sum = parseInt(ordersum) - parseInt(oldship);  
        $("#shipping_price_image").show();
        $("#shipping_price_data").hide(); 
        var url = baseurl+'/ajax/shipping?id='+id+'&zip='+zip+'&weight='+weight;
        $.getJSON(url, 
        function(data){
            newsum =  parseInt($sum)+ parseInt(data["sell_price"]); 
            tax = newsum/4;
            total = newsum + tax 
            $("#shipping_price_data").html(data["sell_price"]);
            $("#shipping_price_text").val(data["sell_price"]); 
            $("#order_sum").val(Math.round(newsum)+".00"); 
            $("#order_sum_data").html(Math.round(newsum)+".00");  
            $("#order_tax_data").html(Math.round(tax)+".00"); 
            $("#order_total_data").html(Math.round(total)+".00"); 
            $("#order_delivery_option").html(data["style_delivery"]);
            $("#order_total_tax").html(Math.round(total)+".00");
            $("#shipping_price_image").hide();
            $("#shipping_price_data").show();
            if(id == 1){
                document.getElementById("payment_options_id").selectedIndex=0; 
            }else {
                document.getElementById("payment_options_id").selectedIndex=1; 
            }
            changepay();
        }
        );
        }else { 
            if(document.getElementById("delivery_options_id").value != 1){
                alert(lang_invoice);
                document.getElementById("delivery_options_id").selectedIndex=0;
            }     
        }
 }
  function shippingnull() {
        var approved = document.getElementById("approved_invoice").value; 
        if(approved == 'Y') {
        var id = document.getElementById("delivery_options_id").value;
        var zip = document.getElementById("customers_delivery_postalcode").value;    
        var weight = document.getElementById("orders_weight_hidden").value;
        var oldship = document.getElementById("shipping_price_text").value;
        var ordersum = document.getElementById("order_sum").value;
        ordersum = ordersum.replace(",","");
        oldship = oldship.replace(",","");
        ordersum = parseInt(ordersum);
        oldship = parseInt(oldship); 
        $sum = parseInt(ordersum) - parseInt(oldship);  
        $("#shipping_price_image").show();
        $("#shipping_price_data").hide(); 
        var url = baseurl+'/ajax/shipping?id='+id+'&zip='+zip+'&weight='+weight;
        $.getJSON(url, 
        function(data){
            newsum =  parseInt($sum)+ parseInt(data["sell_price"]); 
            tax = newsum/4;
            total = newsum + tax 
            $("#shipping_price_data").html(data["sell_price"]);
            $("#shipping_price_text").val(data["sell_price"]); 
            $("#order_sum").val(Math.round(newsum)+".00"); 
            $("#order_sum_data").html(Math.round(newsum)+".00");  
            $("#order_tax_data").html(Math.round(tax)+".00"); 
            $("#order_total_data").html(Math.round(total)+".00"); 
            $("#order_delivery_option").html(data["style_delivery"]);
            $("#order_total_tax").html(Math.round(total)+".00");
            $("#shipping_price_image").hide();
            $("#shipping_price_data").show();
            if(id == 1){
                document.getElementById("payment_options_id").selectedIndex=0; 
            }else {
                document.getElementById("payment_options_id").selectedIndex=1; 
            }
        }
        );
        }else { 
        if(document.getElementById("delivery_options_id").value != 1){
            var login = document.getElementById("logindata").value; 
             if(login == 0){
                alert(lang_invoice_notlogin);
             }else {
                alert(lang_invoice);       
             } 
        } 
         document.getElementById("delivery_options_id").selectedIndex=0;        
        }
 }
 function changepay() {  
        var approved = document.getElementById("approved_invoice").value; 
        if(approved == 'Y') {
        var id = document.getElementById("payment_options_id").value;
        var oldpay = document.getElementById("payment_option_price_text").value;
        var ordersum = document.getElementById("order_sum").value;
        $sum = parseInt(ordersum) - parseInt(oldpay);  
        $("#payment_price_image").show();
        $("#payment_option_price_data").hide(); 
        var url = baseurl+'/ajax/payment?id='+id;
        $.getJSON(url, 
        function(data){
            newsum =  parseInt($sum)+ parseInt(data["payment_options_price"]); 
            tax = newsum/4;
            total = newsum + tax 
            $("#payment_option_price_data").html(data["payment_options_price"]);
            $("#payment_option_price_text").val(data["payment_options_price"]); 
            $("#order_sum").val(Math.round(newsum)+".00"); 
            $("#order_sum_data").html(Math.round(newsum)+".00");  
            $("#order_tax_data").html(Math.round(tax)+".00"); 
            $("#order_total_data").html(Math.round(total)+".00"); 
            $("#order_payment_option").html(data["payment_options_description"]);
            $("#order_total_tax").html(Math.round(total)+".00");
            $("#payment_price_image").hide();
            $("#payment_option_price_data").show();    
            if(id == 1){
                document.getElementById("delivery_options_id").selectedIndex=0; 
                shippingnull();
            }
        }
        );  
        }else {
            
            var login = document.getElementById("logindata").value;
             if(document.getElementById("payment_options_id").value != 1){ 
            if(login == 0){
            alert(lang_invoice_notlogin);
             }else {
                alert(lang_invoice);       
             } 
            } 
            document.getElementById("payment_options_id").selectedIndex=0;   
        }
        
 }
 function uploadjobfile() {
    var answer = confirm(lang_upload+"?");
    if(answer){
        document.getElementById("uploadfile").value = 1;    
    }else {
        document.getElementById("uploadfile").value = 0;     
    }
    return true;
 }
 function changelanguage(src, lang) {
    var url = baseurl+'/language?lang='+lang; 
    $.ajax({
   type: "GET",
   url: url,
   success: function(msg){
    window.location.reload() 
   }
 });
 }
 function validatecode(id) { 
        var copyOfValue=document.getElementById(id).value;
        var newText=copyOfValue;
        var doReplace=false; 
            /*
            Remove characters that are not numbers or commas...
            */
            for(i=0; i<copyOfValue.length; i++){
                if(!isNumber(copyOfValue.charAt(i))){
                    newText=newText.replace(copyOfValue.charAt(i),"");
                    doReplace=true;
                }
     }
     if(doReplace){
        document.getElementById(id).value=newText; 
     }
 }
 function copyorder(id) {
    var answer = confirm(lang_confirm_copy+".");
    if(answer){
    window.location = baseurl+"/copy/orderid-"+id; 
    }
 } 
 function startpro() {
    $("#uploading").show();
    return true;
 } 
 function neworder() {
    var url = baseurl+'/customer/neworder'; 
        $.ajax({
       type: "GET",
       url: url,
       success: function(msg){
          window.location = baseurl+"/customer";  
       }
     });
    
 }
 function checkjobline(){
    if(checkconfirm()) {
        var str = 'Following required fields are missing\n';
        var flag = true;
        if(document.getElementById("job_description_text1").value==""){
        str +=" jobdescription"
            flag = false;
        }
        if(document.getElementById("size_text1").value==""){
        if(!flag){
            str +=" ,"
        }
        str +="size"
            flag = false;
        }
        if(document.getElementById("pages_text1").value==""){
        if(!flag){
            str +=" ,"
        }
        str +="pages"
            flag = false;
        }
        if(document.getElementById("pages_duplex_text1").value==""){
        if(!flag){
            str +=" ,"
        }
        str +="duplex"
            flag = false;
        }
        if(document.getElementById("pages_color_text1").value==""){
        str +=" pgs B/W"
            flag = false;
        }
        if(document.getElementById("unit_text1").value==""){
        if(!flag){
            str +=" ,"
        }
        str +="unit"
            flag = false;
        }
         if(document.getElementById("paper_name_text1").value==""){
         if(!flag){
            str +=" ,"
        }
        str +="paper"
            flag = false;
        } 
         if(document.getElementById("price_text1").value==""){
         if(!flag){
            str +=" ,"
        }
        str +="price"
            flag = false;
        } 
        if(flag){
           return true; 
        }else {
            alert(str);
            return false; 
        }                
        return false; 
    }else {
        return false;
    }
 }
 function submituser($link , $finder) {
    document.getElementById("search").action = $link;
    document.getElementById("usersearch").value = $finder;
    document.getElementById("search").submit();    
 }
 function addjobline() {
 var id=parseInt($("#job_count").val())+1;
 var newjobcount=parseInt($("#newjobcount").val());  
    $("#newjobcount").val(newjobcount+1); 
 $('#job_count').val(id); 
  if(!$('#addjob').is(':visible')){
    $('#addjob').show();
  }
 var str = '<tr id="main'+id+'">';
        str +='<td class="ar" >';
        str +='<div class="chk left"></div <a href="javascript:void(0)" onclick="deleteaddjob('+id+')">X</a>    </td>';
        str +='<td>'+id+'</td>';
        str +='<td><input class="bz" type="text" name="adddata['+id+'][job_description]" id="job_description_text'+id+'" value="" /></td>';
        str +='<td><input class="bz" type="text" name="adddata['+id+'][size]" id="size_text'+id+'" value="" /></td>';
        str +='<td><input class="bz" type="text" name="adddata['+id+'][pages]" id="pages_text'+id+'" value="" /></td>';
        str +='<td><input class="bz" type="text" name="adddata['+id+'][pages_duplex]" id="pages_duplex_text'+id+'" value="" /></td>';
        str +='<td><input class="bz" type="text" name="adddata['+id+'][pages_color]" id="pages_color_text'+id+'" value="" /></td>';
        str +='<td><input class="bz" type="text" name="adddata['+id+'][unit]" id="unit_text'+id+'" value=""/></td>';
        str +='<td><input class="bz" type="text" name="adddata['+id+'][paper_name]" id="paper_name_text'+id+'" value="" /></td>';
        str +='<td><input class="bz" type="text" name="adddata['+id+'][paper_cover_name]" id="paper_cover_name_text'+id+'" value="" /></td>';
        str +='<td><input class="bz" type="text" name="adddata['+id+'][finishing_name]" id="finishing_name_text'+id+'" value="" /></td>';
        str +='<td><input class="bz" type="text" name="adddata['+id+'][price]" id="price_text'+id+'" value=""/></td>';
        str+='</tr>   ';
        $("#addjob").append(str);
 }
 function deleteaddjob(id) {
    var lastid=parseInt($("#job_count").val());
    var newjobcount=parseInt($("#newjobcount").val());  
    $("#newjobcount").val(newjobcount-1); 
    if(newjobcount-1 == 0){
        $('#addjob').hide(); 
    }
    if(lastid==id){
        $('#job_count').val(lastid-1);     
    }
     $("#main"+id).remove(); 
 }

