﻿// AppView.js
/**
 * The App View Manager provides a means of .
 * @ class
 */
ATUM.UI.Action = {};

ATUM.UI.Action = {

    /*
     *@public
    */
    ExecuteAction:function(clientPage, action, target, args, form_name, target_form) 
    {	
	    //if (form_name == '') 
	    //{
		    form_name = 'MainForm';
	    //}
	    //alert('execute pre url set. form_name: ' + form_name);
	    
	    var myForm = document.forms[form_name];
	    var url = clientPage+'?action=' + action + '&args=' + args;
        
        myForm.action = url;
	    myForm.method = 'POST';

	    //alert('execute pre submit');
	    
	    myForm.submit();
	    //alert('execute post submit');
	    return false;
    },
    
    SetFormParams:function(clientPage,action, args)
    {
        //if (formname=='')
        //{
            var formname = 'MainForm';
        //}
	    var myForm = document.forms[formname];
	    var url = clientPage+'?action=' + action + '&args=' + args;

        myForm.action = url;
	    myForm.method = 'POST';

//            if ((targetform==\"\") || (targetform==null))
//            {
//                targetform = '_self';
//            }
            
            ///document.forms[formname].target = targetform;
            
//            alert('SetFormParams - action: ' + action);
//            document.forms[formname].method = 'POST';
        
    
    },
    
    _ExecuteAction:function(action,args,formName)
    {
	    try
	    {
//		    alert(ATUM.UI.Specification.IsString('p'));
//		    alert(ATUM.UI.Specification.IsNumeric('p'));
//		    alert(ATUM.UI.Specification.IsNull('p'));
//		    alert(ATUM.UI.Specification.IsObject('p'));
//		    _specs.IsString('p');
		    alert('in ATUM.UI.Action.ExecuteAction - FormName:' + formName); 
		    this.Postback(formName);
		    
//		    alert(action+args+formname);
	    }
	    catch(e)
	    {
		    alert('unable to process requested action! error: ' + e);
	    }
    },
    
    //GetForm
    GetForm:function(argForm)
    {
    	var theForm = document.forms[argForm];

//        alert('In GetForm : ' + argForm);
//        alert('IsDefined' + ATUM.UI.Specification.IsDefined( argForm ) );
//        alert('IsNull' + ATUM.UI.Specification.IsNull( argForm ) );
//        
// 		alert(true || false);
//		
		var undefinedForm = (!ATUM.UI.Specification.IsDefined( argForm )) ;
//        alert('IsDefined ' + undefinedForm );

		var nullForm = ATUM.UI.Specification.IsNull( argForm );
//        alert('IsNull ' + nullForm );
		
		if ( undefinedForm  )
		{
            alert('undefinedForm : ' + undefinedForm );
			theForm = document.forms[0];
		}
		else if ( nullForm )
		{
            alert('IsNull: ' + nullForm );
			theForm = document.forms[0];
		}
		
//		if ( ATUM.UI.Specification.IsObject( argForm ) )
//		{
//			if ( argForm.className == "FORM" )
//				theForm = argForm;
//		}
//		else if ( ATUM.UI.Specification.IsNumeric( argForm ) )
//			theForm = document.forms[argForm];
//		else if ( ATUM.UI.Specification.IsString( argForm ) )
//		{
//			for ( var ii = 0; ii < documents.forms.length; ++ii )
//			{
//				if ( documents.forms[ii].id == objForm )
//				{
//					theForm = documents.forms[ii];
//					break;
//				}
//			}
//		}

		// Assign class name if we have a form.
//		if ( theForm != null )
//			 theForm.className = "FORM";
//        
        
		// Return result.
		return theForm;

    },
    
    /*	PostBack
        @public
	    Usage:
	        PostBack()
	        PostBack( formIndex )
	        PostBack( formIndex, eventTarget )
	        PostBack( formIndex, eventTarget, eventArgument )
	        PostBack( eventTarget )
	        PostBack( eventTarget, eventArgument )
	*/
    Postback:function()
    {
        alert('in Postback'); 

        var theForm = null;
		var eventTarget = null;
		var eventArgument = null;
		
        //alert('Postback arguments.length: ' + arguments.length);
        
		for ( var ii = 0; ii < arguments.length; ++ii )
		{
			if ( ATUM.UI.Specification.IsNumeric( arguments[ii] ) )
				theForm = this.GetForm( arguments[0] );
			else if ( ATUM.UI.Specification.IsString( arguments[ii] ) )
			{
				if ( eventTarget == null )
					eventTarget = arguments[ii];
				else if ( eventArgument == null )
					eventArgument = arguments[ii];
				else
					alert( "Invalid arguments" );
			}
			else
				alert( "Invalid argument type" );
		}

		if ( theForm == null )
			theForm = this.GetForm();

		if ( eventTarget == null )
			eventTarget = "";

		if ( eventArgument == null )
			eventArgument = "";

		if ( ATUM.UI.Specification.IsDefined( theForm.__EVENTTARGET ) )
			theForm.__EVENTARGET.value = eventTarget.split("$").join(":");

		if ( ATUM.UI.Specification.IsDefined( theForm.__EVENTARGUMENT ) )
			theForm.__EVENTARGUMENT.value = eventArgument;

		// Submit the form.
		theForm.submit();
    }

}
