// TUI Web Forms JS
// Copyright 2011
// Author: MrZ
$(document).ready(function() {
    GetMyIP(); //--Lets get the IP and string it along :)
	SetReferringPage(); //--Lets set the referring page and string it along :)
	$(".Numeric").keyup(function(){
        this.value = this.value.replace(/[^0-9\.\-\(\)\+]/g,'');
    }); //--END Numeric Field Simple Validation Function
    $(".Alpha").keyup(function(){
        this.value = this.value.replace(/[^A-Z\a-z]/g,'');
    }); //--END Numeric Field Simple Validation Function
    $(".InputBox").focus(function(){
        $(this).css("background-color","#FFFFC0");    
    }); //--END Input Change Color Function
    $(".InputBox").blur(function(){
        $(this).css("background-color","#FFFFFF");    
    }); //--END Input Change Color Function
    $(".Textarea").focus(function(){
        $(this).css("background-color","#FFFFC0");    
    }); //--END Input Change Color Function
    $(".Textarea").blur(function(){
        $(this).css("background-color","#FFFFFF");    
    }); //--END Input Change Color Function
    $("#TheForm").submit(function(e) {
        $("#SubmitButton").click();
        return false;
    }); //--END Form Submit Function
	$("#NewsForm").submit(function(e) {
        $("#NewsButton").click();
        return false;
    }); //--END Form Submit Function
	$("#NewsButton").click(function(){
        $('div #NewsForm input:text').css({ borderColor:"#D0D0D0" }); $('div #NewsForm textarea').css({ borderColor:"#D0D0D0" });
        if (!StartValidation('NewsForm')) { return false; }
        SubmitNewsletter();
    }); //--END NewsButton Click Function
    $("#SubmitButton").click(function(){
        $('div #TheForm input:text').css({ borderColor:"#D0D0D0" }); $('div #TheForm textarea').css({ borderColor:"#D0D0D0" });
        var FormType = $("#SubmitButton").attr("title"); //---Form Type - this helps direct the custom validation
        if (!StartValidation('TheForm')) { return false; }
        //---Custom Saving Code
        if (FormType=="ContactForm" || FormType=="PackageForm") {
            if (SubmitContactForm(FormType)==false) { return false; }
        } else {
            alert("Bad Form Type!");
        }
        //----------------------
        //alert("done!");
    }); //--END SubmitButton Click Function
}); //--END DocumentReady Function
function CheckValue(CID,val) {
    if (val == "") { FieldName=$('#'+CID).attr("title"); $('#'+CID).css({ borderColor:"red" }); NewDialog('<b class="UhOh">Uh-Oh!</b><br> The form field: <b><i>'+FieldName+'</i></b> <br>is empty or missing information!<br><br> Please fill it in to continue.','Form Submission - Error!',CID); return false; }
    if (CID.search(/email/i) != -1) { if (ValidateEmail(val)) { /*GOOD!*/ } else { $('#'+CID).css({ borderColor:"red" }); NewDialog('<b class="UhOh">Uh-Oh!</b><br> A vaild email address is needed!<br><br> Please fill it in to continue.','Form Submission - Error!',CID); return false; } }
} //--END CheckValue Function
function ValidateEmail(str) { 
    var goodEmail = str.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/); if (goodEmail){ return true; } else { return false; }
} //--END ValidateEmail Function
function HighlightField(id){
    $('#'+id).focus(); $('#'+id).css({ borderColor:"red" });
} //--END HighlightField Function
function NewDialog(content,top_title,fid) {
      new Boxy.alert(content, null, {title: top_title, fixed: false, modal:true, afterHide:function(){ setTimeout('HighlightField("'+fid+'")',500); } });
} //--END NewDialog Function
function ValidatePhone(form_id) {
    var l = $('#'+form_id).val().length; // Length of the Phone Number
    if (l<=6) { NewDialog('<b class="UhOh">Uh-Oh!</b><br> The form field: <b><i>Phone Number</i></b> <br> does not meet the minimum length!<br><br> Please correct the phone number.','Form Submission - Error!',form_id); return false; }
} //--END ValidatePhone
function GetMyIP(){
	$.get('http://'+window.location.hostname+'/includes/php/ip.php', function(data) {
	  $('#IpAddress').val(data);
	});
} //--END GetMyIP Function
function SetReferringPage(){
	$('#ReferringPage').val(document.referrer);	
} //--END SetReferringPage Function
function StartValidation(form_id) {
    var all_inputs = $('div #'+form_id+' input:text').get(); var all_texts = $('div #'+form_id+' textarea').get();
    var numInputs = all_inputs.length; var numTexts = all_texts.length;
    if (numInputs <= 0) { NewDialog('<b class="UhOh">Uh-Oh!</b> This form has an error! <br> Please email us at TuiTech@'+window.location.hostname+' - Thanks!','Form Submission - Error!','TheForm'); return false; }
    for (var x = 0; x <= numInputs-1; x++) {
        var id = all_inputs[x].id; var value = $('#'+id).val(); var isRequired = $('#'+id).attr("alt");
        if (isRequired == "required") { if(CheckValue(id,value) == false) { return false; } } else { /*eh skip it*/ }
    }
    if (numTexts >= 1) {
        for (var x = 0; x != numTexts; x++) {
            var id = all_texts[x].id; var value = $('#'+id).val(); var isRequired = $('#'+id).attr("alt");
            if (isRequired == "required") { if (CheckValue(id,value) == false) { return false; } } else { /*eh skip it*/ }
        }
    }
    return true;
} //--END StartValidation Function
function SubmitContactForm(type){
    if($('#ContactName').val() == null) { var contactname = "Empty"; } else { var contactname = $('#ContactName').val(); }
	if($('#Email').val() == null) { var email = "Empty"; } else { var email = $('#Email').val(); }
	if($('#IpAddress').val() == null) { var ip = "Empty"; } else { var ip = $('#IpAddress').val(); }
	if($('#ReferringPage').val() == null) { var ref = "Empty"; } else { var ref = $('#ReferringPage').val(); }
    //---[Custom fields - added 4/27/11 for contact form modification]
	if($('#Language').val() == null) { var language = "Empty"; } else { var language = $('#Language').val(); }
	if($('#Newsletter').val() == null) { var newsletter = "Empty"; } else { var newsletter = $('#Newsletter').val(); }
	if($('#FoundUs').val() == null) { var foundus = "Empty"; } else { var foundus = $('#FoundUs').val(); }
	if($('#Building').val() == null) { var building = "Empty"; } else { var building = $('#Building').val(); }
    //----------------
	if (type=="PackageForm") { 
		if($('#Phone').val() == null) { var phone = ""; } else { var phone = $('#Phone').val(); }
		if($('#Company').val() == null) { var company = "Empty"; } else { var company = $('#Company').val(); }
		if($('#ItemInterest').val() == null) { var itemInterest = "Empty"; } else { var itemInterest = $('#ItemInterest').val(); }
	} else if (type=="NewsLetterForm") {
		
	} else {
		if (ValidatePhone('Phone')==false) { return false; }
		if($('#Phone').val() == null) { var phone = ""; } else { var phone = $('#Phone').val(); }
		if($('#Company').val() == null) { var company = "Empty"; } else { var company = $('#Company').val(); }
		if($('#ItemInterest').val() == null) { var itemInterest = "Empty"; } else { var itemInterest = $('#ItemInterest').val(); }
	}
    if($('#Message').val() == null) { var message = "Empty"; } else { var message = $('#Message').val(); }
    var domain = window.location.hostname;
    $("#SubmitDiv").slideUp("fast"); $("#SubmittingDiv").slideDown("fast");
    $.post("http://"+window.location.hostname+"/includes/php/transfer.php", { Name: contactname, Email: email, Company: company, Phone: phone, Message: message, Domain: domain, FormType: type, ItemInterest:itemInterest, IpAddress:ip, ReferringPage:ref, Language:language, Newsletter:newsletter, FoundUs:foundus, Building:building },
           function(data){
                if (data==1 || data=="1") {
                    $("#SubmittingDiv").slideUp("fast");
                    $("#FullForm").html('<div style=" width:300px; margin:auto; margin-top:20px; margin-bottom:20px; border:solid 1px #009900; background-color:#FFFFCC; padding:15px; text-align:center;"><span style="color:green; font-weight:bold;">Thank you! We will contact you prompty.</span></div>');
                } else {
                    $("#FormErrors").html('An error occured saving the form. Please try again.'); $("#FormErrors").fadeIn("fast");
                    $("#SubmittingDiv").slideUp("fast"); $("#SubmitDiv").slideDown("fast");
                }
                return false;
           }
    ); //--END Post
    return true;
} //--END SubmitContactForm Function
function SubmitNewsletter() {
 	if($('#Email').val() == null) { var email = "Empty"; } else { var email = $('#Email').val(); }
	if($('#IpAddress').val() == null) { var ip = "Empty"; } else { var ip = $('#IpAddress').val(); }
	if($('#ReferringPage').val() == null) { var ref = "Empty"; } else { var ref = $('#ReferringPage').val(); }
 	var domain = window.location.hostname;
	$("#SubmitDiv").slideUp("fast"); $("#SubmittingDiv").slideDown("fast");
	$.post("http://"+window.location.hostname+"/includes/php/transfer.php", { Email: email, Domain: domain, IpAddress:ip, ReferringPage:ref, FormType:'news' },
           function(data){
                if (data==1 || data=="1") {
                    $("#SubmittingDiv").slideUp("fast");
                    $("#FullForm").html('<div style=" width:230px; margin:auto; margin-top:10px; margin-bottom:10px; border:solid 1px #009900; background-color:#FFFFCC; padding:15px; text-align:center;"><span style="color:green; font-weight:bold;">Thanks for Signing Up!</span></div>');
                } else {
                    $("#FormErrors").html('An error occured saving the form. Please try again.'); $("#FormErrors").fadeIn("fast");
                    $("#SubmittingDiv").slideUp("fast"); $("#SubmitDiv").slideDown("fast");
                }
                return false;
           }
    ); //--END Post
} //--END SubmitNewsletter Function
