/* Outfit Core JS */

// makes magic form fields happen
$(document).observe('dom:loaded', function(){

  // Select all textboxes and assign them to an array
  var textboxes = $$('form.contact input.input-text', 'form.contact textarea.input-textarea', 'form.signup input.input-text');

  // Iterate through all textboxes in the form
  textboxes.each(function(input, index){
		
    var label = input.previous();

    // TimedObserver to check for autocomplete	
    if (index == 0){
      new Form.Observer(input.up('form'), 0.1, function(form, value) {
        textboxes.each(function(inputX){
          if (!inputX.value.empty()) {
            inputX.previous().addClassName('hastext');
          }
        });
      });
    }

    // Fade the label back when a field gains focus		
    input.onfocus = function(){
      if (input.value.empty()){
        label.addClassName('focus');            
      }
    }

    // Check if a field is empty when the user switches out
    input.onblur = function(){
      if (input.value.empty()){
        label.removeClassName('focus').removeClassName('hastext');          
      }
    }

    // Fade the label back if a field has text		
    if (!input.value.empty()) {
      label.addClassName('hastext');
    }

    // Fade the label back when the user starts to type		
    input.onkeypress = function(){
      label.addClassName('hastext');
    };
  });
  
  
});

// toggle contact form
function toggleContact() { Effect.toggle('contact', 'blind', { duration: 0.8 }); }


function togglePrivacy() {
	Effect.toggle('privacy', 'appear', { duration: 0.5 });
}

function submitSignup() {

	$('signup_name_first_label').style.borderColor = "#ffffff";
	$('signup_name_last_label').style.borderColor = "#ffffff";
	$('signup_email_label').style.borderColor = "#ffffff";
	$('signupfieldsreq').style.color = "#ffffff";
	$('signupfieldsreq').style.fontWeight = "normal";
	
	var error_count = 0;
	
	if (document.signup.name_first.value == "" || document.signup.name_first.value == "First Name") {
		error_count++;
		$('signup_name_first_label').style.borderColor = "#fcb040";
	}
	if (document.signup.name_last.value == "" || document.signup.name_last.value == "Last Name") {
		error_count++;
		$('signup_name_last_label').style.borderColor = "#fcb040";
	}
	if (!validateEmail(document.signup.email.value)) {
		error_count++;
		$('signup_email_label').style.borderColor = "#fcb040";
	}
	
	if (error_count == 0) {
	
		new Ajax.Request('addSubscriber.php', {
						 method: 'post',
						 parameters: { 
						 	name_first: document.signup.name_first.value,
							name_last: document.signup.name_last.value,
							email: document.signup.email.value						 },
						 onLoading: function() {
						 	$('signupfieldsreq').innerHTML = "Sending...";
						 },
						 onSuccess: function(contactSuccess) {
							if (contactSuccess.responseText == "Success") { 
								Effect.Fade('form', { duration: 0.5, afterFinish: function() { Effect.Appear('thankyou', { duration: 0.5, afterFinish: function() { $('signupfieldsreq').innerHTML = "All fields required"; $('signup_name_first').value = ""; $('signup_name_last').value = ""; $('signup_email').value = ""; $('signup_name_first_label').removeClassName('hastext'); $('signup_name_last_label').removeClassName('hastext'); $('signup_email_label').removeClassName('hastext'); $('signup_name_first_label').removeClassName('focus'); $('signup_name_last_label').removeClassName('focus'); $('signup_email_label').removeClassName('focus'); Effect.Fade('thankyou', { duration: 0.5, delay: 3.0, afterFinish: function() { Effect.Appear('form', { duration: 0.5 }); } }); } });
 } }); 							}
							else {
								alert("Something went wrong.\nPlease email info@outfitevents.com");
								
							}
						 },
						 onFailure: function() { 
						 	alert("Something went wrong.\nPlease email info@outfitevents.com");
						 }
		}); 
		
	
	}
	else {
		$('signupfieldsreq').style.color = "#fcb040";
		$('signupfieldsreq').style.fontWeight = "bold";
	}
}

