/* Author:  AWC

*/


/* Intialize the jQuery Date Picker */
var initDatepicker = function() {  
    $('input[type=date]').each(function() {  
        var $input = $(this);  
        $input.datepicker({  
            minDate: $input.attr('min'),  
            maxDate: $input.attr('max'),  
            dateFormat: 'mm-dd-yy',
            yearRange: '1900:2100',
 		    changeMonth: true,
			changeYear: true
        });  
    });  
};  
if(!Modernizr.inputtypes.date){  
    $(document).ready(initDatepicker);  
};
/* Intialize the HTML5 Placeholder for all browsers for webforms2 */
var initPlaceholder = function() {  
    $('input[placeholder]').placehold("placeholder");  
};    
if(!Modernizr.input.placeholder){  
    $(document).ready(initPlaceholder);  
};  

$(document).ready(function(){
	
	
	/* Homepage Callouts, Equalize the heights */
	$('#residential-callouts .callout-content,#business-callouts .callout-content ,#wholesale-callouts .callout-content').equalHeightColumns();
	
	/* General use for form elements for all browsers for webforms2 */
	$( "input:submit, button" ).button(); // Converts to jQuery UI Button with custom theme
	//$( "#status" ).buttonset(); // Converts to jQuery UI Checkboxes with custom theme
	
	$( "#account_number").mask("99999999999",{placeholder:" "});
	//$(" #phone_number, #fax" ).mask("(999) 999-9999",{placeholder:" "}); // feature declined by clients
	//$( "#postal_code,#billing_postal_code").mask("99999 ? - 9999",{placeholder:" "}); // feature declined by client
	$( "#drivers_license").mask("9999999999",{placeholder:" "});
	//$(" #account_phone_number, #mobile_line, #land_line, #fax_line" ).mask("(441) 999-9999",{placeholder:" "});
	
	$('.validateEmail').blur(function() {
		if($(".validateEmail:eq(0)").val() && $(".validateEmail:eq(1)").val() && $(".validateEmail:eq(0)").val() != $(".validateEmail:eq(1)").val()) {
			$(this).val('');
			alert('Your emails do not match!');
		} 
	});
	
	/* Toggle the Account Information for New vs Exsiting Users */
	$('.customer-type').live('change',function(){
		var selectedVal = $('select.customer-type option:selected').val(); 
		
		if(selectedVal == 'New Customer') {
			
			$('.hide-section').show();
			$('.hide-customer-section').hide();
			$('#account_phone_number,#account_number').removeClass('required');
		
		} else if(selectedVal == 'Exsiting Customer') {
			$('.hide-customer-section').show();
			$('.hide-section').show();
			$('#account_phone_number,#account_number').addClass('required');
		
		} else if(selectedVal == '') {
			$('.hide-customer-section').hide();
			$('.hide-section').hide();
		}
	});
	
	
	/* FAQ */
	$('.faq-select dt a').live('click',function(){
				
		$(this).closest('dt').next().slideToggle();
		
		return false;
	});

	/* modify phone number fields if infinity plus */
	if ($('#addLines').length > 0) {
		var multiPhoneLineHtml = $('#addLines').html().toString();
	}
	$("input.plan").change(function(){
		if ($(this).val() == 'Infinity Plus') {
			
			$('#addLines').html(
							'<label for="mobile_line">Line 1:</label><input id="mobile_line" class="text med bermudaPhoneNumber" style="margin-bottom: 5px;" name="mobile_line[]" type="text"><br/>'
						 +'<label for="mobile_line">Line 2:</label><input id="mobile_line" class="text med bermudaPhoneNumber" name="mobile_line[]" type="text">'
													);
			// now remove any additional lines still left
			$('.extraneous').remove();
		} else {
			$('#addLines').html(multiPhoneLineHtml);
		}
	});
	
	/* Add Another Field for Phone Numbers */
	
	$(".addAnother").live('click',function() {
		
		var parentContainer = $(this).parent();
		var divContainer = $(parentContainer).parent();
		var newElement = '';
		
		if($(divContainer).children(".row").size() < 10) {
			
			newElement = $(parentContainer).clone();
			newElement.find('a.removeInput').remove();
			newElement.addClass('extraneous');
			newElement.append(' <a class="removeInput" href="javascript:;"> Remove</a>');
			$(divContainer).append(newElement);
			$(divContainer).children(".row:last").children("input").val('');
			
		} else {
			alert('You have reached your limit of 10');
		}
		
		//$(".bermudaPhoneNumber").mask("(441) 999-9999",{placeholder:" "}); // feature declined by the client
	});
	
	$('.removeInput').live('click', function() {
		$(this).parent().remove();
	});
	
	// bring up security code details
	$('.securityCardHuh').click(function() {
		$('.explainSecurityCode').toggle();
	});
	
	/* Billing same as Street Address */
	$("#billing_sameas").click(function() {
		// quick function to remove invalid classes from validation
		var billingFields = $("#billing_street_address, #billing_street_address_2, #billing_parish, #billing_postal_code");
		function removeValidation() {
			billingFields.removeClass('invalid').addClass('success');
			billingFields.each(function() {
				$("label[for=" + $(this).attr('name') + "]").removeClass('invalid').addClass('success');
				$("em[htmlfor="+ $(this).attr('name') + "]").remove();
			});
		}
		
		// is this checked already
		if ($(this).attr('checked')) {
			$("#billing_street_address").val($("#street_address").val());
			$("#billing_street_address_2").val($("#street_address_2").val());
			$("#billing_parish").val($("#street_parish").val());
			$("#billing_postal_code").val($("#postal_code").val());
			removeValidation();
		} else {
			billingFields.val('');
			removeValidation();
		}
		
	});
	/* Sign-Up Form Validation */
	$('#sign-up').validate({
                    highlight: function(element, errorClass, validClass) {
                        $(element).addClass(errorClass).removeClass(validClass);
                        $(element.form).find("label[for=" + element.name + "]").addClass(errorClass);
                    },
                    unhighlight: function(element, errorClass, validClass) {
                        $(element).removeClass(errorClass).addClass(validClass);
                        $(element.form).find("label[for=" + element.name + "]")
                        .removeClass(errorClass).addClass(validClass);
                    },
                    errorElement:"em",
                    errorClass: "invalid",
                    validClass: "success"
                });
    
   /* Contact Form Validation */
	$('#contact-us-form').validate({
                    highlight: function(element, errorClass, validClass) {
                        $(element).addClass(errorClass).removeClass(validClass);
                        $(element.form).find("label[for=" + element.name + "]").addClass(errorClass);
                    },
                    unhighlight: function(element, errorClass, validClass) {
                        $(element).removeClass(errorClass).addClass(validClass);
                        $(element.form).find("label[for=" + element.name + "]")
                        .removeClass(errorClass).addClass(validClass);
                    },
                    
                    errorElement:"em",
                    errorClass: "invalid",
                    validClass: "success"
                });
                
    /* Contact Form Validation */
	$('#contact-form').validate({
                    highlight: function(element, errorClass, validClass) {
                        $(element).addClass(errorClass).removeClass(validClass);
                        $(element.form).find("label[for=" + element.name + "]").addClass(errorClass);
                    },
                    unhighlight: function(element, errorClass, validClass) {
                        $(element).removeClass(errorClass).addClass(validClass);
                        $(element.form).find("label[for=" + element.name + "]")
                        .removeClass(errorClass).addClass(validClass);
                    },
                    
                    errorElement:"em",
                    errorClass: "invalid",
                    validClass: "success"
                });

	
	
	/* Contact Form*/
	$(".form-header").click(function() {
		$(this).next(".form-content").slideToggle(500);
	});
	
	$(".residentialCheckbox").click(function() {
		if($(this).attr('checked')) {
			$("#company").removeAttr('required');
		} else {
			$("#company").attr('required', true);
		}
	});
	
	/* Twitter Callout */
	/* $.socialFader({ tweetHolder:"#twitter", tweetName:"TeleBermuda", tweetCount: 1 }); */
	
	
	function makeLink(text) // this REGEX converts http(s) links that are embedded in the tweet text into real hyperlinks.
	{  
		var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
		return text.replace(exp,"<a href='$1'>$1</a>"); 

	}
	
	/* Validates and Email Address */
	function isEmail (s) {
		var isEmail_re       = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
		return String(s).search (isEmail_re) != -1;
	}
	/* Validates Checkboxes */
	function validateCheckboxes(className) {
		var validCB = false;		
		$("."+className).each(function() {
			if($(this).attr("checked")) {
				validCB = true;
			}
		});	
		
		return validCB;
	}
	
	/* Homepage Billboard Rotator */
	$('.slides').cycle({ cleartypeNoBg: true, cleartype:true, fx:'fade', prev:   '.nav-left',  next:   '.nav-right', timeout: 5000 });
	
	
	/*Modals*/
	$('.pop-up').colorbox({inline:true});
	
	/* Data Centre Video */
	$('.pop-video').colorbox({iframe:true, innerWidth:460, innerHeight:360});
});




























