$('document').ready(function(){
 

   //start postage selections 
    $('#post').change(function(){
       $("#submit").html("");
       
        if($("#post").val()!="")
        {
            if($("#post").val()=="SA")
            {
             $("#insurance").html("");
             
            
            $.post(
       
    "ajax.php?op=country", //Ajax file 
  
    //function that is called when server returns a value.
    function(data){
         content= "<br /><br /><select id='province_select'><option value=''>Please Select Province</option>"
        for(var i=0; i<data.length; i++)
        {
            
             content+="<option value='"+data[i]["area_id"]+"'>"+data[i]['area_name']+"</option>"
        }
        content+="</select>"
        
        $('#province').html(content)
    },
   
    //How you want the data formated when it is returned from the server.
    "json"
    );
    

        
        
        $("#insurance").html("")
         $("#town").html("")
            }
            else
            {
                $("#insurance").html(
        "<br /><br /><select id='insure_select' name='insure_select'>"+
        "<option value=''>Add Insurance (2% of total price)</option>"+ 
        "<option value='Yes'>Yes</option>"+
        "<option value='No'>No</option></select>")
            
                          $("#postage").html("") 
                          $("#town").html("");
                          $("#province").html("") 
                          
            }
        }
        else
        {
            $("#postage").html("");
            $("#town").html(""); 
            $("#submit").html("");
              $("#insurance").html("");
        }      $("#province").html("")
        
    })
    
    
    $("#province_select").live("change", function(){ 
        
               $("#submit").html(""); 
        if($("#province_select").val()!="")
        {
            if($("#post").val()=="SA"&&$("#province_select")!="")
            {
                $.post(
       
    "ajax.php?op=town&id="+$("#province_select").val(), //Ajax file 
   
    //function that is called when server returns a value.
    function(data){
         content= "<br /><br /><select id='town_select'><option value=''>Please Select Town</option>"
        for(var i=0; i<data.length; i++)
        {
            
             content+="<option value='"+data[i]['area_name']+"'>"+data[i]['area_name']+"</option>"
        }
         content+="<option value='Other'>OTHER</option>"
        content+="</select>"
        
        $('#town').html(content)
    },
   
    //How you want the data formated when it is returned from the server.
    "json"
    );
        
            }

        }
        else
        {
            $('#town').html("")
            $("#postage").html("");
            $("#submit").html("");
        }
    })
    
    $("#town_select").live("change", function(){ 
        
               $("#submit").html(""); 
        if($("#town_select").val()!="")
        {
            if($("#post").val()=="SA")
            {
                if($("#town_select").val()!="Other")
                {
        $("#postage").html(
        "<br /><br /><select id='post_type' name='post_type'>"+
        "<option value=''>Select Delivery</option>"+ 
        "<option value='Post Office'>Post Office</option>"+
        "<option value='Courier'>Courier</option></select>")
        $("#insurance").html("")
                }
                else
                {
                            $("#postage").html(
        "<br /><br /><select id='post_type' name='post_type'>"+
        "<option value=''>Select Delivery</option>"+ 
        "<option value='Post Office'>Post Office</option>")
                    
                     $("#insurance").html("") 
                }
        
            }
            else
            {
                $("#insurance").html(
        "<br /><br /><select id='insure_select' name='insure_select'>"+
        "<option value=''>Add Insurance (2% of total price)</option>"+ 
        "<option value='Yes'>Yes</option>"+
        "<option value='No'>No</option></select>")
            
                          $("#postage").html("") 
            }
        }
        else
        {
            $("#postage").html("");
            $("#submit").html("");
        }
    })
    
    $("#post_type").live("change", function(){
        
        if($("#post_type").val()!="")
        {
           $("#submit").html("<br /><br /><input type='button' id='submit_paypal' value='Pay with Paypal'>&nbsp;<input type='button' id='submit_vcs' value='Pay with VCS' >");  
        }
        else
        {
                       $("#submit").html("");;  
        }
        
    })
     $("#insurance").live("change", function(){
        
        if($("#insure_select").val()!="")
        {
           $("#submit").html("<br /><br /><input type='button' id='submit_paypal' value='Pay with Paypal'>&nbsp;<input type='button' id='submit_vcs' value='Pay with VCS' >");  
        }
        else
        {
                       $("#submit").html("");;  
        }
        
    })
  
  //end selections
   
   //submit form to payment gateway (VCS) 
    $('#submit_form').click(function(){
        
                      
             var value=$("#street").val()+"|"+$('#suburb').val()+"|"+$('#city').val()+"|"+$('#zip').val()     
             $("#m_7").val(value) 
            escape($("#m_7").val())
           
           
            
        var msg="This is a required field."
               var check=""
    if($('#m7').val()=="")
    {
       $("#er_m7").html(msg) 
       check="1"
    }
    else
    {
       $("#er_m7").html("")  
    }
    if($('#m6').val()=="")
    {
       $("#er_m6").html(msg) 
       check="1"
    }
        else
    {
         escape($("#m6").val())
       $("#er_m6").html("")  
    }
    if($('#m3').val()=="")
    {
         
       $("#er_m3").html(msg) 
       check="1"
    }
        else
    {
        escape($("#m3").val())
       $("#er_m3").html("")  
    }
    if($('#m2').val()=="")
    {
         
       $("#er_m2").html(msg)
       check="1" 
    }
        else
    {
        escape($("#m2").val())
       $("#er_m2").html("")  
    }
    if($('#m1').val()=="")
    {
       $("#er_m1").html(msg)
       check="1"
    } 
        else
    {
        //escape($("#m1").val())
       $("#er_m1").html("")  
    }
    

    
    if(check=="")  
    {                                
 $('#vcs_form').submit();

    }

    })
    
    
     $("#btnSubmit").click(function(){
        
     var name=$("#txtName").val()
     var comment=$("#txtComment").val()
     var email=$("#txtEmail").val()
     var prodid=$("#prod_id").val()
     
    if(name&&email&&$("#recaptcha_response_field").val()&&comment) 
      {
        
         
       $.post("custom_html/product.php",{ recaptcha_response_field: $("#recaptcha_response_field").val(), recaptcha_challenge_field: $("[name=recaptcha_challenge_field]").val(), name: name, email: email, comment: comment,prodid:prodid },
   function(data){
     alert(data);
     
     
     if(data.substring(0,5)=="Thank")
     {
       
       $("#txtComment").val("")
       $("#txtEmail").val("")
       $("#txtName").val("") 
       Shadowbox.close()
     }
     
      Recaptcha.reload() 
     }, "text"); 
    }
    else
    {
        alert("Please fill in all required fields")
    }
        
  
        
    })

})

