
    $(document).ready(function(){
      //contact form processing
      $('#iam').change(function() 
        {
          if($('#iam').val() == 'Other')
            {
              //alert(''+$('#iam').val());
              $('#iam_other_label').fadeIn();
              $('#iam_other').fadeIn();
              $('#iam').css({'border':'1px solid red'});
              $('#iam_other').css({'border':'1px solid red','margin-bottom':'15px'});
            }
          else
            {
              $('#iam_other_label').fadeOut();
              $('#iam_other').fadeOut();
              $('#iam').css({'border':'1px solid silver'});
              $('#iam_other').css({'border':'1px solid silver'});
            }
        });

      $('#id_like').change(function() 
        {
          switch($('#id_like').val())
            {
              case 'Other':
                //alert(''+$('#iam').val());
                $('#id_like_other_label').fadeIn();
                $('#id_like_other').fadeIn();
                $('#id_like').css({'border':'1px solid red'});
                $('#id_like_other').css({'border':'1px solid red','margin-bottom':'15px'});
                $('#quote').fadeOut();
                $('#quote2').fadeOut();
                $('#comments').fadeOut();
              break;

              case 'Obtain technical advice':
                //alert(''+$('#iam').val());
                $('#comments').fadeIn();
                $('#quote').fadeOut();
                $('#quote2').fadeOut();
                $('#id_like_other_label').fadeOut();
                $('#id_like_other').fadeOut();
                $('#id_like').css({'border':'1px solid silver'});
                $('#id_like_other').css({'border':'1px solid silver','margin-bottom':'15px'});
              break;

              case 'Get a budget quote':
                //alert(''+$('#iam').val());
                $('#quote').fadeIn();
                $('#comments').fadeOut();
                $('#quote2').fadeOut();
                $('#id_like_other_label').fadeOut();
                $('#id_like_other').fadeOut();
                $('#id_like').css({'border':'1px solid silver'});
                $('#id_like_other').css({'border':'1px solid silver','margin-bottom':'15px'});
				$('html, body').stop(true,true).animate({ scrollTop: $("#quote").offset().top }, 1500);
              break;

              default:
                $('#quote').fadeOut();
                $('#comments').fadeOut();
                $('#quote2').fadeOut();
                $('#id_like_other_label').fadeOut();
                $('#id_like_other').fadeOut();
                $('#id_like').css({'border':'1px solid silver'});
                $('#id_like_other').css({'border':'1px solid silver','margin-bottom':'15px'});
            }
        });    

      $('#name').keypress(function() 
        {
          if($('#name').val() == '')
            {
              //alert('wrong');
              $('#name').css({'border-color':'red'});
            }
          else
           {
                 //alert('digits');
                 $('#name').css({'border-color':'silver'});
           }
        });

      $('#email').keypress(function() 
        {
          if($('#email').val() == '')
            {
              //alert('wrong');
              $('#email').css({'border-color':'red'});
            }
          else
           {
                 //alert('digits');
                 $('#email').css({'border-color':'silver'});
           }
        });
		
      $('#postcode').keypress(function() 
        {
          if($('#postcode').val() == '')
            {
              //alert('wrong');
              $('#postcode').css({'border-color':'red'});
            }
          else
           {
                 //alert('digits');
                 $('#postcode').css({'border-color':'silver'});
           }
        });

      $('#iam_other').keypress(function() 
        {
          if($('#iam_other').val() == '')
            {
              //alert('wrong');
              $('#iam_other').css({'border-color':'red'});
              $('#iam').css({'border-color':'red'});
            }
          else
           {
                 //alert('digits');
                 $('#iam_other').css({'border-color':'silver'});
                 $('#iam').css({'border-color':'silver'});
           }
        });

      $('#id_like_other').keypress(function() 
        {
          if($('#id_like_other').val() == '')
            {
              //alert('wrong');
              $('#id_like_other').css({'border-color':'red'});
              $('#id_like').css({'border-color':'red'});
            }
          else
           {
                 //alert('digits');
                 $('#id_like_other').css({'border-color':'silver'});
                 $('#id_like').css({'border-color':'silver'});
           }
        });

      $('#telephone').keypress(function() 
        {
          if($('#telephone').val().match(/[^0-9]/g))
            {
              //alert('wrong');
              $('#telephone').css({'border-color':'red'});
            }
          else
           {
                 //alert('digits');
                 $('#telephone').css({'border-color':'silver'});
           }
        });

      $('#quantity_input').keypress(function() 
        {
          //$('#target').click();
          if($('#quantity_input').val().match(/[^0-9]/g))
            {
              //alert('wrong');
              $('#quantity_input').css({'border-color':'red'});
            }
          else
           {

                 //alert('digits');
                 $('#quantity_input').css({'border-color':'silver'});

           }
        });


      $('#submit_quantity').click(function(e) 
        {
          e.preventDefault();
          $('#specification').html('<label for="window_type">Type of Window:</label><select id="window_type" name="window_type"><option selected="selected" value="Sash">Sash</option><option value="Casement">Casement</option><option value="Horizontal Slider">Horizontal Slider</option><option value="Non-opening">Non-opening</option><option value="Shaped / Arched Window">Shaped / Arched Window</option><option value="Curved Window">Curved Window</option></select><label for="panes">Number of panes:</label><select id="panes" name="panes"><option selected="selected" value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option></select><label for="height">Approximate overall height:</label><input type="text" name="height" id="height" /><label for="width">Approximate overall width:</label><input type="text" name="width" id="width" />');

          //$('#target').click();
          //if($('#quantity_input').val().match(/[^0-9]/g) == true)
          if(isNaN(parseFloat($('#quantity_input').val())))
          //if($('#quantity_input').val() == '')
            {
              //alert('wrong');
              $('#quantity_input').css({'border-color':'red'});
            }
          else
           {
             //alert('digits');
             $('#quantity_input').css({'border-color':'silver'});
             $('#quote2').fadeIn();

             var html_to_put = $('#specification').html();

             $('#specification').html('');
             $('#summary').html('');
             $('#summary').css({'border':'1px solid black','width':'95%','height':'auto','margin-top':'10px','margin-bottom':'10px','margin-left':'5px','padding':'5px'});

             $('#summary p').css({'color':'black'});

             $('#summary').append('<p>SUMMARY</p>');

             for(i=1; i<=$('#quantity_input').val();i++)
               {
                 $('#summary').append('<p class="window_'+i+'">Window '+i+':</p>');
                 $('#specification').append('<div id="window_'+i+'" class="window" style="width:95%;height:auto;margin-top:10px;margin-left:5px;margin-bottom:10px;border:1px solid gray; padding:5px;padding-bottom:20px;"><p style="margin-bottom:0px;padding:0px;">Window '+i+'</p>'+html_to_put+'<p><button id="window_'+i+'" style="display:none;" class="window_button">Update</button></p></div>');
               }

             $('#summary').append('<p id="special_p">Special finishes: </p>');

             $('#summary').css({'display':'none'});

             $('#checkboxes').children().find('input').each(function()
              {
                $(this).css({'width':'10px','height':'10px','padding':'0px','margin':'0px','outline':'none','border':'1px solid silver','position':'relative','top':'-10px'});
              });
           }
        });

      $('#submit_form').click(function(event) 
        {
          event.preventDefault();
          var valid = 'true';
          if($('#name').val() == '')
            {
              valid='false';
              $('#name').css({'border-color':'red'});
            }
          if($('#telephone').val() == '')
            {
              valid='false';
              $('#telephone').css({'border-color':'red'});
            }
          if($('#email').val() == '')
            {
              valid='false';
              $('#email').css({'border-color':'red'});
            }
          if($('#postcode').val() == '')
            {
              valid='false';
              $('#postcode').css({'border-color':'red'});
            }
          if($('#id_like').val() == 'Get a budget quote')
            {
              if($('#quantity_input').val()=='')
                {
                  valid='false';
                  $('#quantity_input').css({'border-color':'red'});
                }
			  else
			    {
                         var number_of_windows = $('#quantity_input').val(); 
                         if(number_of_windows > 0 && $('#quantity_input').val().match(/^\d{1,}$/))
                           {
						     if(!$('#window_1').find('#window_type').length)
							   {
							     valid = 'false';
								 alert('Please click the Continue button and fill in windows details.');
							   }
							 else
							   {
							     var indicator = 0;
                                 $('#specification').find('.window').each(function()
								 {
                                   indicator++;
                                 });
                                 //alert('Indicator: '+indicator+' Number of windows: '+number_of_windows);
								 if(number_of_windows != indicator)
								   {
								     valid = 'false';
									 alert('Please click Continue button an fill in the details for all '+number_of_windows+' windows.');
								   }
								 else
								   {
                                     for(a = 1; a <= number_of_windows; a++ )
                                       {
								         var height = $('#window_'+a).find('#height').val();
                                         var width = $('#window_'+a).find('#width').val();
									     if(height == '' || width == '')
									      {
									       valid = 'false';
										   //alert('Please specify the approximate width and height of the windows.');
			     				          }
                                       }
								     if(valid == 'false')
								       {
								         alert('Please specify the approximate width and height of the windows.');
								       }
								   }
							   }
                           } 
						 else
						   {
						     valid = 'false';
							 alert('Total number of windows can contain digits only.');
						   }
				}
            }

 
         if(valid == 'true')
           {
               var post_data = '';
               post_data = post_data+'name=Name: '+$('#name').val();

               if($('#company').val() != '')
                 {
                   post_data = post_data+'&company=Company: '+$('#company').val();
                 }  

               if($('#position').val() != '')
                 {
                   post_data = post_data+'&position=Position: '+$('#position').val();
                 }  

               post_data = post_data+'&telephone=Contact Telephone: '+$('#telephone').val();
               post_data = post_data+'&email=Contact Email: '+$('#email').val();
			   
               if($('#address').val() != '')
                 {
                   post_data = post_data+'&address=Address: '+$('#address').val();
                 }  			   

               if($('#postcode').val() != '')
                 {
                   post_data = post_data+'&postcode=Postcode of property: '+$('#postcode').val();
                 }  

               if($('#iam').val() != '')
                 {
                   if($('#iam').val() == 'Other')
                     {
                       post_data = post_data+'&iam=I am: '+$('#iam_other').val();
                     }
                   else
                     {
                       post_data = post_data+'&iam=I am: '+$('#iam').val();
                     }
                 }  

               if($('#id_like').val() != '')
                 {
                   switch($('#id_like').val())
                     {
                       case 'Other':
                         post_data = post_data+'&id_like=I would like to: '+$('#id_like_other').val();
                       break;

                       case 'Obtain technical advice':
                         post_data = post_data+'&id_like=I would like to: '+$('#id_like').val();
                         if($('#comments_text').val() != '')
                           {
                             post_data = post_data+'&comments=Comments: '+$('#comments_text').val();
                           }
                       break;

                       case 'Get a budget quote':
                         post_data = post_data+'&id_like=I would like to: '+$('#id_like').val();
                         if($('#how_did_you_hear').val() != '')
                           {
                             post_data = post_data+'&how_did_you_hear=How did you hear about Storm Windows: '+$('#how_did_you_hear').val();
                           }
                         var number_of_windows = $('#quantity_input').val(); 
                         if(number_of_windows > 0)
                           {
                             for(a = 1; a <= number_of_windows; a++ )
                               {
                                 post_data = post_data+'&window'+a+'=Window '+a+': '+$('#window_'+a).find('#window_type').val()+', '+$('#window_'+a).find('#panes').val()+' panes, approx height: '+$('#window_'+a).find('#height').val()+', approx width: '+$('#window_'+a).find('#width').val();
                               }
                           } 
                         if($('#matched_frames').is(":checked"))
                            {
                              post_data = post_data+'&matched_frames=Colour matched frames: YES ';
                            }
                         if($('#white_frames').is(":checked"))
                            {
                              post_data = post_data+'&white_frames=Standard white frames: YES ';
                            }
                         if($('#k_glass').is(":checked"))
                            {
                              post_data = post_data+'&k_glass=Thermally efficient K glass: YES ';
                            }
                         if($('#tough_glass').is(":checked"))
                            {
                              post_data = post_data+'&tough_glass=4mm toughened glass: YES ';
                            }
                       break;

                       default:
                         post_data = post_data+'&id_like=I would like to: '+$('#id_like').val();
                         
                     }
                 }    

//alert(''+post_data);				 


             $.ajax({
               type: 'POST',
               url: '/assets/ajax/sendmail.php',
               data: ''+post_data,
               success: function(data) 
                 {
                   alert(''+data);
                   var url = 'http://'+window.location.host+'/index.php?id=99';    
                   $(location).attr('href',url);
                 }
              });


           }
         else
          {
          }
        });
    });

