$(document).ready(function()
{
    $(function()
    {
        $("div.scrollable").scrollable({
           vertical:false,
           size:3,
           loop: true,
           clickable: false
         });
    });

   $("input:text,input:password,textarea").focus(function()
   {
       $(this).css({"border":"1px solid #00aeef"});
   });

   $(".basketInput, #userType").blur(function()
   {
       $(this).css({"border":"1px solid #999"});
   });

   $("#orderSubmit").click(function()
   {
      $("#confirmOrderDialog").dialog
        ({
            resizable: false,
            modal: true,
            closeOnEscape: true,
            draggable: true,
            buttons:
            {
                'ok': function()
                {
                    $(this).dialog('close');
                    $("#orderForm").submit();
                },
                'mégse/cancel': function()
                {
                    $(this).dialog('close');
                    return false;
                }
            }
        })
   });

   $("#addCountryInput").blur(function()
   {
       if(MinMaxLengthValidator($(this).val(),3,100) && SpecWordValidator($(this).val())) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});
   });

   $("#copyaddress").click(function()
   {
        $("#billingZipCode").val($("#shippingZipCode").val());
        $("#billingTown").val($("#shippingTown").val());
        $("#billingAddress").val($("#shippingAddress").val());
   });

   $("#newcountry").click(function()
   {
        $("#addCountryDialog").dialog
        ({
            show: 'clip',
            hide: 'clip',
            resizable: false,
            modal: true,
            closeOnEscape: true,
            draggable: false,
            width: 400,
            height:200,
            buttons: {'Hozzáad/Add': function()
            {
                if(MinMaxLengthValidator($("#addCountryInput").val(),3,100) && SpecWordValidator($("#addCountryInput").val()))
                {
                    var country=$("#addCountryInput").val();
                    country=country.substr(0,1).toUpperCase() + country.substr(1);
                    $('#country').append( new Option(country,country));
                    $(this).dialog('close');
                }
                else $("#addCountryInput").css({"border":"1px solid #ff0000"});

            },
            'Mégse/Cancel': function() {$(this).dialog('close');}}
        });
   });

   $("#modifySubmit").click(function()
   {
       $("#ModifyLoading").css({"display":"inline"});
   });