// validate and send contact forms
function submitContact() {

	// reset
	$('contact_name_first_label').style.borderColor = "#ffffff";
	$('contact_name_last_label').style.borderColor = "#ffffff";
	$('contact_email_label').style.borderColor = "#ffffff";
	$('contact_phone_label').style.borderColor = "#ffffff";
	$('contact_comments_label').style.borderColor = "#ffffff";
	$('fieldsreq').style.color = "#ffffff";
	$('fieldsreq').style.fontWeight = "normal";
	

	var error_count = 0;
	
	if (document.contact.name_first.value == "" || document.contact.name_first.value == 'First Name') {
		error_count++;
		$('contact_name_first_label').style.borderColor = "#fcb040";
	}
	if (document.contact.name_last.value == "" || document.contact.name_last.value == "Last Name") {
		error_count++;
		$('contact_name_last_label').style.borderColor = "#fcb040";
	}
	if (!validateEmail(document.contact.email.value)) {
		error_count++;
		$('contact_email_label').style.borderColor = "#fcb040";
	}
	if (document.contact.phone.value == "" || document.contact.phone.value == "Phone") {
		error_count++;
		$('contact_phone_label').style.borderColor = "#fcb040";
	}
	if (document.contact.comments.value == "" || document.contact.comments.value == "Question or Comments") {
		error_count++;
		$('contact_comments_label').style.borderColor = "#fcb040";
	}
	
	if (error_count == 0) {
	
		
		new Ajax.Request('sendContact.php', {
						 method: 'post',
						 parameters: { 
						 	name_first: document.contact.name_first.value,
							name_last: document.contact.name_last.value,
							email: document.contact.email.value,
							phone: document.contact.phone.value,
							comments: document.contact.comments.value,
							k: document.contact.k.value
						 },
						 onLoading: function() {
						 	$('form1div').innerHTML = "";
						 	$('form2div').innerHTML = "";
						 	$('form3div').innerHTML = "";
						 },
						 onSuccess: function(contactSuccess) {
							if (contactSuccess.responseText == "1") { 
								$('form1div').innerHTML = '<p class="contactsent1">Your question or comment has been sent.</p><p class="contactsent2">An Outfit representative will respond within 24 hours.</p>';
								$('form1div').style.width = "500px";
								$('form1div').style.margin = "56px 0 0 45px";
								$('form1div').style.height = "98px";
								$('form2div').innerHTML = "";
								$('form2div').style.width = "1px";
								$('form3div').innerHTML = "";
								$('form3div').style.width = "1px";
							}
							else {
								$('form1div').innerHTML = '<p class="contactsent1">Something went wrong.</p><p class="contactsent2">Please email your comment or question to info@outfitevents.com</p>';
								$('form1div').style.width = "500px";
								$('form1div').style.margin = "56px 0 0 45px";
								$('form1div').style.height = "98px";
								$('form2div').innerHTML = "";
								$('form2div').style.width = "1px";
								$('form3div').innerHTML = "";
								$('form3div').style.width = "1px";
								
							}
						 },
						 onFailure: function() { 
						 	$('form1div').innerHTML = '<p class="contactsent1">Something went wrong.</p><p class="contactsent2">Please email your comment or question to info@outfitevents.com</p>';
							$('form1div').style.width = "500px";
							$('form1div').style.margin = "56px 0 0 45px";
							$('form1div').style.height = "98px";
							$('form2div').innerHTML = "";
							$('form2div').style.width = "1px";
							$('form3div').innerHTML = "";
							$('form3div').style.width = "1px";								
						 }
		}); 
		
		
		
		
	}
	else {
		$('fieldsreq').style.color = "#fcb040";
		$('fieldsreq').style.fontWeight = "bold";
	}	
}


// email validator function
function validateEmail(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   	if (!reg.test(email)) {
    	return false;
   	}
   	else {
   		return true;
   	}
}

