$(document).ready(function(){

// ############################################################### quotation

$('#qtform').submit(function(){
	$('input[type=submit]', this).attr('disabled', 'disabled');
	$('input[type=submit]', this).css('background', '#ccc');
	var str = $(this).serialize();
   $.ajax({
   type: "POST",
   url: "/wp-content/themes/hitchens/quoteapp/process.php",
   data: str,
   success: function(msg){
   $("#noter").ajaxComplete(function(event, request, settings){
		if(msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
	{
	$('#quoteform').hide();
	$('#confirmation').show();
	$('#bar').removeClass('step2').addClass('final');
}
else
{
result = msg;
}
$(this).html(result);
});
}
});
return false;
});


$('#accform').submit(function(){
	$('input[type=submit]', this).attr('disabled', 'disabled');
	$('input[type=submit]', this).css('background', '#ccc');
	var str = $(this).serialize();
   $.ajax({
   type: "POST",
   url: "/wp-content/themes/hitchens/quoteapp/accept.php",
   data: str,
   success: function(msg){
   $("#noter").ajaxComplete(function(event, request, settings){
		if(msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
	{
	$('#quoteform').hide();
	$('#confirmation').show();
	$('#bar').removeClass('step2').addClass('final');
}
else
{
result = msg;
}
$(this).html(result);
});
}
});
return false;
});

$('#noins').change(function(){
	if($(this).is(':checked')){
		$('#insurs').hide();
	}else{
		$('#insurs').show();
	}
});
// ############################################################## normal site

$('.addval').click(function(){
	var targ = $(this).siblings('.qty');
	var currentVal = parseInt(targ.val());
            if (currentVal != NaN){
                targ.val(currentVal + 1);
            }
});

$('.subval').click(function(){
	var targ = $(this).siblings('.qty');
	var currentVal = parseInt(targ.val());
           if (currentVal != NaN && currentVal > 0){
                targ.val(currentVal - 1);
           }

});


$('.payo').change(function(){
	if($(this).is(':checked')){
		var cur = $(this).attr('rel');
		$('.payops').hide();
		$('#'+cur).show();	
	}
});


/* insert other jquery below*/

$('#heroes').cycle({
	fx: 'scrollLeft',
	timeout: 2700
});

$('#left-img').cycle({
	fx: 'fade',
	timeout: 3000,
	delay: 300,
	speed: 200
});

$('#right-img').cycle({
	fx: 'fade',
	timeout: 3000,
	delay: 800,
	speed: 200
});

$('#remov-img').cycle({
	fx: 'fade',
	timeout: 3000,
	speed: 200
});

$('#rady').click(function(){
	$('#itemsrad').show();
});

$('#radn').click(function(){
	$('#itemsrad').hide();
});

$('input[type=radio]').attr('checked', false);

$('#formbtn').click(function(){
	validate_first();						 
});


var removeLink = ' - <a class="remove" href="#" onclick="$(this).parent().slideUp(function(){ $(this).remove() }); return false">Remove</a>';

$('a.additem').relCopy({ append: removeLink});

$('#qform').submit(function(){
	
	var str = $(this).serialize();
   $.ajax({
   type: "POST",
   url: "/php/quoteform.php",
   data: str,
   success: function(msg){
   $("#noter").ajaxComplete(function(event, request, settings){
		if(msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
	{
	$('#quoteform').hide();
	$('#confirmation').show();
	$('#bar').removeClass('step2').addClass('final');
}
else
{
result = msg;
}
$(this).html(result);
});
}
});
return false;
});


/* $(document).pngFix(); */

$("#datepicker, #datepicker2, #datepicker3, #datepicker4").datepicker({ dateFormat: 'dd-mm-yy' });

});

function validate_first(){
	
	var name = $('#name');
	var email = $('#email');
	var phone = $('#phoner');
	var uplift = $('#uplift');
	var delivery = $('#delivery');
	var date = $('#datepicker');
	var error;
	
	
	$('.required').each(function(){	
		var el = $(this).attr('rel');					 
		if($(this).val() == ''){
		if($(this).hasClass('error') == false){
		$(this).addClass('error').after('<label class="er">'+ el +'</label>');
		}
		error = 1;
		}else{
		$(this).removeClass('error');
		$(this).next().remove();
		}
	});
		
		
		if(!error){
			$('#first').hide();
			$('#second').show();
			$('#bar').addClass('step2');
		}
}


/**
 * jQuery-Plugin "relCopy"
 * 
 * @version: 1.1.0, 25.02.2010
 * 
 * @author: Andres Vidal
 *          code@andresvidal.com
 *          http://www.andresvidal.com
 *
 * Instructions: Call $(selector).relCopy(options) on an element with a jQuery type selector 
 * defined in the attribute "rel" tag. This defines the DOM element to copy.
 * @example: $('a.copy').relCopy({limit: 5}); // <a href="example.com" class="copy" rel=".phone">Copy Phone</a>
 *
 * @param: string	excludeSelector - A jQuery selector used to exclude an element and its children
 * @param: integer	limit - The number of allowed copies. Default: 0 is unlimited
 * @param: string	append - HTML to attach at the end of each copy. Default: remove link
 * @param: string	copyClass - A class to attach to each copy
 * @param: boolean	clearInputs - Option to clear each copies text input fields or textarea
 * 
 */

(function($) {

	$.fn.relCopy = function(options) {
		var settings = jQuery.extend({
			excludeSelector: ".exclude",
			emptySelector: ".empty",
			copyClass: "copy",
			append: '',
			clearInputs: true,
			limit: 0 // 0 = unlimited
		}, options);
		
		settings.limit = parseInt(settings.limit);
		
		// loop each element
		this.each(function() {
			
			// set click action
			$(this).click(function(){
				var rel = $(this).attr('rel'); // rel in jquery selector format				
				var counter = $(rel).length;
				
				// stop limit
				if (settings.limit != 0 && counter >= settings.limit){
					return false;
				};
				
				var master = $(rel+":first");
				var parent = $(master).parent();						
				var clone = $(master).clone(true).addClass(settings.copyClass+counter).append(settings.append);
				
				//Remove Elements with excludeSelector
				if (settings.excludeSelector){
					$(clone).find(settings.excludeSelector).remove();
				};
				
				//Empty Elements with emptySelector
				if (settings.emptySelector){
					$(clone).find(settings.emptySelector).empty();
				};								
				
				// Increment Clone IDs
				if ( $(clone).attr('id') ){
					var newid = $(clone).attr('id') + (counter +1);
					$(clone).attr('id', newid);
				};
				
				;
				
				// Increment Clone Children IDs
				$(clone).find('[id]').each(function(){
					var newid = $(this).attr('id') + (counter +1);
					$(this).attr('id', newid);
				});
				
				//Clear Inputs/Textarea
				if (settings.clearInputs){
					$(clone).find(':input:not(.addy)').each(function(){
						var type = $(this).attr('type');
						switch(type)
						{
							case "button":
								break;
							case "reset":
								break;
							case "submit":
								break;
							case "checkbox":
								$(this).attr('checked', '');
								break;
							default:
							  $(this).val("0");
						}						
					});					
				};
				
				$(parent).find(rel+':last').after(clone);
				
				return false;
				
			}); // end click action
			
		}); //end each loop
		
		return this; // return to jQuery
	};
	
})(jQuery);