//---------------------------input ellenőrzés-----------------------------------

    $("#subject").blur(function()
   {
       if(MinMaxLengthValidator($(this).val(),3,100)) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});
   });

   $("#message").blur(function()
   {
       if(MinMaxLengthValidator($(this).val(),0,200)) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});
   });

   $("#orderComment").blur(function()
   {
       if(rexExValidator($(this).val(),/^[^<>]*$/)) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});

   });

   $("#userName,#loginUser").blur(function()
   {
       if(rexExValidator($(this).val(),/^[0-9a-zA-Z]{5,50}$/)) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});

   });

   $("#country").blur(function()
   {
       if(MinMaxLengthValidator($(this).val(),3,100) && SpecWordValidator($(this).val())) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});

   });

   $("#email").blur(function()
   {
       if(rexExValidator($(this).val(),/^[0-9a-z\.-]+@([0-9a-z-]+\.)+[a-z]{2,4}$/i)) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});
   });
   
   $("#emailAgain").blur(function()
   {
       if(rexExValidator($(this).val(),/^[0-9a-z\.-]+@([0-9a-z-]+\.)+[a-z]{2,4}$/i) && CompareValidator($(this).val(),$("#email").val())) $(this).css({"border":"1px solid #00ff00"});
       else 
       {
           $(this).css({"border":"1px solid #ff0000"});
           $("#email").css({"border":"1px solid #ff0000"});
       }
   });

   $('#email,#emailAgain,#pw1,#pw2').live("cut copy paste",function() {
      return false;
   });

   $("#pw1,#loginPw").blur(function()
   {
       if(MinLengthValidator($(this).val(),5)) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});
   });

   $("#pw2").blur(function()
   {
       if(MinLengthValidator($(this).val(),5) && CompareValidator($(this).val(),$("#pw1").val())) $(this).css({"border":"1px solid #00ff00"});
       else 
       {
           $(this).css({"border":"1px solid #ff0000"});
           $("#pw1").css({"border":"1px solid #ff0000"});
       }
   });

   $("#modPw1").blur(function()
   {
       if($(this).val().length>0)
       {
           if(MinLengthValidator($(this).val(),5)) $(this).css({"border":"1px solid #00ff00"});
           else $(this).css({"border":"1px solid #ff0000"});
       }
       else $(this).css({"border":"1px solid #999999"});
   });

   $("#modPw2").blur(function()
   {
       if($(this).val().length>0 && $("#modPw1").val().length>0)
       {
           if(MinLengthValidator($(this).val(),5) && CompareValidator($(this).val(),$("#modPw1").val())) $(this).css({"border":"1px solid #00ff00"});
           else
           {
               $(this).css({"border":"1px solid #ff0000"});
               $("#modPw1").css({"border":"1px solid #ff0000"});
           }
       }
       else $(this).css({"border":"1px solid #999999"});
   });

   $("#fullName").blur(function()
   {
       if(MinMaxLengthValidator($(this).val(),5,100) && SpecWordValidator($(this).val())) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});
   });

   $("#mobilePhone").blur(function()
   {
       if(rexExValidator($(this).val(),/^[0-9\-\+\/]{8,20}$/)) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});
   });

   $("#shippingName ,#billingName").blur(function()
   {
       if(MinMaxLengthValidator($(this).val(),5,100) && SpecWordValidator($(this).val())) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});
   });

    $("#shippingZipCode ,#billingZipCode").blur(function()
   {
       if(rexExValidator($(this).val(),/^[0-9\-]{4,10}$/)) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});
   });

    $("#shippingTown ,#billingTown").blur(function()
   {
       if(MinMaxLengthValidator($(this).val(),2,100) && SpecWordValidator($(this).val())) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});
   });

    $("#shippingAddress ,#billingAddress").blur(function()
   {
       if(MinMaxLengthValidator($(this).val(),10,150) && SpecWordValidator($(this).val())) $(this).css({"border":"1px solid #00ff00"});
       else $(this).css({"border":"1px solid #ff0000"});
   });

   $("#taxNumber").blur(function(){
       if($(this).val().length>0)
       {
            if(rexExValidator($(this).val(),/^([0-9]{8}\-[0-9]{1}\-[0-9]{2}|[a-z]{2,3}[0-9]{8,10})$/i)) $(this).css({"border":"1px solid #00ff00"});
            else $(this).css({"border":"1px solid #ff0000"});
       }
       else $(this).css({"border":"1px solid #999999"});
   });

    $("#fantasyName").blur(function()
    {
           if($(this).val().length>0)
           {
                if(MinMaxLengthValidator($(this).val(),3,100) && SpecWordValidator($(this).val())) $(this).css({"border":"1px solid #00ff00"});
                else $(this).css({"border":"1px solid #ff0000"});
           }
           else $(this).css({"border":"1px solid #999999"});
     });

     $("#captcha").blur(function()
     {
        $(this).css({"border":"1px solid #999999"});
     });
