
function w_open( u, w, h )
{
    var o = "width="+ w +",height="+ h +",resizable=no,scrollbars=no";
    o += ",left=" + (screen.availWidth - w)  / 2 + ",top=" + (screen.availHeight - h) / 2;

    var w = window.open( u, "", o );
}


function formLimit( field, countfield, maxlimit )
{
 	if( field.value.length > maxlimit )
	{
        // if too long...trim it!
		field.value = field.value.substring( 0, maxlimit );
    }
	else
	{
    	// otherwise, update 'characters left' counter
		countfield.value = maxlimit - field.value.length;
	}
}

function formCheck( formobj, fieldRequired, fieldDescription )
{
	var alertMsg  = "";
	var l_Msg     = alertMsg.length;
	var sendFocus = '';

	for( var i = 0; i < fieldRequired.length; i++ )
	{
		var obj = formobj.elements[fieldRequired[i]];
		if( obj )
		{
			if( obj.type == null )
			{
				var blnchecked = false;
				for( var j = 0; j < obj.length; j++ )
				{
					if( obj[j].checked )
					{
						blnchecked = true;
					}
				}

				if( !blnchecked )
				{
					alertMsg += fieldDescription[i] + "\n";
				}
				continue;
			}

			switch( obj.type )
			{
    			case "select-one":
    			{
    				if( obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" )
    				{
    					alertMsg += fieldDescription[i] + "\n";
    				}
    				break;
    			}
    			case "select-multiple":
    			{
    				if( obj.selectedIndex == -1 )
    				{
    					alertMsg += fieldDescription[i] + "\n";
    				}
    				break;
                }
    			case "text":
    			case "textarea":
    			case "password":
    			{
    				if( obj.value == "" || obj.value == null )
    				{
    					alertMsg += fieldDescription[i] + "\n";
    				}
    				break;
    			}
    			case "checkbox":
    			{
    				if (obj.checked == false)
    				{
    					alertMsg += fieldDescription[i] + "\n";
    				}
    				break;
    			}
        		default:
			}

			if( (alertMsg) && (!sendFocus) )
			{
				sendFocus = obj;
			}
		}
	}

	if( alertMsg.length == l_Msg )
	{
		formobj.submit();
		return true;
	}
	else
    {
		alert(alertMsg);
		sendFocus.focus();
		return false;
	}
}


function XMLSubmit( e )
{

	/* Cancel the submit event, and find out which form was submitted */
    knackerEvent( e );

	var target = document.getElementById( e );

	if (!target) return;

    /* Check if this form is already in the process of being submitted.
     * If so, don't allow it to be submitted again. */
	if (target.ajaxInProgress) return;

    /* Set up the request */
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('POST', './agent/xml-submit.php', true);

    /* The callback function */
    xmlhttp.onreadystatechange = function()
    {
		if (xmlhttp.readyState == 4)
		{
        	if (xmlhttp.status == 200)
        	{
        	    addXMLFeedback( xmlhttp.responseXML, target );
        	}
			else
		    {
		        target.submit();
		    }
		}
    }

	var getstr = "";
	for( i = 0; i < target.childNodes.length; i++ )
	{
		if (target.childNodes[i].tagName == "INPUT")
		{
			if (target.childNodes[i].type == "text")
			{
				getstr += target.childNodes[i].name + "=" + target.childNodes[i].value + "&";
			}
			else if (target.childNodes[i].type == "checkbox")
			{
				if (target.childNodes[i].checked)
				{
					getstr += target.childNodes[i].name + "=" + target.childNodes[i].value + "&";
				}
				else
				{
					getstr += target.childNodes[i].name + "=&";
				}
			}
			else if (target.childNodes[i].type == "radio")
			{
				if (target.childNodes[i].checked)
				{
					getstr += target.childNodes[i].name + "=" + target.childNodes[i].value + "&";
				}
			}
			else if (target.childNodes[i].type == "hidden")
			{
				getstr += target.childNodes[i].name + "=" + target.childNodes[i].value + "&";
			}
		}
		else if (target.childNodes[i].tagName == "SELECT")
		{
			var sel = target.childNodes[i];
			getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
		}
		else if (target.childNodes[i].tagName == "OPTION")
		{
			//var sel = target.childNodes[i];
			getstr += target.name + "=" + target.options[target.selectedIndex].value + "&";
		}
	}

	/* Send the POST request */
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(getstr);

    target.ajaxInProgress = true;
}

function XMLSubmitFormOnly( e )
{
	/* Cancel the submit event, and find out which form was submitted */
    knackerEvent(e);

	var target = document.getElementById( e );

	if (!target) return;
    /* Check if this form is already in the process of being submitted.
     * If so, don't allow it to be submitted again. */
	if (target.ajaxInProgress) return;

    /* Set up the request */
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('POST', './agent/xml-submit.php', true);

    /* The callback function */
    xmlhttp.onreadystatechange = function()
    {
		if (xmlhttp.readyState == 4)
		{
        	if (xmlhttp.status == 200)
        	{
        	    addXMLFeedback(xmlhttp.responseXML, target);
        	}
			else
		    {
		        target.submit();
		    }
		}
    }

	var getstr = "";
	for( i = 0; i < target.length; i++ )
	{
		if (target[i].tagName == "INPUT")
		{
			if (target[i].type == "text")
			{
				getstr += target[i].name + "=" + target[i].value + "&";
			}
			else if (target[i].type == "checkbox")
			{
				if (target[i].checked)
				{
					getstr += target[i].name + "=" + target[i].value + "&";
				}
				else
				{
					getstr += target[i].name + "=&";
				}
			}
			else if (target[i].type == "radio")
			{
				if (target[i].checked)
				{
					getstr += target[i].name + "=" + target[i].value + "&";
				}
			}
			else if (target[i].type == "hidden")
			{
				getstr += target[i].name + "=" + target[i].value + "&";
			}
		}
		else if (target[i].tagName == "SELECT")
		{
			var sel = target[i];
			getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
		}
		else if (target[i].tagName == "OPTION")
		{
			//var sel = target[i];
			getstr += target.name + "=" + target.options[target.selectedIndex].value + "&";
		}
	}

	/* Send the POST request */
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send( getstr );

    target.ajaxInProgress = true;
}

function addXMLFeedback( responseXML, target )
{
    if( responseXML )
    {
        var feedbackText = responseXML.getElementsByTagName( 'error' );
    	var feedbackLink = responseXML.getElementsByTagName( 'response' );

        if( feedbackText && feedbackText[0] )
        {
            feedbackText = feedbackText[0].firstChild.data;
        }

    	if( feedbackLink && feedbackLink[0] && feedbackLink[0].firstChild && feedbackLink[0].firstChild.data )
    	{
    		if( confirm( feedbackText ) )
    		{
    			window.location = feedbackLink[0].firstChild.data;
    		}
    	}
    	else
        {
    		alert( feedbackText );
    	}
    }

    // Free up the form to go again
    target.ajaxInProgress = false;
}

function moverMenu( me )
{
    me.style.backgroundColor = '#F0F4FF';
}

function moutMenu( me )
{
    me.style.backgroundColor = '';
}

function selectAll(e)
{
	var target = document.getElementById( e );
	for( i = 0; i < target.length; i++ )
	{
		if( target[i].type == 'checkbox' )
		{
			target[i].checked = true;
		}
	}
}

function selectNone(e)
{
	var target = document.getElementById( e );
	for( i = 0; i < target.length; i++ )
	{
		if( target[i].type == 'checkbox' )
		{
			target[i].checked = false;
		}
	}
}
