/* 
	A Westfield Production
	Westfield Digital Business
	Copyright 2008
	Author agus daud - agusdaud@gmail.com 	*/
/*
function checkAllCentres()
{
	var checkbox_value	= "";
	var centres_length	= document.registrationForm.centres.length;
	var centres			= document.registrationForm.centres;
	for ( var i=0; i < centres_length; i++ )
	{
		checkbox_value = centres[i];
		checkbox_value.checked = true;
	}
}

function uncheckAllCentres()
{
	var checkbox_value	= "";
	var centres_length	= document.registrationForm.centres.length;
	var centres			= document.registrationForm.centres;
	for ( var i=0; i < centres_length; i++ )
	{
		checkbox_value = centres[i];
		checkbox_value.checked = false;
	}
}
*/

/* COOKIES */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function selectDisplayState(obj) {
		
	var className = $(obj).attr("class");
	$("ul.state-news li a").removeClass("selected");
	$(obj).find("a").addClass("selected");
	$("dl.region-list").hide();
	$("dl#region-"+className).fadeIn(500);
	
}

function parseXML( xml ) {
	if( window.ActiveXObject && window.GetObject ) {
		var dom = new ActiveXObject('Microsoft.XMLDOM');
		dom.loadXML( xml );
		return dom;
	}
	if( window.DOMParser )
		return new DOMParser().parseFromString( xml, 'text/xml' );
	throw new Error( 'No XML parser available' );
} 

function showResponse(responseText, statusText)  { 
    
	// for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 	
	$(".ajax-loader").hide();
	if (responseText == '\n<span class="captcha-error">The word is incorrect, Please try again</span>\n\n') {
		//Recaptcha.reload();
		$('.captcha-container .error').html(responseText);
	} else {
		$("#supplier-enquiry").css({visibility: "hidden"});
	}
} 

