var totalItems = 0;
// The following are specified on the calling page
//var competition_info_URL = "xml/competition_info.xml";


 $(document).ready(function(){
	$.ajax({
		type: "GET",
		url: competition_info_URL ,
		dataType: "xml",
		success: function(xml) {
			var title = $(xml).find('category_title').text();
			var last_updated = $(xml).find('last_updated').text();
			var approved_by = $(xml).find('approved_by').text();
			var textcontainer = $('#section_1');
			totalItems = $(xml).find('competition').length;
			var count = 0;
			var strText = '<p style="width: 100%;text-align: center;" class="red_bold_large"\>' + title + '<\/p\>';
			$(xml).find('society_competition').each(function(){
				var competition_type = $(this).find('competition_type').text();
				if (competition_type == 'society_competition') {
					$(this).find('competition').each(function(){
						if (count < itemsPerPage) {
							//strText += "<div class='itemNo'\>";
							var comp_name = $(this).find('comp_name').text();
							var description = $(this).find('description').text();
							var conditions = $(this).find('conditions').text();
							strText += "<div class='blue_bold_large'\>" +comp_name  + "<\/div\>";
							strText += "<div class='simple_text'\>" + description  + "<\/div\>";
							strText += "<div class='italic_text'\>" + conditions  + "<\/div\>";
						}
						count++;
					})
				} else if (competition_type == 'society_trophies'){
					var title = $(this).find('title').text();
					strText += '<p style="width: 100%;text-align: center;" class="red_bold_large"\>' + title + '<\/p\>';
					$(this).find('competition').each(function(){
						if (count < itemsPerPage) {
							var comp_name = $(this).find('comp_name').text();
							var description = $(this).find('description').text();
							strText += "<div class='blue_bold_large'\>" +comp_name  + "<\/div\>";
							strText += "<div class='simple_text'\>" + description  + "<\/div\>";
						}
						count++;
					})
				}
			});
			strText += "<p style='text-align:center;font-style:italic;font-weight: 600;'\>" + approved_by + "<\/p\><hr \/\><p \/\>";
			
			textcontainer.append(strText);
		},
		error:(function(x,e) { 
			if(x.status==0){
				alert('You are offline!!\n Please Check Your Network.');
			}else if(x.status==404){
				alert('Requested URL not found.');
			}else if(x.status==500){
				alert('Internel Server Error.');
			}else if(e=='parsererror'){
				alert('Error.\nParsing JSON Request failed.');
			}else if(e=='timeout'){
				alert('Request Time out.');
			}else {
				alert('Unknow Error.\n'+x.responseText);
			}
		})
	});
});


function refresh(pageNo) {
	$.ajax({
		type: "GET",
		url: rule_URL,
		dataType: "xml",
		success: function(xml) {
			var div = document.getElementById("section_1");
			while (div.hasChildNodes()) {
				div.removeChild(div.lastChild);
			}
			var title = $(xml).find('title').text();
			var last_updated = $(xml).find('last_updated').text();
			var approved_by = $(xml).find('approved_by').text();
			var textcontainer = $('#section_1');
			totalItems = $(xml).find('competition').length;
			var count = 0;
			var strText = '<p style="width: 100%;text-align: center;" class="red_bold_large"\>' + rule_title + '<\/p\>';
			$(xml).find('competition').each(function(){
				if (count >= itemsPerPage*(pageNo -1) && count < itemsPerPage*pageNo) {
					var comp_name = $(this).find('comp_name').text();
					var description = $(this).find('description').text();
					var conditions = $(this).find('conditions').text();
					strText += "<div class='blue_bold_large'\>" +comp_name  + "<\/div\>";
					strText += "<div class='simple_text'\>" + description  + "<\/div\>";
					strText += "<div class='italic_text'\>" + conditions  + "<\/div\>";
				}
				count++;
			});
			strText += "<p style='text-align:center;font-style:italic;font-weight: 600;'\>" + approved_by + "<\/p\>";
			
			textcontainer.append(strText);
		}//,
		//failure: function() {(alert('oh no'))};
	});
	};
