if ("undefined" === typeof window.console) { window.console = { "log": function () {} } }

jQuery ( function () {
	formValidate ();
	initContactForm ();
	initNewContactForm ();
})
// form validator
function formValidate () {
	var errorClass = 'error',
		regEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
		regPhone = /^[0-9\-\ \()]+$/,
		//reZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
		reZip = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
	
	jQuery ( 'form.validate' ).each ( function () {
		var form = jQuery ( this );
		
		function checkFields () {
			var flag = false,
				requiredFields = jQuery ( 'input.required, textarea.required', form ),
				requiredSelect = jQuery ( 'select.required', form ),
				requiredFieldsEmail = jQuery ( 'input.required-email', form ),
				requiredFieldsPhone = jQuery ( 'input.required-phone', form ),
				requiredFieldsZip = jQuery ( 'input.required-zip', form );
			
			requiredFields.each ( function () {
				jQuery ( this ).parents ( '.field-box:eq(0)' ).removeClass ( errorClass );
				if ( this.value == '' || this.value == this.defaultValue ) addError ( this );
			})
			
			requiredSelect.each ( function () {
				jQuery ( this ).parents ( '.field-box:eq(0)' ).removeClass ( errorClass );
				if ( this.selectedIndex == 0 ) addError ( this );
			})
			
			requiredFieldsEmail.each ( function () {
				jQuery ( this ).parents ( '.field-box:eq(0)' ).removeClass ( errorClass );
				if ( !regEmail.test ( this.value ) ) addError ( this );
			})
			
			requiredFieldsPhone.each ( function () {
				jQuery ( this ).parents ( '.field-box:eq(0)' ).removeClass ( errorClass );
				if ( !regPhone.test ( this.value ) ) addError ( this );
			})
			
			requiredFieldsZip.each ( function () {
				jQuery ( this ).parents ( '.field-box:eq(0)' ).removeClass ( errorClass );
				if ( !reZip.test ( this.value ) ) addError ( this );
			})
			
			function addError ( o ) {
				jQuery ( o ).parents ( '.field-box:eq(0)' ).addClass ( errorClass );
				flag = true;
			}
			
			if(flag == true) { alert ( 'You must fill in all required fields correctly to continue. Required fields are marked with a *' ); }
			return flag;
		}
		
		form.submit ( function () {
			if ( checkFields () ) return false;
		})
	})
}

function checkFields_go_button (form) {
	var errorClass = 'error',
		regEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
		regPhone = /^[0-9\-\ \()]+$/,
		//reZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
		reZip = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;

	var flag = false,
		requiredFields = jQuery ( 'input.required, textarea.required', form ),
		requiredSelect = jQuery ( 'select.required', form ),
		requiredFieldsEmail = jQuery ( 'input.required-email', form ),
		requiredFieldsPhone = jQuery ( 'input.required-phone', form ),
		confirmEmail = jQuery ( '#confirm-email', form ),
		postcode1 = jQuery ( '#postcode1', form ),
		email = jQuery ( '#email', form );

console.log("There are " + postcode1.length + " postcode1 items");
		
	requiredFields.each ( function () {
		if ( jQuery ( this ).is ( ':hidden' ) ) return;
		jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
		/*if ( this.value == '' || this.value == this.defaultValue ) addError ( this );*/
		if ( this.value == '' ) addError ( this );
	})
	
	requiredSelect.each ( function () {
		if ( jQuery ( this ).is ( ':hidden' ) );
		jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
		if ( this.selectedIndex == 0 ) addError ( this );
	})
	
	requiredFieldsEmail.each ( function () {
		if ( jQuery ( this ).is ( ':hidden' ) ) return;
		jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
		if ( !regEmail.test ( this.value ) ) addError ( this );
	})
	
	requiredFieldsPhone.each ( function () {
		if ( jQuery ( this ).is ( ':hidden' ) ) return;
		jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
		if ( !regPhone.test ( this.value ) ) addError ( this );
	})
	
	confirmEmail.each ( function () {
		if ( jQuery ( this ).is ( ':hidden' ) ) return;
		jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
		if ( this.value !== email.val () || this.value == '' ) addError ( this );
	})
	
	postcode1.each ( function () {
console.log("Checking Postcode1");
		if ( jQuery ( this ).is ( ':hidden' ) ) return;
console.log("   Postcode1 not hidden");

		jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
console.log("   Postcode1 has " + jQuery ( this ).parents ( '.row:eq(0)' ).length + " parent(s)");

		if ( this.value === "Please enter your postcode" || this.value == '' ) addError ( this );
console.log("   Postcode1 value =  " + this.value);
	})
	
	function addError ( o ) {
		jQuery ( o ).parents ( '.row:eq(0)' ).addClass ( errorClass );
		flag = true;
	}
	
	if(flag == true) { alert ( 'You must fill in all required fields correctly to continue. Required fields are marked with a *' ); }
	return flag;
}