jQuery(document).ready( function() {

	/* Westfield Account Online Registration Form */
	var checkAllBox = 'false';
	$('#select-all-centres').click(function() {
		if (checkAllBox == 'false') {
			$('#registrationForm input[@name=centres]').attr('checked','checked');
			checkAllBox = 'true';
		} else {
			$('#registrationForm input[@name=centres]').attr('checked','');
			checkAllBox = 'false';
		}
	});
	//, function() {
	//	
	//});

	 /* Supplier Directory */
	 
	 $.tablesorter.defaults.widgets = ['zebra']; 
//	 $("table.table-supplier").tablesorter( {sortList: [[0,0]]} );
	 $("table.table-supplier").tablesorter( {} );
	 
	 /* News and Events */
	 
	 //initial selected state on news and events page
	 if (readCookie("statenews")) {
		selectDisplayState($("ul.state-news li."+readCookie("statenews")));
	 } else {
		selectDisplayState($("ul.state-news li.nsw"));
	 }
	 
	 $("ul.state-news li").click(function() {
		
		createCookie("statenews",$(this).attr("class"), 7);
		selectDisplayState(this);
		return false;
		
	 });
	 
	 $("dl.region-list ul.list-arrow li").click( function() {
		
		var centreId = $(this).find("a").attr("id");
		
		$("dl.region-list ul.list-arrow li").removeClass("selected");
		$(this).addClass("selected");
		$("dl.centre-news-events").hide();
		//$("dl#"+centreId+"-news").fadeIn(500);
		$("dl#"+centreId+"-news").show();
		
		return false;
		
	 });

	/* T &amp; C Popup */
	 $(".terms-popup .close-popup").click( function() {
		$(".terms-popup").fadeOut(700);
		return false;
	 });
	 
	 $("a.terms-and-condition").click( function() {
		$(".terms-popup").fadeIn(700);
		return false;
	 });

	/* IPP iFrame and Popup */
	var href = '';
	$("#westfieldGlobalNavigationCountryLink a").click( function() {
		href = $(this).attr("href");
	});
	$("#westfieldGlobalNavigationContent a").click( function() {
		href = $(this).attr("href");
	});
	$("#westfieldGlobalNavigationCorporate a").click( function() {
		href = $(this).attr("href");
	});
	$(".left-header h2 a").click( function() {
		href = $(this).attr("href");
	});
	$("ul.primary-nav li a").click( function() {
		href = $(this).attr("href");
	});
	$("ul.primary-right-nav li a").click( function() {
		href = $(this).attr("href");
	});
	$("ul.footer-nav li a").click( function() {
		href = $(this).attr("href");
	});
	
	if ( $("#ipp-frame").length > 0 ) {
		$("#westfieldGlobalNavigationCountryLink a").addClass("ipp");
		$("#westfieldGlobalNavigationContent a").addClass("ipp");
		$("#westfieldGlobalNavigationCorporate a").addClass("ipp");
		$(".left-header a").addClass("ipp");
		$("ul.primary-nav li a").addClass("ipp");
		$("ul.primary-right-nav li a").addClass("ipp");
		$("ul.footer-nav li a").addClass("ipp");
	}
	
	 $(".ipp-popup .close-popup").click( function() {
		$(".ipp-popup").fadeOut(700);
		return false;
	 });
	 $(".ipp-popup #no-button").click( function() {
		$(".ipp-popup").fadeOut(700);
		return false;
	 });
	 $(".ipp-popup #yes-button").click( function() {
		window.location=href;
		return false;
	 });
	 $("a.ipp").click( function() {
		$(".ipp-popup").fadeIn(700);
		return false;
	 });

	 
/*	$(".subscribe-popup .close a").click(function() {
		$(this).parent().parent().fadeOut();						  
	});*/
	
	 /* Subscribe Form - Closed Popup */
	 $("#subscribe-form-popup .overlay-popup-cancel").click( function() {
		$("#subscribe-form-popup").fadeOut(700, function() {
			$(".black-overlay").hide();	
		});
		return false;
	 });	
	
	if ($("#subscribeform").length > 0) {
		subscribeUrl = ($("#subscribeform").attr("action"));
		subscribeUrl = subscribeUrl.replace("-no-js.html",".comp")
		$("#subscribeform").attr("action",subscribeUrl);
	}

	 /* Contact Us Validation  */ 
	 $("#order-form").validate({
	 	errorContainer: $("#orderformwarning, #summary"),
		rules: {
			orderformfirstname: "required",
			orderformsurname: "required",
			orderformcompanyname: "required",
			orderformphone: {"required" : true, "minlength" : 9},
			orderformemail: {"required" : true, "email" : true},
			orderformconfirmemail: {"required" : true, "email" : true, "equalTo" : "#orderformemail"}
		}
	 });
	 
	 /* Supplier Enquiry Validation  */ 
	jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
		return $(param).val()?(value != $(param).val()):(value != param);
	});

	$("#supplier-enquiry").validate({
			errorContainer: $("#warning, #summary"),
			rules: {
				firstname: "required",
				surname: {
					notEqualTo:"#firstname",
					required:true
				},
				retailername: "required",
				phone: {"minlength" : 9, "digits":true, "required":true},
				supplieremail: {"required" : true, "email" : true},
				enquiry: "required",
				captcha: "required"
			}
	 });
 
	 /* Contact Us Validation  */ 
	 $("#contact-us").validate({
	 	errorContainer: $("#contactuswarning, #summary"),
		rules: {
			contactusfirstname: "required",
			contactussurname: "required",
			contactuscaptcha: "required",
			contactuscompanyname: "required",
			contactusemail: {"required" : true, "email" : true},
			contactusenquiry: "required"
		}
	 });
	 
	 /* Subscribe Validation */
	 $("#subscribeform").validate({
		rules: {
			email: {"email" : true, "required" : true}
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				target: "#subscribe-form-popup #subscribe-form-result",
				success: function(data) {
					jQuery("#subscribe-form-popup .overlay-popup-cancel").html("Cancel");
					jQuery("#subscribe-form-popup .result").html("");
					jQuery(".black-overlay").show();
					jQuery("#subscribe-form-popup").fadeIn(700);
				},
				clearForm: true
			});
			return false;
		}
	 });
	 
	 /* Competition Form */
	 $("#competition-form").validate({
//		errorContainer: $("#competitionwarning, #summary"),
	 	rules: {
			competitionfirstname: "required",
			competitionsurname: "required",
			competitionbusinessname: "required",
			competitionpositiontitle: "required",
			competitionphone: {"required" : true, "minlength" : 9},
			competitionemail: {"email" : true, "required" : true},
			competitionconfirmemail: {"required" : true, "email" : true, "equalTo" : "#competitionemail"},
//			competitionwords: "required"
			competitiontermandconditions: "required"
		}
	 });
	 
	 /* Registration Form */
	 $("#registrationForm").validate({
	 	rules: {
			yourfirstname: "required",
            yoursurname: "required",
            youremail: {"email":true, "required":true},
            yourjobtitle: "required",
            yourlessee: "required",
            centres: "required",
            yourtenant: {"required":true, "maxlength":6, "digits":true},
            adminfirstname: "required",
            adminsurname: "required",
            adminemail: {"email":true, "required":true},
            admindirectphone: "required",
			tc: "required"
		}
	 });

	/* Pre-populate subscribe e-mail text box */
	$('input.subscribe-input-text').val('enter email address');
	$('input.subscribe-input-text').focus(function(){																  
		$(this).val('');
	});

	// Table Filter
	var theTable = $('table.table-supplier');

	$('select#filter-category').change(function() {
		var filter_value = $('#filter-state').val()+" "+$('#filter-category').val();
		filter_value = filter_value.replace(/all-categories/, '');
		filter_value = filter_value.replace(/all-states/, '');
		$.uiTableFilter( theTable, filter_value );
		
		var category = $('#filter-category').val();
		if ( category == "all-categories" )
		{ $('span.category').html('All Categories'); }
		else
		{ $('span.category').html(category); }
	})
	
	$('select#filter-state').change(function() {
		var filter_value = $('#filter-state').val()+" "+$('#filter-category').val();
		filter_value = filter_value.replace(/all-states/, '');
		filter_value = filter_value.replace(/all-categories/, '');
		$.uiTableFilter( theTable, filter_value );

		var state = $('#filter-state').val();
		state = state.replace(/available-/, '');
		if ( state == "all-states" )
		{ $('span.state').html('All States'); }
		else
		{ $('span.state').html(state); }
	})
});