 $(document).ready(function() {
 //$('#quoteFloatContentWrapper').show();
 
 $('input[type="text"]').addClass("idleField");
 	$('input[title]').each(function() {
		if($(this).val() === '') {$(this).val($(this).attr('title'));	
		}

		$(this).focus(function() {
			if($(this).val() == $(this).attr('title')) {
				$(this).val('').addClass('focused');	
				$(this).removeClass("idleField").addClass("focused");
			}
		});
			
		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title')).removeClass('focused').addClass("idleField");	
			}
		});
});

//everything during the click
 $('#quoteFloat').click(function() {
   $('#quoteFloatContentWrapper').slideToggle(400);
	if ($('#quoteMessageClosed').is('.show')) {
		$('#quoteMessageClosed').removeClass("show").addClass("hide");
		$('#quoteMessageOpen').removeClass("hide").addClass("show");
	} else {
		$('#quoteMessageClosed').removeClass("hide").addClass("show");
		$('#quoteMessageOpen').removeClass("show").addClass("hide");
	}
   return false;
 });
});

function validateFormOnSubmit(theForm) {
var reason = '';

  reason += validateEmptyName(document.forms["theContactForm"].name);
  reason += validateEmail(document.forms["theContactForm"].emailAddress);
  reason += validatePhone(document.forms["theContactForm"].phoneNumber);
  reason += validateEmpty(document.forms["theContactForm"].cprojectdetails);
  
  var captchaResult = validateCaptcha();
  if (captchaResult && reason != '') {
	Recaptcha.reload();
	reason += "When fixed, please verify again.";
	document.forms["theContactForm"].recaptcha_response_field.style.background = '#FFFFD2'; 
  } else if (captchaResult != 1){ 
	Recaptcha.reload();
	reason += "Please verify again.";
	document.forms["theContactForm"].recaptcha_response_field.style.background = '#FFFFD2'; 
  }
  if (reason != '') {
    document.getElementById("formError").innerHTML=reason;
    return false;
  } else {
  return true;
  }  
}




function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error='';
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == '') {
        fld.style.background = '#FFFFD2';
        error = "Please provide an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FFFFD2';
        error = "Please provide a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFFFD2';
        error = "Please provide a valid email address.\n";
    } else {
        fld.style.background = '#fff';
    }
    return error;
}

function validatePhone(fld) {
    var error = '';
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == '') {
        error = "Please provide a phone number.\n";
        fld.style.background = '#FFFFD2';
    } else if (isNaN(parseInt(stripped))) {
        error = "Please provide a valid phone number.\n";
        fld.style.background = '#FFFFD2';
    } else if (!(stripped.length == 10)) {
        error = "Please provide a valid phone number. Make sure you included an area code.\n";
        fld.style.background = '#FFFFD2';
    } else {
		fld.style.background = '#fff';
	}
    return error;
}

function validateEmpty(fld) {
    var error = '';
  
    if (fld.value.length == 0) {
        fld.style.background = '#FFFFD2'; 
        error = "Please provide some project details.\n"
    } else {
        fld.style.background = '#fff';
    }
    return error;   
}

function validateEmptyName(fld) {
    var error = '';
  
    if (fld.value.length == 0 || fld.value=="Your Name") {
        fld.style.background = '#FFFFD2'; 
        error = "Please provide your name.\n"
    } else {
        fld.style.background = '#fff';
    }
    return error;   
}

function validateCaptcha() {
	var error = '';
            challengeField = $("input#recaptcha_challenge_field").val();
            responseField = $("input#recaptcha_response_field").val();
            //alert(challengeField);
            //alert(responseField);
            //return false;
            var html = $.ajax({
            type: "POST",
            url: "/includes/scripts/recaptchacheck.php",
            data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
            async: false
            }).responseText;

            if (html.replace(/^\s+|\s+$/, '') == "success")  {
				document.forms["theContactForm"].recaptcha_response_field.style.background = '#fff'; 
				return true;
            } else {
				document.forms["theContactForm"].recaptcha_response_field.style.background = '#FFFFD2'; 
				return false;
            }
			
        };
