/*
IMPORTANT

In order for ajax to work, you need to have the following line in form.js

Line Number 313:         a.push({name:'system_ajax',value:'true'});

If you need to download a new version of form.js make sure you add this line back in.

*/


// templates can assign SWFObjects to this using inline addSWFObject() calls,
// which will be written out automatically on document.ready()
// this is needed, because templates using inline swfobject.write() calls, break JQuery events
// - BDC
var swfObjects = new Object();


$(document).ready(function () {

	MM_preloadImages(SKIN_URL + 'images/nav_whoweare_over.jpg',SKIN_URL + 'images/nav_meetpeople_over.jpg',SKIN_URL + 'images/nav_connect_over.jpg',SKIN_URL + 'images/nav_makediff_over.jpg',SKIN_URL + 'images/nav_rewards_over.jpg',SKIN_URL + 'images/nav_balance_over.jpg',SKIN_URL + 'images/banner_btn_apply_ovr.jpg',SKIN_URL + 'images/banner_btn_search_ovr.jpg');

    //add in control mechanism for position duration controls
    $(".duration-checkbox").click(function(event){
            var textBox = document.getElementById(event.target.id.replace("bool[", "").replace("]", ""));
            textBox.readOnly = !event.target.checked;
            if (textBox.readOnly) textBox.value = '';
        }
    );
    
    $('.submit_not_hiring').click(function(){
      if(!confirm('Are you sure you want to delete this application?'))
        return false;
    });

    // confirms on click
    $(".confirm-prompt").click(function(event){return confirm_action();});

    $('.clear-value').each(function(i) {this.value = '';});

	writeSWFObjects();
});

// add an SWFObject to be written on document.ready()
function addSWFObject( obj, id )
{
	swfObjects[ id ] = obj;
}

// write all SWFObjects
function writeSWFObjects()
{
	for( id in swfObjects )
		swfObjects[ id ].write( id );
}


function popup( url ){
	popup = window.open( url, 'popup', 'height=300,width=500' );
	if( window.focus )
		popup.focus();
	return false;
}

function initPositionFilter()
{
    $('#position_filter_change').hide();
    $('#filter_position_type').change(function () { positionFilterChange() } );
    $('#filter_store').change(function () { positionFilterChange() } );
    $('#filter_city').change(function () { positionFilterChange() } );
}

function positionFilterChange()
{
    submit_button('position_filter_change');
}


function confirm_action()
{
	return confirm("Are you sure you wish to continue?");
}

function clear_validation_errors()
{
	$('#error_list').empty();
	$('#error_list').hide();
}

function submit_button(button_name)
{
	$('#' + form_name).append('<input type="hidden" name="submit_' + button_name + '" value="JSSUBMIT" />');
	document.getElementById(form_name).submit();
}

function render_validation_errors(validation_errors)
{
	clear_validation_errors();

    $("#error_list").append('<ul id="error_list_ul"></ul>');

	for (var i = 0; i < validation_errors.length; i++) {
		$("#error_list_ul").append('<li>' + validation_errors[i].label + ': ' + validation_errors[i].message + '</li>');
	}

    $("#error_list").slideDown("normal");
}


function addObjectToObject(parent,child)
{
	//default define the object
	function childFunction() {};
	var child_object = new childFunction();


	for (var i = 0; i < child.attributes.length;i++) {
		var attribute = child.attributes[i];
		eval('child_object.' + attribute.nodeName + ' = "' + attribute.nodeValue + '";');
	}

	//check to see if the object is suppose to be an array
	child_object.isArray = false;
	if (child_object.type != undefined && child_object.type == "array") child_object.isArray = true;

	//redefine the object as an array
	if (child_object.isArray == true) {
		child_object = new Array();
		child_object.isArray = true;
	}


	var array_element_name = '';
	for (var i = 0; i < child.childNodes.length;i++)
	{
		var node = child.childNodes[i];

		if (node.nodeName == "#text" && child.childNodes.length == 1 && child_object.isArray == false) {

			if (child.attributes.length > 0) {
				child_object.value = node.nodeValue;
			} else {
				child_object = node.nodeValue;
			}

			continue;
		} else if (node.nodeName == "#text") {
			 continue;
		}

		array_element_name = node.nodeName;


		child_object = addObjectToObject(child_object,node);

	}


	if (parent.isArray == true) {
		parent.push(child_object);
	} else {
		eval('parent.' + child.nodeName + ' = child_object;');
	}

	return parent;
}


function ajax_link(url)
{
	$.post(url,ajax_form_options,ajax_response);
}

var ajax_form_options = {
	dataType: 'xml',
	system_ajax: 'true',
	after: function(responseXML) { ajax_response(responseXML); }
};

function ajax_response(responseXML)
{
	var result = responseXML.documentElement.getElementsByTagName('result')[0];

	function AjaxController() {}
	var ajax_controller = new AjaxController();
	ajax_controller.action = $('action',responseXML).text();

	for (var i = 0; i < result.childNodes.length;i++) {
		var node = result.childNodes[i];
		if (node.nodeName == "#text") continue;

		ajax_controller = addObjectToObject(ajax_controller,node);

		ajax_controller = ajax_controller;

	}


	eval('ajax_controller.' + ajax_controller.action + ' = ' + ajax_controller.action + '_response');
	eval('ajax_controller.' + ajax_controller.action + '()');


}