// init Contact Form
function initContactForm () {
	var errorClass = 'error',
		regEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
		regPhone = /^[0-9\-\ \()]+$/,
		//reZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
		reZip = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
		
	jQuery ( '.contact-form' ).each ( function () {
		var form = jQuery ( this ),
			addBoxAddress = jQuery ( '#add-box-address', this ),
			addBox = jQuery ( '#add-box', this ),
			chooseAddress = jQuery ( '#choose-address', this ),
			otherAddress = jQuery ( '#other-address', this ),
			btnAddAddress = jQuery ( '#btn-add-address', this ),
			postcode1 = jQuery ( '#postcode1', this ),
			selectYourAddress = jQuery ( '#your-address', this );
		
		chooseAddress.hide ();
		otherAddress.hide ();
		addBoxAddress.hide ();
		
		btnAddAddress.click ( function () {
			var codeField = postcode1.get ( 0 );
			chooseAddress.hide ();
			otherAddress.hide ();
			addBoxAddress.hide ();
			if ( codeField.value !== '' && codeField.value !== "Please enter your postcode") {
				selectYourAddress.parent().show();
				chooseAddress.fadeIn ();
			}
		})
		
		var select = selectYourAddress.get(0);
		select.onchange = function () {
			if ( this.selectedIndex > 0 && this.selectedIndex < this.options.length - 1 ) addBoxAddress.fadeIn ();
			else if ( this.selectedIndex > 0 ) {
				selectYourAddress.parent().hide ();
				otherAddress.fadeIn ();
				addBoxAddress.fadeIn ();
			}
		}
		
		function checkFields () {
			var flag = false,
				requiredFields = jQuery ( 'input.required, textarea.required', form ),
				requiredSelect = jQuery ( 'select.required', form ),
				requiredFieldsEmail = jQuery ( 'input.required-email', form ),
				requiredFieldsPhone = jQuery ( 'input.required-phone', form ),
				requiredFieldsZip = jQuery ( 'input.required-zip', form ),
				confirmEmail = jQuery ( '#confirm-email', form ),
				email = jQuery ( '#email', form );
			
			requiredFields.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) ) return;
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				/*if ( this.value == '' || this.value == this.defaultValue ) addError ( this );*/
				if ( this.value == '' ) addError ( this );
			})
			
			requiredSelect.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) );
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				if ( this.selectedIndex == 0 ) addError ( this );
			})
			
			requiredFieldsEmail.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) ) return;
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				if ( !regEmail.test ( this.value ) ) addError ( this );
			})
			
			requiredFieldsPhone.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) ) return;
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				if ( !regPhone.test ( this.value ) ) addError ( this );
			})
			
			requiredFieldsZip.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) ) return;
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				if ( !reZip.test ( this.value ) ) addError ( this );
			})
			
			confirmEmail.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) ) return;
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				if ( this.value !== email.val () || this.value == '' ) addError ( this );
			})
			
			function addError ( o ) {
				jQuery ( o ).parents ( '.row:eq(0)' ).addClass ( errorClass );
				flag = true;
			}
			
			if(flag == true) { alert ( 'You must fill in all required fields correctly to continue. Required fields are marked with a *' ); }
			return flag;
		}
		
		// row-postcode
		
		form.submit ( function () {
			if ( checkFields () ) return false;
		})
	})
}

