$(document).ready(function() {
   $('#JSidentifier').attr('value', '1');
 });

function clone(){
	var count = parseInt($('input[type=file]').length);
	if(count < 10) {
		$('\n<input type="file" name="myFile[' + count +']" /><br />').insertAfter('input[name="myFile['+(count-1)+']"]').hide().fadeIn(750);
	 } else {
		alert('You can upload a maximum of ' + count + ' files at once!');
	}
}

function del(id){
	$('p[class=' + id + ']').fadeOut(750, function(){
		$(this).remove();
	});
}

// pre-submit callback
function showRequest(formData, jqForm) {
	alert('About to submit: \n\n' + $.param(formData));
	return true;
}

// post-submit callback
function showResponse(responseText, statusText)  {
	alert('this: ' + this.tagName +
		'\nstatus: ' + statusText +
		'\n\nresponseText: \n' +
		responseText + '\n\nThe output div should have already been updated with the responseText.');
}

function showIt(query) {
	var successful = $('#successful')[0].checked;
	var val = $(query, '#test').fieldValue(successful);
	var ser = $(query, '#test').fieldSerialize(successful);
	alert('$("'+query+'").fieldValue(): ' + val + '\n\n$("'+query+'").fieldSerialize(): ' + ser);
}

function validate1(formData, jqForm, options) {
	for (var i=0; i < formData.length; i++) {
		if (!formData[i].value) {
			alert('Please enter a value for both Username and Password');
			return false;
	   }
	}
	alert('Both fields contain values.');
}

function validate2(formData, jqForm, options) {
	var form = jqForm[0];
	if (!form.username.value || !form.password.value) {
		alert('Please enter a value for both Username and Password');
		return false;
	}
	alert('Both fields contain values.');
}

function validate3(formData, jqForm, options) {
	var usernameValue = $('#validateForm3 input[@name=username]').fieldValue();
	var passwordValue = $('#validateForm3 input[@name=password]').fieldValue();
	if (!usernameValue[0] || !passwordValue[0]) {
		alert('Please enter a value for both Username and Password');
		return false;
	}
	alert('Both fields contain values.');
}

function processJson(data) {
	alert(data.message);
}

function processXml(responseXML) {
	var message = $('message', responseXML).text();
	alert(message);
}

// helper
function objToString(o) {
	var s = '{\n';
	for (var p in o)
		s += '    ' + p + ': ' + o[p] + '\n';
	return s + '}';
}

// helper
function elementToString(n, useRefs) {
	var attr = "", nest = "", a = n.attributes;
	for (var i=0; a && i < a.length; i++)
		attr += ' ' + a[i].nodeName + '="' + a[i].nodeValue + '"';
	if (n.hasChildNodes == false)
		return "<" + n.nodeName + "\/>";
	for (var i=0; i < n.childNodes.length; i++) {
		var c = n.childNodes.item(i);
		if (c.nodeType == 1)       nest += elementToString(c);
		else if (c.nodeType == 2)  attr += " " + c.nodeName + "=\"" + c.nodeValue + "\" ";
		else if (c.nodeType == 3)  nest += c.nodeValue;
	}
	var s = "<" + n.nodeName + attr + ">" + nest + "<\/" + n.nodeName + ">";
	return useRefs ? s.replace(/</g,'&lt;').replace(/>/g,'&gt;') : s;

};

$().ajaxError(function(ev,xhr,o,err) {
	alert(err);
	if (window.console && window.console.log) console.log(err);
});

$(function() {
	$('#upload').ajaxForm({
		beforeSubmit: function(a,f,o) {
			$('#uploadOutput').html('<img src="./template/img/ajax-loader.gif" alt="uploading..." /><br />');
			$('input[type=submit]').attr('disabled','disabled');
		},
		success: function(data) {
			var $out = $('#uploadOutput');
			if (typeof data == 'object' && data.nodeType)
				data = elementToString(data.documentElement, true);
			else if (typeof data == 'object')
				data = objToString(data);
			$out.html(data);
			$('input[type=submit]').removeAttr('disabled');
			var count = parseInt($('input[type=file]').length);
			$('input[name="myFile[0]"]').val('');
			for(i=1; i <= count; i++){

				$('input[name="myFile['+ i +']"]').remove()

			}
		}
	});
});