/** 
Copyright 2007-2009 Fabien Di Tore - fabien@ditore.ch

Project started on 2008-07-31

This file is part of iceFramework

ICEFramework IS NOT OPEN SOURCE.


IceFramework n'est pas libre. Si vous avez acces a ces sources, vous pouvez le modifier et l'utiliser dans le contexte 
pour lequel il vous a ete mis à disposition (l'application).
Vous ne pouvez PAS le redistribuer, gratuitement ou contre retribution. 
Vous ne pouvez PAS creer de travaux derives de cette version d'IceFramework, le renommer ou masquer cette license. 
Toutes les modifications sont autorisées dans le cadre de l'application pour laquelle il vous a ete mis a�
disposition. Il est modifiable par n'importe quel tiers.

**/

function IMConfirm(msg){
	
	return false;
}

function doAction(sAction,obj){
	obj.action=sAction;
	if(submitable(obj)){
		obj.submit();
	}
}

function doActionNC(sAction,obj){
	obj.action=sAction;
	obj.submit();
}

function doActionLink(sAction,iNumForm){
	var obj = document.forms[iNumForm];
	obj.action=sAction;
	if(submitable(obj)){
		obj.submit();
	}
//	return false;
}


function doActionLinkNC(sAction,iNumForm){
	var obj = document.forms[iNumForm];

	obj.action=sAction;
	obj.submit();
	return false;
}


function doActionLinkConfirm(sAction,iNumForm,sMsg){

    var obj = document.forms[iNumForm];
    
    confirm(sMsg,function(){
       	obj.action=sAction;
        //alert(document.forms[iNumForm].action.value);
       	if(submitable(obj)){
        	obj.submit();
       	}
    });
}

function doActionConfirm(sAction,obj,sMsg){
   /* if(confirm(sMsg)){
            //alert('Submitted');
           	obj.action=sAction;
            //alert(document.forms[iNumForm].action.value);
            if(submitable(obj)){
            	obj.submit();
            }
    }*/
   
    confirm(sMsg,function(){
	 	obj.action=sAction;
        //alert(document.forms[iNumForm].action.value);
        if(submitable(obj)){
        	obj.submit();
        }
    	
    });
    
}



function submitable(obj){ //handle formchecker stuff
	if(obj.onsubmit != undefined){
		return obj.onsubmit();
	}
	return true;
}

/*
sZone is the zone to render
sLocalZone is the zone to push content to
*/
function doActionAjax(sAction,sZone,frm,sLocalZone,effect,callback){
	//alert('test');
	//alert('#'+sZone);
	if($j('#'+sLocalZone).length >0){
		
		sUrl = sAction;
		var datas = '';
		if(frm != undefined){
			datas = grabFormDatas(frm);
		}
		
		if(sLocalZone==undefined || sLocalZone ==''){
			sLocalZone = sZone;			
			
		}
		
		if(effect == undefined){
			effect ='none';
		}
		
		if(frm == undefined || submitable(frm)){
			switch(effect){
				case 'slide':
				
				break;
				case 'fade':
				/*	$j('#'+sLocalZone).fadeOut('slow',function(){
						$j('#'+sLocalZone).empty().append($j('#IMCoreAjaxLoading').html());
					});*/
				break;
				default:
					//$j('#'+sLocalZone).empty().append(msg);
			//		alert($j('#IMCoreAjaxLoading').html());
					$j('#'+sLocalZone).empty().append($j('#IMCoreAjaxLoading').html());
				break;
			}
			
			/*$j('#'+sZone).fadeOut(200,function(){
				$j(this).empty().append($j('#IMCoreAjaxLoading').html()).fadeIn();
				});*/
			//window.location.hash=sAction ;
			$j.ajax({
				type: "POST",
				url: sUrl,
				data: "AJAX="+sZone+'&IMLocalZone='+sLocalZone+'&'+datas,
				success: function(msg){
					//alert(sLocalZone);
				//	$j('#'+sLocalZone).empty().append(msg);
					/*$j('#'+sZone).fadeOut(200,function(){
						$j(this).empty().append(msg).fadeIn();
					});*/
					
					switch(effect){
						case 'fade':
							$j('#'+sLocalZone).fadeOut('slow',function(){
								$j('#'+sLocalZone).empty().append(msg).fadeIn('slow');
								//alert('pouet');
							});
						break;
						case 'slide':
							$j('#'+sLocalZone).slideUp(500,function(){
								$j('#'+sLocalZone).empty().append(msg).slideDown(500);
							});
						break;
						default:
							$j('#'+sLocalZone).empty().append(msg);
						break;
					}
					
					if(callback!=undefined){
						callback(msg);
					}
				}
			});
		}
	}
}


function grabFormDatas(frm){

	//var result = new Object;
	var str = '';
	var sep = '';
//alert(frm.elements);
	for (var item in frm.elements){
	
		//alert(item.name);
		//result[item.name] = item.value;
		itemD = frm.elements[item];
		
		if(itemD && itemD.type != undefined && itemD.name != undefined && itemD.name != '' && itemD.value!=undefined && (itemD.type != 'checkbox' || itemD.checked)){	
			str += sep + itemD.name+'='+itemD.value;
			sep = '&';
		}
	}
	return str;
}

function inspect(obj, maxLevels, level)
{
  var str = '', type, msg;

    // Start Input Validations
    // Don't touch, we start iterating at level zero
    if(level == null)  level = 0;

    // At least you want to show the first level
    if(maxLevels == null) maxLevels = 1;
    if(maxLevels < 1)     
        return '<font color="red">Error: Levels number must be > 0</font>';

    // We start with a non null object
    if(obj == null)
    return '<font color="red">Error: Object <b>NULL</b></font>';
    // End Input Validations

    // Each Iteration must be indented
    str += '';

    // Start iterations for all objects in obj
    for(property in obj)
    {
      try
      {
          // Show "property" and "type property"
          type =  typeof(obj[property]);
          str += '(' + type + ') ' + property + 
                 ( (obj[property]==null)?(': `null`'):('')) + "\n";

          // We keep iterating if this property is an Object, non null
          // and we are inside the required number of levels
          if((type == 'object') && (obj[property] != null) && (level+1 < maxLevels))
          str += inspect(obj[property], maxLevels, level+1);
      }
      catch(err)
      {
        // Is there some properties in obj we can't access? Print it red.
        if(typeof(err) == 'string') msg = err;
        else if(err.message)        msg = err.message;
        else if(err.description)    msg = err.description;
        else                        msg = 'Unknown';

        str += '<li><font color="red">(Error) ' + property + ': ' + msg +'</font></li>';
      }
    }

      // Close indent
      str += "\n";

    return str;
}

function filemanCallback(id){
	$j('#'+id+' +img').attr('src',$j('#'+id).val());
}

function clearIf(val,obj){
	if(obj.value == val){
		obj.value ='';
	}
}

function restoreIf(val,obj){
	if(obj.value== ''){
		obj.value = val;
	}
	
}