// init NEW Contact Form
function initNewContactForm () {
	var errorClass = 'error',
		regEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
		regPhone = /^[0-9\-\ \()]+$/,
		//reZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
		reZip = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
		
	jQuery ( '.new-contact-form' ).each ( function () {
		var form = jQuery ( this ),
			type1 = jQuery ( '#type', this ),
			type2 = jQuery ( '.conformtype2', this ),
			contact1 = jQuery ( '#contact1', this ),
			contact2 = jQuery ( '#contact2', this );
		
		contact1.hide ();
		contact2.hide ();
		
		var selectType1 = type1.get(0);
		var selectType2 = type2.get(0);
		if (selectType1) {
			selectType1.onchange = function () {
				if ( this.selectedIndex > 0) contact1.fadeIn ()
			}
		}
		if (selectType2) {
			selectType2.onchange = function () {
				if ( this.selectedIndex > 0) contact2.fadeIn ()
			}
		}
		
		function checkFields () {
			var flag = false,
				requiredFields = jQuery ( 'input.required, textarea.required', form ),
				requiredSelect = jQuery ( 'select.required', form ),
				requiredFieldsEmail = jQuery ( 'input.required-email', form ),
				requiredFieldsPhone = jQuery ( 'input.required-phone', form ),
				requiredFieldsZip = jQuery ( 'input.required-zip', form ),
				confirmEmail = jQuery ( '#confirm-email', form ),
				email = jQuery ( '#email', form );
			
			requiredFields.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) ) return;
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				if ( this.value == '' || this.value == this.defaultValue ) addError ( this );
			})
			
			requiredSelect.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) );
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				if ( this.selectedIndex == 0 ) addError ( this );
			})
			
			requiredFieldsEmail.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) ) return;
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				if ( !regEmail.test ( this.value ) ) addError ( this );
			})
			
			requiredFieldsPhone.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) ) return;
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				if ( !regPhone.test ( this.value ) ) addError ( this );
			})
			
			requiredFieldsZip.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) ) return;
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				if ( !reZip.test ( this.value ) ) addError ( this );
			})
			
			confirmEmail.each ( function () {
				if ( jQuery ( this ).is ( ':hidden' ) ) return;
				jQuery ( this ).parents ( '.row:eq(0)' ).removeClass ( errorClass );
				if ( this.value !== email.val () || this.value == '' ) addError ( this );
			})
			
			function addError ( o ) {
				jQuery ( o ).parents ( '.row:eq(0)' ).addClass ( errorClass );
				flag = true;
			}
			
			if(flag == true) { alert ( 'You must fill in all required fields correctly to continue. Required fields are marked with a *' ); }
			return flag;
		}
				
		form.submit ( function () {
			if ( checkFields () ) return false;
		})
	})
}
// init page progressive enhancement...
function initPage() {
	
	// nav is hidden, custom font loaded, now to rescale/pad out the nav
	initAutoScalingNav({
		menuId: "nav",
		sideClasses: true,
		minPaddings: 10
	});
	
	// fade back in the nav
	$("#nav li a").animate({opacity:1});
	
	// fields clearer
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
// auto-scaling nav function
function initAutoScalingNav(o) {
	if (!o.menuId) o.menuId = "nav";
	if (!o.tag) o.tag = "a";
	if (!o.spacing) o.spacing = 0;
	if (!o.constant) o.constant = 0;
	if (!o.minPaddings) o.minPaddings = 0;
	if (!o.liHovering) o.liHovering = false;
	if (!o.sideClasses) o.sideClasses = false;
	if (!o.equalLinks) o.equalLinks = false;
	if (!o.flexible) o.flexible = false;
	var nav = document.getElementById(o.menuId);
	if(nav) {
		nav.className += " scaling-active";
		var lis = nav.getElementsByTagName("li");
		var asFl = [];
		var lisFl = [];
		var width = 0;
		for (var i=0, j=0; i<lis.length; i++) {
			if(lis[i].parentNode == nav) {
				var t = lis[i].getElementsByTagName(o.tag).item(0);
				asFl.push(t);
				asFl[j++].width = t.offsetWidth;
				lisFl.push(lis[i]);
				if(width < t.offsetWidth) width = t.offsetWidth;
			}
			if(o.liHovering) {
				lis[i].onmouseover = function() {
					this.className += " hover";
				}
				lis[i].onmouseout = function() {
					this.className = this.className.replace("hover", "");
				}
			}
		}
		var menuWidth = nav.clientWidth - asFl.length*o.spacing - o.constant;
		if(o.equalLinks && width * asFl.length < menuWidth) {
			for (var i=0; i<asFl.length; i++) {
				asFl[i].width = width;
			}
		}
		width = getItemsWidth(asFl);
		if(width < menuWidth) {
			var version = navigator.userAgent.toLowerCase();
			for (var i=0; getItemsWidth(asFl) < menuWidth; i++) {
				asFl[i].width++;
				if(!o.flexible) {
					asFl[i].style.width = asFl[i].width + "px";
				}
				if(i >= asFl.length-1) i=-1;
			}
			if(o.flexible) {
				for (var i=0; i<asFl.length; i++) {
					width = (asFl[i].width - o.spacing - o.constant/asFl.length)/menuWidth*100;
					if(i != asFl.length-1) {
						lisFl[i].style.width = width + "%";
					}
					else {
						if(navigator.appName.indexOf("Microsoft Internet Explorer") == -1 || version.indexOf("msie 8") != -1 || version.indexOf("msie 9") != -1)
							lisFl[i].style.width = width + "%";
					}
				}
			}
		}
		else if(o.minPaddings > 0) {
			for (var i=0; i<asFl.length; i++) {
				asFl[i].style.paddingLeft = o.minPaddings + "px";
				asFl[i].style.paddingRight = o.minPaddings + "px";
			}
		}
		if(o.sideClasses) {
			lisFl[0].className += " first-child";
			lisFl[0].getElementsByTagName(o.tag).item(0).className += " first-child-a";
			lisFl[lisFl.length-1].className += " last-child";
			lisFl[lisFl.length-1].getElementsByTagName(o.tag).item(0).className += " last-child-a";
		}
		nav.className += " scaling-ready";
	}
	function getItemsWidth(a) {
		var w = 0;
		for(var q=0; q<a.length; q++) {
			w += a[q].width;
		}
		return w;
	}
}
// fields clearer function
function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass) == -1) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
// run the init page function on page load
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);