$(".change_cur").live("change",function(){
   
    var url=$(".change_cur").val()
    
    if(url!="")
    {
        window.location=url
    }
})

//update price when quantity changes (cart_list_vcs.tmpl)
   function updatePrice(line, currency)
    {
        var price=$('#prod_price'+line).html().substr(1)
        var qty=$('#prod_qty'+line).val()
        if(qty=="")
        {
            qty=1
        }
        var sub=price*qty
        sub=sub.toFixed(2)
        var total=0
        $('#prod_sub'+line).html(currency+sub)
        
       var subs= $('span[name=sub]') 
        
          subs.each( function() 
          {
             
            var subtotal=$(this).html().substr(1)   
            total=parseFloat(total)+parseFloat(subtotal)
            
           })
           total=total.toFixed(2)
         $("#prod_total").html(currency+total) 
    }
    
    //submit postage information to vcs form
    $('#submit_vcs').live("click", function(){
    
        if($("#txtEmail").val())
        {
        $("#gate").val("vcs");
        $("#postage_form").submit();
        }
        else
        {
            alert("Please enter your email address")
        }
    })
    
   //submit postage information to paypal
        $('#submit_paypal').live("click", function(){
        
            
            
        if($("#txtEmail").val())
        {
            
        
            
        if($("#post").val()=="SA")
        {
            var x=confirm("Please note: Paypal does not support SA Rand as a currency. From here on out the transaction will be in US Dollars. Click 'Ok' to continue, or 'Cancel' to go back and pay with VCS")
        
            if(x)
            {
                $("#gate").val("paypal");
        $("#postage_form").submit();
            }
        
        }
        else
        {
            $("#gate").val("paypal");
        $("#postage_form").submit();
        }
        }
        else
        {
            alert("Please enter your email address")
        }
        
    })

