// wait for the DOM to be loaded 
$(document).ready(function() { 
$('.error').hide();

// cddetails - character limiter
function limitChars(textid, limit, infodiv)
{
var text = $('#'+textid).val(); 
var textlength = text.length;
if(textlength > limit) {
	$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
	$('#'+textid).val(text.substr(0,limit));
	return false;
	} else {
	$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
	return true;
	}
}	   

// form validation
$(function() {

// text limiter for the textarea so they don't go all crazy with the description.
$('#cddetails').keyup(function(){
	limitChars('cddetails', 1000, 'charlimitinfo');
})
	
	   
		$(".scheduleformsubmit").click(function() {
			
			$('.error').hide();
			
			// validate the form first
			var cdsendtoaddress = $("input#cdsendtoaddress").val(); 
			cdsendtoaddress =  $.trim(cdsendtoaddress); 				  
        		if (cdsendtoaddress == "") {   
      				$("span#cdsendtoaddress_error").show();   
      				$("input#cdsendtoaddress").focus();   
      			return false;
	    	}
			
			var cdfirstname = $("input#cdfirstname").val(); 
			cdfirstname =  $.trim(cdfirstname); 				  
        		if (cdfirstname == "") {   
      				$("span#cdfirstname_error").show();   
      				$("input#cdfirstname").focus();   
      			return false;
	    	}
			
			var cdlastname = $("input#cdlastname").val(); 
			cdlastname =  $.trim(cdlastname); 				  
        		if (cdlastname == "") {   
      				$("span#cdlastname_error").show();   
      				$("input#cdlastname").focus();   
      			return false;
	    	}
			
			var cdphonenum = $("input#cdphonenum").val();
			cdphonenum =  $.trim(cdphonenum);    
        		if (cdphonenum == "") {   
      				$("span#cdphonenum_error").show();   
      				$("input#cdphonenum").focus();   
      			return false;
	    	}
			
			var cdaddress1 = $("input#cdaddress1").val();
			cdaddress1 =  $.trim(cdaddress1);    
        		if (cdaddress1 == "") {   
      				$("span#cdaddress1_error").show();   
      				$("input#cdaddress1").focus();   
      			return false;
	    	}
			
			// this field is optional
			var cdaddress2 = $("input#cdaddress2").val();
			cdaddress2 =  $.trim(cdaddress2);
			
			var cdcity = $("input#cdcity").val();
			cdcity =  $.trim(cdcity);    
        		if (cdcity == "") {   
      				$("span#cdcity_error").show();   
      				$("input#cdcity").focus();   
      			return false;
	    	}
			
			var cdstatemenu = $("input#cdstatemenu_customselect").val();
			cdstatemenu =  $.trim(cdstatemenu);    
        		if (cdstatemenu == "") {   
      				$("span#cdstatemenu_error").show();   
      				$("input#cdstatemenu_customselect").focus();   
      			return false;
	    	}
			
			var cdzipcode = $("input#cdzipcode").val();
			cdzipcode =  $.trim(cdzipcode);    
        		if (cdzipcode == "") {   
      				$("span#cdzipcode_error").show();   
      				$("input#cdzipcode").focus();   
      			return false;
	    	}
			
			var cdcrossstreet = $("input#cdcrossstreet").val();
			cdcrossstreet =  $.trim(cdcrossstreet);    
        		if (cdcrossstreet == "") {   
      				$("span#cdcrossstreet_error").show();   
      				$("input#cdcrossstreet").focus();   
      			return false;
	    	}
			
			var cdemail = $("input#cdemail").val();
			cdemail =  $.trim(cdemail);    
        		if (cdemail == "") {   
      				$("span#cdemail_error").show();   
      				$("input#cdemail").focus();   
      			return false;
	    	}
			
			var puloc = '';
			var pudesc = '';
			
			
			$("input[type='checkbox']:checked#puloc").each(function(index) {
				puloc += $(this).val() + ", ";
			});
				if (puloc == "") {   
      				$("span#puloc_error").show();   
      				$("input#puloc").focus();   
      			return false;
	    	}
			
			$("input[type='checkbox']:checked#pudesc").each(function(index) {
				pudesc += $(this).val() + ", ";
			});
				if (pudesc == "") {   
      				$("span#pudesc_error").show();   
      				$("input#pudesc").focus();   
      			return false;
	    	}
        	
			var cddetails = $("textarea#cddetails").val();
			cddetails =  $.trim(cddetails);       
        		if (cddetails == "") {   
      				$("span#cddetails_error").show();   
      				$("textarea#cddetails").focus();   
      			return false;
	    	}
			
// submit the form
var dataString = 'cdsendtoaddress='+ cdsendtoaddress + '&cdfirstname=' + cdfirstname + '&cdlastname=' + cdlastname + '&cdphonenum=' + cdphonenum + '&cdaddress1=' + cdaddress1 + '&cdaddress2=' + cdaddress2 + '&cdcity=' + cdcity + '&cdstatemenu=' + cdstatemenu + '&cdzipcode=' + cdzipcode + '&cdcrossstreet=' + cdcrossstreet + '&cdemail=' + cdemail + '&puloc=' + puloc + '&pudesc=' + pudesc + '&cddetails=' + cddetails;
		
	alert(dataString);
				
		$.ajax({   
  			type: "POST",   
  			url: "http://www.clothingdonations.org/wp-content/themes/clothingdonationv1/app-scheduleform/script-scheduleform.php",   
  			data: dataString,
  			success: function() {   
    						$('#scheduleform').html("<div class='cdbluecontactbox-ty'></div>");   
				    		$('.cdbluecontactbox-ty').html("<h2>Thank you</h2>")
    						.append("<p>Your message has been sent.</p>").fadeIn('normal');
							
			}
							
				});   
				return false;
		
		}); 
	});
	
function resetform() {
	$('form')[0].reset();
	alert("form reset");	
}
	
}); // closes the very first tag
