/* Original Functions */
$.fn.setFocus = function(e){
	if (document.getElementById(e)) document.getElementById(e).focus();
	return this;
};

/* makes forms ajax driven */
$.fn.ajaxSubmit = function(e) {		
	/* Change a form's submission type to ajax */
	this.submit(function(){
		// confirm form
		var confirmTxt = $("#confirmTxt").val();
		input_box = confirm('Would you like to save your changes?');	
		if (input_box == true)	{
		} else {
			return false;	
		}
			
			
		var params = {};
			$(this)
			.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], option[@selected], textarea")
			.filter(":enabled")
			.each(function() {
			params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
		});
		
		//$("body").addClass("curWait");
		$.post("?call=ajax", params, function(xml){
		//$("body").removeClass("curWait");
			var strError = "Error";
			oFocus = null;
			record = xml.getElementsByTagName("status");
			var status = record[0].childNodes[0].nodeValue;
			//alert(status);
			if (status == 1) {
				$("div.msgBox").html("Record has been updated").filter(":hidden").fadeIn("normal").fadeOut(1000);
				parseValidationResponses(xml);
				
				record = xml.getElementsByTagName("location");
				var strRedirect = record[0].childNodes[0].nodeValue;
				if (strRedirect =='0') {
				} else {
					window.location = "index.php?"+strRedirect;
				}
				
				record = xml.getElementsByTagName("jfns");
				//var functionName = record[0].childNodes[0].childNodes[0].nodeValue;
				//if (functionName != 0) {
				//	var req_id = record[0].childNodes[1].childNodes[0].nodeValue;
				//	eval(functionName+"("+req_id+")");
				//}
			} else {
				parseValidationResponses(xml);
				//alert("Znaleziono błędy:\n" + strError);
				if (oFocus) $("#" + oFocus).get(0).focus();
			}
		});
		return false;
	});			
	return this;
}


function parseRecords(xml, dest) {
	var opttypes = document.getElementById(dest);
	record = xml.getElementsByTagName("record");
	ID = new Array();
	nazwa = new Array();
	for (i = 0; i < record.length; i++) {
		nazwa[i] = record[i].childNodes[1].childNodes[0].nodeValue;
		ID[i] = record[i].childNodes[0].childNodes[0].nodeValue;
	}

	for (i = opttypes.options.length-1; i >= 0; i--) {
		opttypes.remove(i);
	}

	for(var i = 0; i < nazwa.length; i++) {
		opttypes.options[i] = new Option(nazwa[i],ID[i]);
	}
} 



function parseValidationResponses(xml) {
	record = xml.getElementsByTagName("field");
	var name = new Array();
	var message = new Array();
	var results = new Array();
	for (i = 0; i < record.length; i++) {
		name[i] 	= record[i].childNodes[0].childNodes[0].nodeValue;
		results[i] 	= record[i].childNodes[1].childNodes[0].nodeValue;
		
		if(record[i].childNodes[2].hasChildNodes()) {
			message[i] = record[i].childNodes[2].childNodes[0].nodeValue;
		} else {
			message[i] = '';
		}
	
	}
	
	for(var i = 0; i < name.length; i++) {
		$("#resp_"+name[i]).html('');
	}
	for(var i = 0; i < name.length; i++) {
		if (results[i] == 1) {
			$("#resp_"+name[i]).css('color','green');
		} else {
			$("#resp_"+name[i]).css('color','red');
		}
		$("#resp_"+name[i]).html(message[i]);
	}
}



function getActiveList(option) {
	$.ajax({
	   type: "POST",
	   url: "index.php",
	   data: "option="+option+"&action=doActiveList&call=ajax",
	   error: function(){
	   		alert('Error when loading data...');
	   },
	   success: function(data){
			$("#defList").html(data);
	   }
	});
}

function getActiveSubList(option, id, map_field, output) {
	$.ajax({
	   type: "POST",
	   url: "index.php",
	   data: "option="+option+"&action=doActiveSubList&call=ajax&id="+id+"&map_field="+map_field+"",
	   error: function(){
	   		alert('Error when loading data...');
	   },
	   success: function(data){
			$("#"+output).html(data);
	   }
	});
}




function doEdit(option, id) {
	$("#editTab").show();
	$('#container-1 > ul').tabs('select', 1);
	$('#container-1 > ul').tabs('disable', 0);

	$.ajax({
	   type: "POST",
	   url: "index.php",
	   data: "option="+option+"&action=doActiveEdit&call=ajax&id="+id,
	   error: function(){
	   		alert('Error when loading record via ajax...');
	   },
	   success: function(data){
			$("#tabEditContent").html(data);
			if (option=='Structure') {
				$("#container-2 > ul").tabs('select', 3);
			} else {
				$("#container-2 > ul").tabs('select', 0);
			}
	   }
	}); 
}

function newRecord(option) {
	$('#container-1 > ul').tabs('select', 1);
	
	$.ajax({
	   type: "POST",
	   url: "index.php",
	   data: "option="+option+"&action=newActiveRecord&call=ajax",
	   error: function(){
	   		alert('Error when creating record...');
	   },
	   success: function(id){
		   	if (id > 0) {
		   		getActiveList(option);
				doEdit(option, id);
			} else {
			alert('Cannot create new record');
			}
	   }
	}); 
}