//--------------------change-language-------------------------------------------
    $("#languageHu").click(function()
    {
        changeLanguageAjax("hu");
    });
    
    $("#languageEn").click(function()
    {
        changeLanguageAjax("en");
    });
    function changeLanguageAjax(lan)
    {
    $.ajax({
            url: "ajax/languageChange.ajax.php",
            type: "GET",
            contentType: "application/text; charset=utf-8",
            data: ({"lan": lan}),
            dataType: "text",
            beforeSend: function()
            {
                $("#LangugageDiv").css({"display":"none"});
                $("#languageLoading").css({"display":"block"});
            },
            success: function(data)
            {
                if(data=="1") window.location.reload();
                else
                {
                    $("#LangugageDiv").css({"display":"block"});
                    $("#languageLoading").css({"display":"none"});
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                alert("XMLHttpRequest=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
            }
        });
    }

//----------------------Edit-shipping-billing-data------------------------------

 $("#shippingDataEdit").click(function()
 {
     $("#shippingDataTable").slideDown();
 });

 $("#billingDataEdit").click(function()
 {
     $("#billingDataTable").slideDown();
 });

 $("#shippingDataSubmit").click(function()
 {
     var name=$("#shippingName").val();
     var zipcode=$("#shippingZipCode").val();
     var town=$("#shippingTown").val();
     var address=$("#shippingAddress").val();
     var loading="#shippingDataLoading";
     var url="ajax/modifyShippingData.ajax.php";
     addressModifyAjax(name,zipcode,town,address,loading,url);
 });

 $("#billingDataSubmit").click(function()
 {
     var name=$("#billingName").val();
     var zipcode=$("#billingZipCode").val();
     var town=$("#billingTown").val();
     var address=$("#billingAddress").val();
     var loading="#billingDataLoading";
     var url="ajax/modifyBillingData.ajax.php";
     addressModifyAjax(name,zipcode,town,address,loading,url);
 });

 //--------------------------------modify-tax-----------------------------------
$("#taxSubmit").click(function()
    {
      var taxNumber=$("#taxNumber").val();
      var fantasyName=$("#fantasyName").val();
      $.ajax({
            url: "ajax/modifyTax.ajax.php",
            type: "GET",
            contentType: "application/text; charset=utf-8",
            data: ({"taxNumber": taxNumber,"fantasyName": fantasyName}),
            dataType: "text",
            beforeSend: function()
            {
                $("#taxModifyLoading").css({"display":"inline"});
            },
            success: function(data)
            {
                $("#taxModifyLoading").css({"display":"none"});
                 $("#modifyDialog").html(data);
                    $("#modifyDialog").dialog({
                        show: 'clip',
                        hide: 'clip',
                        resizable: false,
                        modal: true,
                        closeOnEscape: true,
                        draggable: false
                    });
            },
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                alert("XMLHttpRequest=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
            }
        });
});

//---------------------Login-Ajax-----------------------------------------------

 $("#loginSubmit").click(function()
    {
      var user=$("#loginUser").val();
      var password=$("#loginPw").val();
      $.ajax({
            url: "ajax/login.ajax.php",
            type: "GET",
            contentType: "application/text; charset=utf-8",
            data: ({"user": user,"password": password}),
            dataType: "text",
            beforeSend: function()
            {
                $("#loginLoading").css({"display":"block"});
            },
            success: function(data)
            {
                $("#loginLoading").css({"display":"none"});
                if(data=="1")
                {
                    window.location.reload();
                }
                else
                {
                    $("#loginDialog").html(data);
                    $("#loginDialog").dialog({
                        show: 'clip',
                        hide: 'clip',
                        resizable: false,
                        modal: true,
                        closeOnEscape: true,
                        draggable: false
                    });
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                alert("XMLHttpRequest=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
            }
        });
    });
});

//-----------------------basket-------------------------------------------------

function editBasket(id,countID)
    {
    $.ajax({
            url: "ajax/editBasket.ajax.php",
            type: "GET",
            contentType: "application/text; charset=utf-8",
            data: ({"producttypeID": id, "count": $("#"+countID).val()}),
            dataType: "text",
            beforeSend: function()
            {
                $("#mainLoading").css({"display":"block"});
            },
            success: function(data)
            {
               $("#mainLoading").css({"display":"none"});
               if(data=="1") window.location.reload();
               else
               {
                   $("#ajaxError").html(data);
                   $("#ajaxError").dialog(
                    {
                        show: 'clip',
                        hide: 'clip',
                        resizable: false,
                        modal: true,
                        closeOnEscape: true,
                        draggable: false
                    });
               }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                alert("XMLHttpRequest=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
            }
        });
}

function deleteBasket(id)
{
  $("#delete").dialog
    ({
	resizable: false,
        modal: true,
	closeOnEscape: true,
        draggable: true,
	buttons: {'törlés/delete': function()
        {
            $(this).dialog('close');
            $.ajax({
            url: "ajax/deleteBasket.ajax.php",
            type: "GET",
            contentType: "application/text; charset=utf-8",
            data: ({"producttypeID": id}),
            dataType: "text",
            beforeSend: function()
            {
                $("#mainLoading").css({"display":"block"});
            },
            success: function(data)
            {
                if(data=="1") window.location.reload();
                else
                {
                    $("#ajaxError").html(data);
                    $("#ajaxError").dialog(
                    {
                        show: 'clip',
                        hide: 'clip',
                        resizable: false,
                        modal: true,
                        closeOnEscape: true,
                        draggable: false
                    });
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                alert("XMLHttpRequest=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
            }
        });
        },
	'mégse/cancel': function() {$(this).dialog('close');}}
    })
}

function addBasket(producttypeID)
{
    $.ajax({
                url: "ajax/addBasket.ajax.php",
                type: "GET",
                contentType: "application/text; charset=utf-8",
                data: ({"producttypeID": producttypeID}),
                dataType: "text",

                success: function(data)
                {
                    if(data=="1" ||data=="2")
                    {
                        $("#basketDialog").html("A termék a kosárba került!<br>The product was put into the basket!");
                    }
                    else $("#basketDialog").html(data);
                    if(data=="1") $("#basketCount").html(parseInt($("#basketCount").html())+1);

                    $("#basketDialog").dialog(
                    {
                        show: 'clip',
                        hide: 'clip',
                        resizable: false,
                        modal: true,
                        closeOnEscape: true,
                        draggable: false
                    });
                },
                error: function(XMLHttpRequest, textStatus, errorThrown)
                {
                    alert("XMLHttpRequest=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
                }
             });
}

//-----------------------category-----------------------------------------------

function openSubCategory(plusDiv,subCat)
{
       subCat="#"+subCat;
       plusDiv="#"+plusDiv;
       if($(subCat).css("display")=="none")
       {
           $(plusDiv).addClass("categoryMinus");
           $(subCat).slideDown();
       }
       else
       {
           $(plusDiv).removeClass("categoryMinus");
           $(subCat).slideUp();
       }
}

//-----------------------address------------------------------------------------

function addressModifyAjax(name,zipcode,town,address,loading,url)
 {
    $.ajax({
            url: url,
            type: "GET",
            contentType: "application/text; charset=utf-8",
            data: ({"name": name, "zipcode": zipcode, "town": town , "address": address}),
            dataType: "text",
            beforeSend: function()
            {
                $(loading).css({"display":"inline"});
            },
            success: function(data)
            {
                if(data=="1") window.location.reload();
                else
                {
                    $(loading).css({"display":"none"});
                    $("#modifyDialog").html(data);
                    $("#modifyDialog").dialog({
                        show: 'clip',
                        hide: 'clip',
                        resizable: false,
                        modal: true,
                        closeOnEscape: true,
                        draggable: false
                    });
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                alert("XMLHttpRequest=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
            }
         });
 }

//----------------------shipping-fee--------------------------------------------

function modifyShippingFee(usertypeID,hun,paymentMode,postTransferCost,postCollectCost,postAbroadCost,paycheck,foreorderpaycheck,euro)
{
    var instockShippingfee=shippingfee(usertypeID,hun,paymentMode,postTransferCost,postCollectCost,postAbroadCost,paycheck,euro);
    var foreorderShippingfee=shippingfee(usertypeID,hun,paymentMode,postTransferCost,postCollectCost,postAbroadCost,foreorderpaycheck,euro);

    var gross=parseFloat(paycheck)+parseFloat(instockShippingfee);
    var grosseuro=Math.round(gross/euro*100)/100;
    $("#grossPriceFt").html(gross);
    $("#grossPriceEuro").html(grosseuro);
    $("#shippingfeeEuro").html(Math.round(instockShippingfee/euro*100)/100);
    $("#shippingfeeFt").html(instockShippingfee);
    $("#foreOderShippingfeeEuro").html(Math.round(foreorderShippingfee/euro*100)/100);
    $("#foreOderShippingfeeFt").html(foreorderShippingfee);
}

function shippingfee(usertypeID,hun,paymentMode,postTransferCost,postCollectCost,postAbroadCost,paycheck,euro)
{
    if(paycheck==0) return 0;
    if(hun==true)
    {
        if(usertypeID==1)
        {
            if(paymentMode==1)
            {
                if(paycheck>=25000) return 0;
                return postCollectCost;
            }
            if(paymentMode==2)
            {
                if(paycheck>=5000) return 0;
                return postTransferCost;
            }
            if(paymentMode==3)
            {
                return 0;
            }

//            if(paymentMode==2 || paymentMode==3)
//            {
//                if(paycheck>=5000) return 0;
//                return postTransferCost;
//            }
        }

        if(usertypeID==3 || usertypeID==2)
        {
            if(paymentMode==2 || paymentMode==3)
            {
                if(paycheck/1.27>=25000) return 0;
                return postTransferCost;
            } 
        }
    }
    else
    {
        if (usertypeID==1)
        {
            if(paymentMode==3) return 0;

            if(paycheck/euro>=200) return 0;
            return postAbroadCost*euro;
        }

        if(usertypeID==3 || usertypeID==2)
        {
            return postAbroadCost*euro;
        }
    }
}

//------------------------------Validátorok-------------------------------------

function rexExValidator(value,regEx)
{
   if(!regEx.test(value)) return false;
   return true;
}

function MinMaxLengthValidator(value,min,max)
{
    if(value.length>=min && value.length<=max) return true;
    return false;
}

function SpecWordValidator(value)
{
    var reg_specWord=/(select|update|delete|insert|drop|alter|truncate|join|create|where|script|input|option|optiongroup|button|textarea)+/i;
    if(reg_specWord.test(value)) return false;
    return true;
}

function MinLengthValidator(value,min)
{
    if(value.length<min) return false;
    return true;
}

function CompareValidator(value1,value2)
{
    if(value1==value2) return true;
    return false;
}
