$(document).ready(function(){

var theform = $("#styleform");
	
	//hide form elements
	setTimeout('$("#webformcontent, #printformcontent, #webquicklinks, #printquicklinks").slideUp("normal");',750);
	
	/****************************************
	
	Checkboxes
	
	****************************************/	
	
	//float all labels with class checklab & checklab3 because stylefloats destroy non scripted layout
	$(".checklab, .checklab3").css({float:"left"});
	
	//for all checkboxes 
	$("input[type='checkbox']").each(function(e){
		var name = $(this).attr("name");
		//add hidden form elements
		theform.prepend('<input type="hidden" id="' + name + '" name="' + name + '" value="false" />');
		
		//insert replacement elements
		$(this).before('<div id="checkdiv' + e + '" class="styledcont">').after('</div>').remove();
		$("#checkdiv" + e).html('<p id="checkp' + e + '" class="fakeformbut"></p>');
		
		//Define the click
		$("#checkp" + e + ", label[for='" + name + "']").click(function(){
			if($("#" + name).attr("value")=="false"){
				$("#" + name).attr("value", "true");
				$("#checkp" + e).css({backgroundPosition:"0px -25px"});
			}else{
				$("#" + name).attr("value", "false");
				$("#checkp" + e).css({backgroundPosition:"0px 0px"});
			};
		});	
		
	}); //end each
	
	
	/********************************
	
	radio buttons wrapped with div.radiobuuttons
	
	*********************************/
	
	//radio button div
	$(".radiobuttons").each(function(i){
		var theid = $(this).attr("id");
		
		//$(this).removeAttr("id");
		//add hidden form elements
		theform.prepend('<input type="hidden" id="' + theid + 'id" name="' + theid + '" value="false" />');
		
		
		//collect each radio button within div
		$("div[id='" + theid + "'] input[type='radio']").each(function(e){
			var name = $(this).attr("id");
			var valu = $(this).attr("value");
			//insert replacement elements
			$(this).before('<div id="radiodiv' + i + e + '" class="styledcont">').after('</div>').remove();
			$("#radiodiv" + i + e).html('<p id="radiop' + i + e + '" class="fakeformbut"></p>');
		
		
			//Define the click
			$("#radiop" + i + e + ", label[for='" + name + "']").click(function(){
				//if its not selected
				if($("#" + theid + "id").attr("value")!=valu){
					$("#" + theid + "id").attr("value", valu);
					$("div[id='" + theid + "'] .fakeformbut").css({backgroundPosition:"0px 0px"});
					
					$("#radiop" + i + e).css({backgroundPosition:"0px -25px"});
					
				}else{
					//if its clicked twice nothing happens
					//$("#" + theid).attr("value", "false");
					//$("#radiop" + i + e).css({backgroundPosition:"0px 0px"});
				};
			});	
		
		});
	
	});
	
	
	//define onclicks for web and print quote
	/*$("#checkp0, #webqlab").click(function(){
		$("#webformcontent, #webquicklinks").slideToggle("normal");
		
		if($("#formwebquote").attr("value") == "true"){
			var target = $("a[rel='webalink']");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 1000);
		}else{
			$('html,body').animate({scrollTop: 0}, 500);
		};
	});
	$("#checkp1, #printqlab").click(function(){
		$("#printformcontent, #printquicklinks").slideToggle("normal");
		
		if($("#formprintquote").attr("value") == "true"){
			var target = $("a[rel='printalink']");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 1000);
		}else{
			$('html,body').animate({scrollTop: 0}, 500);
		};
	});
	*/
	
	/*********************************
	
	Begin AJAX!!!!
	
	*********************************/
	
	$("#styleform").submit(function() {
    	$('.emailerror2').hide();

		
		//Retrieve fields
		var formname = $("input[name='formname']").val();
		var formemail = $("input[name='formemail']").val();
		var formphone = $("input[name='formphone']").val();
		var formsubject = $("input[name='formsubject']").val();
		var formcountry = $("input[name='formcountry']").val();
		var formmessage = $("textarea[name='formmessage']").val();
		
		//alert ("formname=" + formname + "&formemail=" + formemail);return false;
		
		//VALIDATION
        if (formname == "") {
			$("div.emailerror2").html("Please enter your name:");
        	$("div.emailerror2").slideToggle("normal");
			
			var target = $("div.emailerror2");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formname']").focus();});
        	return false;
        }
		
		if (formemail == "") {
			$("div.emailerror2").html("Please enter your email address:");
        	$("div.emailerror2").slideToggle("normal");
		
			var target = $("div.emailerror2");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formemail']").focus();});
        	return false;
        } else if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(formemail)) {
			/*alert("ok");*/
		} else {
			$("div.emailerror2").html("Please enter your valid email address:");
        	$("div.emailerror2").slideToggle("normal");
		
			var target = $("div.emailerror2");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formemail']").focus();});
        	return false;
		}
		
		if (formphone == "") {
			$("div.emailerror2").html("Please enter your phone:");
        	$("div.emailerror2").slideToggle("normal");
			
			var target = $("div.emailerror2");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formphone']").focus();});
        	return false;
        }
		if (formsubject == "") {
			$("div.emailerror2").html("Please enter your subject:");
        	$("div.emailerror2").slideToggle("normal");
			
			var target = $("div.emailerror2");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formsubject']").focus();});
        	return false;
        }
		if (formcountry == "") {
			$("div.emailerror2").html("Please enter your country:");
        	$("div.emailerror2").slideToggle("normal");
			
			var target = $("div.emailerror2");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formcountry']").focus();});
        	return false;
        }
		
		if (formmessage == "") {
			$("div.emailerror2").html("Please enter your message:");
        	$("div.emailerror2").slideToggle("normal");
			
			var target = $("div.emailerror2");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formmessage']").focus();});
        	return false;
        }
		//Begin send.. show icon slide up sidepanels
		$('html,body').animate({scrollTop: 0}, 200);
		$("#webquicklinks, #printquicklinks").slideUp("normal");
		$("#contact_area").html("<div id='contactsendbox'><div id='fadeable'></div></div>");
		$("#fadeable").html("<div id='emailsending'>Sending Please wait...</div>").hide().fadeIn(500);
		
		
		//populate data input
		var dataString = 
		'formname=' + formname + 
		'&formemail=' + formemail + 
		'&formphone=' + formphone + 
		'&formsubject=' + formsubject +
		'&formcountry=' + formcountry +
		'&formmessage=' + formmessage +
		'&subby=ajax';

		
		$.ajax({
    		type: "POST",
    		url: "http://www.apicsoft.com/formsubmit.php",
    		data: dataString,
    		success: function() {
				
      			$("#emailsending").fadeOut(500, function(){$("#fadeable").html("<div id=\'emailsuccess\'>Thanks, we&#39;ll be in touch very soon...</div>").hide().fadeIn(1000);});
					
			}
  		}); return false;
		
   }); //form submit contact close

/*******************************************************************************************/
//form quote start
/*******************************************************************************************/
var theform = $("#quoteForm");
	
	//hide form elements
	setTimeout('$("#webformcontent, #printformcontent, #webquicklinks, #printquicklinks").slideUp("normal");',750);
	
	/****************************************
	
	Checkboxes
	
	****************************************/	
	
	//float all labels with class checklab & checklab3 because stylefloats destroy non scripted layout
	$(".checklab, .checklab3").css({float:"left"});
	
	//for all checkboxes 
	$("input[type='checkbox']").each(function(e){
		var name = $(this).attr("name");
		//add hidden form elements
		theform.prepend('<input type="hidden" id="' + name + '" name="' + name + '" value="false" />');
		
		//insert replacement elements
		$(this).before('<div id="checkdiv' + e + '" class="styledcont">').after('</div>').remove();
		$("#checkdiv" + e).html('<p id="checkp' + e + '" class="fakeformbut"></p>');
		
		//Define the click
		$("#checkp" + e + ", label[for='" + name + "']").click(function(){
			if($("#" + name).attr("value")=="false"){
				$("#" + name).attr("value", "true");
				$("#checkp" + e).css({backgroundPosition:"0px -25px"});
			}else{
				$("#" + name).attr("value", "false");
				$("#checkp" + e).css({backgroundPosition:"0px 0px"});
			};
		});	
		
	}); //end each
	
	
	/********************************
	
	radio buttons wrapped with div.radiobuuttons
	
	*********************************/
	
	//radio button div
	$(".radiobuttons").each(function(i){
		var theid = $(this).attr("id");
		
		//$(this).removeAttr("id");
		//add hidden form elements
		theform.prepend('<input type="hidden" id="' + theid + 'id" name="' + theid + '" value="false" />');
		
		
		//collect each radio button within div
		$("div[id='" + theid + "'] input[type='radio']").each(function(e){
			var name = $(this).attr("id");
			var valu = $(this).attr("value");
			//insert replacement elements
			$(this).before('<div id="radiodiv' + i + e + '" class="styledcont">').after('</div>').remove();
			$("#radiodiv" + i + e).html('<p id="radiop' + i + e + '" class="fakeformbut"></p>');
		
		
			//Define the click
			$("#radiop" + i + e + ", label[for='" + name + "']").click(function(){
				//if its not selected
				if($("#" + theid + "id").attr("value")!=valu){
					$("#" + theid + "id").attr("value", valu);
					$("div[id='" + theid + "'] .fakeformbut").css({backgroundPosition:"0px 0px"});
					
					$("#radiop" + i + e).css({backgroundPosition:"0px -25px"});
					
				}else{
					//if its clicked twice nothing happens
					//$("#" + theid).attr("value", "false");
					//$("#radiop" + i + e).css({backgroundPosition:"0px 0px"});
				};
			});	
		
		});
	
	});
	
	
	//define onclicks for web and print quote
	/*$("#checkp0, #webqlab").click(function(){
		$("#webformcontent, #webquicklinks").slideToggle("normal");
		
		if($("#formwebquote").attr("value") == "true"){
			var target = $("a[rel='webalink']");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 1000);
		}else{
			$('html,body').animate({scrollTop: 0}, 500);
		};
	});
	$("#checkp1, #printqlab").click(function(){
		$("#printformcontent, #printquicklinks").slideToggle("normal");
		
		if($("#formprintquote").attr("value") == "true"){
			var target = $("a[rel='printalink']");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 1000);
		}else{
			$('html,body').animate({scrollTop: 0}, 500);
		};
	});
	*/
	
	/*********************************
	
	Begin AJAX!!!!
	
	*********************************/
	
	$("#quoteForm").submit(function() {
    	$('.emailerror1'/*,'.emailerror21','.emailerror3','.emailerror4','.emailerror5'*/).hide();

		
		//Retrieve fields
		var formname1 = $("input[name='formname1']").val();
		var formemail1 = $("input[name='formemail1']").val();
		var formphone1 = $("input[name='formphone1']").val();
		var formsubject1 = $("input[name='formsubject1']").val();
		/*var formcountry = $("input[name='formcountry']").val();*/
		var formmessage1 = $("textarea[name='formmessage1']").val();
		
		//alert ("formname=" + formname + "&formemail=" + formemail);return false;
		
		//VALIDATION
        if (formname1 == "") {
			$("div.emailerror1").html("Enter your name:");
        	$("div.emailerror1").slideToggle("normal");
			
			var target = $("div.emailerror1");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formname1']").focus();});
        	return false;
        }
		
		if (formemail1 == "") {
			$("div.emailerror1").html("Enter your email:");
        	$("div.emailerror1").slideToggle("normal");
		
			var target = $("div.emailerror1");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formemail1']").focus();});
        	return false;
        }
		if (formphone1== "") {
			$("div.emailerror1").html("Enter your phone:");
        	$("div.emailerror1").slideToggle("normal");
			
			var target = $("div.emailerror1");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formphone1']").focus();});
        	return false;
        }
		if (formsubject1 == "") {
			$("div.emailerror1").html("Enter Quote for:");
        	$("div.emailerror1").slideToggle("normal");
			
			var target = $("div.emailerror1");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formsubject1']").focus();});
        	return false;
        }
		/*if (formcountry == "") {
			$("div.emailerror2").html("Please enter your country:");
        	$("div.emailerror2").slideToggle("normal");
			
			var target = $("div.emailerror2");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formcountry']").focus();});
        	return false;
        }*/
		
		if (formmessage1 == "") {
			$("div.emailerror1").html("Please enter your message:");
        	$("div.emailerror1").slideToggle("normal");
			
			var target = $("div.emailerror1");
			var targetOffset = target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 500,function(){$("input[name='formmessage1']").focus();});
        	return false;
        }
		//Begin send.. show icon slide up sidepanels
		$('html,body').animate({scrollTop: 0}, 500);
		$("#webquicklinks, #printquicklinks").slideUp("normal");
		$("#quotation").html("<div id='quotesendbox'><div id='fadeable'></div></div>");
		$("#fadeable").html("<div id='quotesending'>Sending Please wait...</div>").hide().fadeIn(500);
		
		
		//populate data input
		var dataString = 
		'formname1=' + formname1 + 
		'&formemail1=' + formemail1 + 
		'&formphone1=' + formphone1 + 
		'&formsubject1=' + formsubject1 +
		/*'&formcountry=' + formcountry +*/
		'&formmessage1=' + formmessage1 +
		'&subby1=ajax1';

		
		$.ajax({
    		type: "POST",
    		url: "http://www.apicsoft.com/quotesubmit.php",
    		data: dataString,
    		success: function() {
				
      			$("#quotesending").fadeOut(500, function(){$("#fadeable").html("<div id=\'quotesuccess\'>Thanks, we&#39;ll be in touch very soon...</div>").hide().fadeIn(1000);});
				/*location.reload.fadeIn(1000)	*/
				setInterval("location.reload().html.hide().fadeIn(500);", 3000);
			}
  		}); return false;
		
   }); //form submit quoteclose
});//document ready close