function doDelete(option, id) {
	$.ajax({
	   type: "POST",
	   url: "index.php",
	   data: "option="+option+"&action=doActiveDelete&call=ajax&id="+id,
	   error: function(){
	   		alert('Error when deleting record...');
	   },
	   success: function(status){
	   		getActiveList(option);
			$("div.msgBox").html("Record has been deleted").filter(":hidden").fadeIn("normal").fadeOut(1000);
	   }
	}); 
}


function closeEditMode(option) {
	$("#tabEditContent").html();
	$("#editTab").hide();
	$('#container-1 > ul').tabs('enable', 0);
	$('#container-1 > ul').tabs('select', 0);
	getActiveList(option);	
} 


function confirm_del() {
	input_box = confirm("Do you want to delete selected record?");	
	if (input_box == true)	
		return true;
	else	
		return false; 	
}  


function createTableFromXML(xml, tag, out) {
	
	var Parent = document.getElementById(out); 
	while(document.getElementById(out).hasChildNodes())     {
		Parent.removeChild(Parent.firstChild);
	}
	
	var x = xml.getElementsByTagName(tag);
	var newEl = document.createElement('TABLE');
	newEl.setAttribute('cellPadding',1);
	var tmp = document.createElement('TBODY');
	newEl.appendChild(tmp);
	var row = document.createElement('TR');
	for (j=0;j<x[0].childNodes.length;j++)
	{
		if (x[0].childNodes[j].nodeType != 1) continue;
		var container = document.createElement('TH');
		var theData = document.createTextNode(x[0].childNodes[j].nodeName);
		container.appendChild(theData);
		row.appendChild(container);
	} 
	tmp.appendChild(row);
	for (i=0;i<x.length;i++) 
	{
		var row = document.createElement('TR');
		for (j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1) continue;
			var container = document.createElement('TD');
			var theData = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
			container.appendChild(theData);
			row.appendChild(container);
		}
		tmp.appendChild(row);
	}
	document.getElementById(out).appendChild(newEl);
} 


// set relation between two tables like Products - Colors
assignRelation = function(key_2, key_1, option, msgTarget) {
			
	$.ajax({
	   type: "POST",
	   url: "index.php",
	   data: "option="+option+"&action=doAssign&call=ajax&key_2="+key_2+"&key_1="+key_1+"",
			 
	   // on success
	   error: function(){ 
	   		alert('Error loading data');   
	   },
	   success: function(xml){
			$("#"+msgTarget+""+key_1).html("Assignment of this item has been changed").filter(":hidden").fadeIn("normal").fadeOut(1000);
		}
   }); 
}
 
$(document).ready(function(){
	$(".rollover").hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) {
				var newSrc = $(this).attr("src").replace(".png","_over.png#hover");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.png#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.png#hover",".png");
				$(this).attr("src",oldSrc);
			}
		}
	);
})


$(document).ready(function(){
	$(".rollover-gif").hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) {
				var newSrc = $(this).attr("src").replace(".gif","_over.gif#hover");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.gif#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.gif#hover",".gif");
				$(this).attr("src",oldSrc);
			}
		}
	);
});

function doRead(news_id) {
	 if (news_id > 0) {
	  $.ajax({
		   type: "POST",
		   url: "/index.php",
		   data: "option=News&action=doRead&call=ajax&id="+news_id,
				 
		   // on success
		   error: function(){
				alert('Error');
		   },
		   success: function(data){
				$("#news_box").fadeOut(200).html(data).fadeIn(500);
			}
	   });
	}
}


$(document).ready(function(){
	$(".list-link").bind("click", function(e){
		  var news_id = $(this).attr("news_id");
		  if (news_id > 0) {
			  $.ajax({
				   type: "POST",
				   url: "/index.php",
				   data: "option=News&action=doRead&call=ajax&id="+news_id,
						 
				   // on success
				   error: function(){
						alert('Error');
				   },
				   success: function(data){
						$("#news_box").fadeOut(200).html(data).fadeIn(500);
					}
			   });
		  }
	});
	$("#archive-link").bind("click", function(e){
			 $.ajax({
			   type: "POST",
			   url: "/index.php",
			   data: "option=News&action=doArchive&call=ajax",
					 
			   // on success
			   error: function(){
					alert('Error');
			   },
			   success: function(data){
					$("#news_box").fadeOut(200).html(data).fadeIn(500);
				}
			});
	});
});


function switchCB(fname) {
	if ($("#cb_"+fname).attr('checked')) {
		 $("#"+fname).val('1');
	} else {
		 $("#"+fname).val('0');
	}
} 

function getContent(option, id) {
	alert(option);	
}

function popWin(url, width, height) {
	var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=1,scrollbars=yes,menubar=no' );
} 

function delFile(option, id, fname) {
	input_box = confirm("Do you want to remove this file?");	
	if (input_box == true)	{
		$.ajax({
		   type: "POST",
		   url: "index.php",
		   data: "option="+option+"&action=doFileDelete&call=ajax&id="+id+"&fname="+fname,
		   error: function(){
				alert('Error when deleting file...');
		   },
		   success: function(status){
				$("#fbox"+fname).html('').fadeIn("normal");
		   }
		}); 
	} else return false;
} 
