$(document).ready(function() {
	$(".validationForm").validate({
		errorClass: "validationError"
	});

	$("ul.sf-menu").find("li:first").addClass("first");
	$("ul.sf-menu").find("li:last").addClass("last");

	$("ul.sf-menu").find("ul").each(function (i){
		$(this).find("li:first").addClass("first");
		$(this).find("li:last").addClass("last");
	});

	$(".report").each(function() {
		if (!$(this).hasClass("notDynamic")) {
			$(this).dataTable({
				"bPaginate":true,
				"bLengthChange":false,
				"bFilter":false,
				"bSort":true,
				"bInfo":true,
				"bProcessing":true,
				"bAutoWidth":true,
				"iDisplayLength":25,
				"sPaginationType":"full_numbers"
			});
		}
	});

	$("ul.sf-menu").superfish({
    	hoverClass:'sfHover',
		delay: 500,
		speed: 'fast', 
        autoArrows: false, 
        dropShadows: false,
        disableHI: true
	}).find('ul').bgIframe({opacity:false});;

	$("table.report tr:even").addClass("even");
	$("table.report tr:odd").addClass("odd");

	$(".checkBoxTree").each(function(){          
		addTreeView($(this), "", "", false, false);
		$(this).TreeViewCheckboxes();              
		$(this).show();                            
	});
	
	$("select.sort").each(function() {
	    var selectedVal = $(this).val();
		$(this).sortOptions(true);
		$(this).val(selectedVal);
	});
});

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' );
} 

jQuery.parseXML = function( xml ) {
   	return jQuery( parseXML(xml) );
};

function addTreeView(element, animation, persist, collapsed, unique) {
	$(element).treeview({                                               
		animated: animation,                                              
		persist: persist,                                                 
		collapsed: collapsed,                                             
		unique: unique                                                    
	});                                                                 
}          

function populateStates(countryElem) {
		var countryVal = countryElem.value;
		$.ajax({
			type: "POST",
			url: apiPath + "/doGenerateProvinceList.jsp",
			data: {fieldName:"province", classes:"required", selectedCountry:countryVal},
			success: function(msg){
				$("#provinceContainer").html(jQuery.trim(msg));
			}
		});
	}

function clearField(theField) {
	if (theField) {
		theField.value = "";
	}
}

function popLiveGame(form, popName) {
	allowResize = "no";
	allowResize = "yes";
			// Start at top left
			LeftPosition = 0;
			TopPosition = 0;
			// Go almost full-screen for Flash Bingo
			myHeight = 830;
			myWidth = 1024;

	
	// Set the left and top window position to zero if logic above has produced a value less than zero
	if (LeftPosition < 0) {	LeftPosition = 0;}
	if (TopPosition < 0) {	TopPosition = 0;}
	// Configure other window properties
	windowprops = "left="+LeftPosition+",top="+TopPosition+",width="+myWidth+",height="+myHeight+",location=0,scrollbars=1,menubar=0,toolbar=0,resizable=" + allowResize;
	// Open window as object
	// IS1315: ensure this window is closed to prevent error message from appearing 
	if (bingoWindowMap[popName] != undefined) bingoWindowMap[popName].close();

	bingoWindowMap[popName] = window.open("", popName, windowprops);
	// Set form target to bingo window name
	form.target = popName;
	// Submit form
	form.submit();
	// Focus on bingo window by object reference
	bingoWindowMap[popName].focus();
	return true;
}