// legacy city link app code that interrupts with all other forms - disabled for now - from common.js
/*
$.fn.extend({
    hintText: function(text)
    {
        this.data('hintText', text);
        if(this.val() == '' || this.val() == text) {
            this.val(text);
            this.addClass('input-hint');
        }
        this.focus(function(){
            if($(this).val() == text) {
                $(this).val('');
                $(this).removeClass('input-hint');
            }
        });
        this.blur(function(){
            if($(this).val() == '') {
                $(this).val(text);
                $(this).addClass('input-hint');
            }
        });
        this.keypress(function(){
            $(this).removeClass('input-warning');
        });
		form = this.parents('form');
        if(form.data('hintText') == undefined) {
            form.submit(function(){
                ok = true;
                inputs = $(this).find('input');
                inputs.each(function(){
                    if($(this).data('hintText') != undefined) {
                        if($(this).val() == '' || $(this).val() == $(this).data('hintText')) {
                            $(this).addClass('input-warning');
                            ok = false;
                        }
                    }
                });
                return ok;
            });
            form.data('hintText', 'function added');
        }
    }
});
*/	
	

// fancy tooltips code (combined into here instead of seperate JS file
(function($) {
    $.fn.tipsy = function(options) {

        options = $.extend({}, $.fn.tipsy.defaults, options);
        
        return this.each(function() {
            
            var opts = $.fn.tipsy.elementOptions(this, options);
            
            $(this).hover(function() {

                $.data(this, 'cancel.tipsy', true);

                var tip = $.data(this, 'active.tipsy');
                if (!tip) {
                    tip = $('<div class="tipsy"><div class="tipsy-inner"/></div>');
                    tip.css({position: 'absolute', zIndex: 100000});
                    $.data(this, 'active.tipsy', tip);
                }

                if ($(this).attr('title') || typeof($(this).attr('original-title')) != 'string') {
                    $(this).attr('original-title', $(this).attr('title') || '').removeAttr('title');
                }

                var title;
                if (typeof opts.title == 'string') {
                    title = $(this).attr(opts.title == 'title' ? 'original-title' : opts.title);
                } else if (typeof opts.title == 'function') {
                    title = opts.title.call(this);
                }

                tip.find('.tipsy-inner')[opts.html ? 'html' : 'text'](title || opts.fallback);

                var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
                tip.get(0).className = 'tipsy'; // reset classname in case of dynamic gravity
                tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
                var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
                var gravity = (typeof opts.gravity == 'function') ? opts.gravity.call(this) : opts.gravity;

                switch (gravity.charAt(0)) {
                    case 'n':
                        tip.css({top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-north');
                        break;
                    case 's':
                        tip.css({top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-south');
                        break;
                    case 'e':
                        tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}).addClass('tipsy-east');
                        break;
                    case 'w':
                        tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}).addClass('tipsy-west');
                        break;
                }

                if (opts.fade) {
                    tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 0.8});
                } else {
                    tip.css({visibility: 'visible'});
                }

            }, function() {
                $.data(this, 'cancel.tipsy', false);
                var self = this;
                setTimeout(function() {
                    if ($.data(this, 'cancel.tipsy')) return;
                    var tip = $.data(self, 'active.tipsy');
                    if (opts.fade) {
                        tip.stop().fadeOut(function() { $(this).remove(); });
                    } else {
                        tip.remove();
                    }
                }, 100);

            });
            
        });
        
    };
    
    // Overwrite this method to provide options on a per-element basis.
    // For example, you could store the gravity in a 'tipsy-gravity' attribute:
    // return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' });
    // (remember - do not modify 'options' in place!)
    $.fn.tipsy.elementOptions = function(ele, options) {
        return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
    };
    
    $.fn.tipsy.defaults = {
        fade: false,
        fallback: '',
        gravity: 'n',
        html: false,
        title: 'title'
    };
    
    $.fn.tipsy.autoNS = function() {
        return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
    };
    
    $.fn.tipsy.autoWE = function() {
        return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
    };
    
})(jQuery);


// once dom is ready, fire following code
$(document).ready(function() {
						   
	// Sidebar consignment form validator
	$('.form-consignment').submit(function() {
		if ($('#parcel_ref_num').val() != "" &&  $('#parcel_ref_num').val() != "Consignment No./ Reference No.") {
			return true;
		}
		alert("You must enter a consignment number in the field to continue");
		$('#parcel_ref_num').css("color","#ff0000");
		return false;
	});
	
	// track another consignment form validator
	$('.form-consignment-tracking').submit(function() {
		if ($('#parcel_ref_num').val() != "" &&  $('#parcel_ref_num').val() != "Consignment No./Ref No.") {
			return true;
		}
		alert("You must enter a consignment number in the field to continue");
		$('#parcel_ref_num').css("color","#ff0000");
		return false;
	});
	
	// legacy js from common.js - commented out as not in use
	//$('#squery').hintText('Search our site');
    //$('#parcel_ref_num').hintText('Consignment No./Ref No.');
	//$('#postcode.track_num').hintText('Delivery Postcode');
	//$('#postcode.data-input').hintText('Delivery Postcode');
	//$('#postcode_directions.data-input').hintText('Please Enter Your Postcode...');
    //$('#custno').hintText('City Link Account number...');
	
	// init tooltips for help and support section
	$('#helpsupp-trackparcel').tipsy();
	$('#helpsupp-rescheduledeliv').tipsy();
	$('#helpsupp-collectparcel').tipsy();
	
});


// legacy city link app code - from common.js
// Define Namespace
var CITYLINK = {};
CITYLINK.Common = {

    validatePostcode : function(postcode)
    {
        var rgxPostcode = new RegExp("^[A-Za-z]{1,2}[0-9]{1,2}[A-Za-z]? ?[0-9]{1}[A-Za-z]{2}$","i");
        if (postcode.match(rgxPostcode) == null) {
            return false;
        }
        return true;
    }

}


