/*
	@ver 2.1
*/
var Core=(function(){
	var createdVariables;
    function constructor()
	{

	}
	
	constructor.detectParent=function (obj,tagName)
	   {
	      	if (obj){
		      	var n=64;//up 2 32 parrent levels
		       	var o=obj.parentNode;
	    		for (i=0;i<n;i++)
		       	{
				
			      	if (o==null)break;
			        var id=o.id+"";
	        		if ((o.tagName)&&(o.tagName==tagName)&&((!id) || ((id)&&(id.indexOf("_fake")==-1))))
			      	{
				     	return o;
	    			}
		       		o=o.parentNode;
	            }
	            return null;  
		   }  
	       return null;
	    }

	constructor.getElementsByTags=function(obj,out,elements)
		{
		    if (obj.hasChildNodes()){
			    var list=obj.childNodes;
				var n=list.length;
				for(var i=0;i<n;i++){
				    var lObj=list.item(i);
				    var tag=lObj.tagName;
				    if (elements.indexOf(tag)>=0){
					    out.push(lObj);
					}
					this.getElementsByTags(lObj,out,elements);
				}
			    
			}
		}                   
    constructor.createVariables=function(obj,array)
    {
        //alert("create Variables");
		var oForm=this.detectParent(obj,"FORM");
		if (!oForm) throw("createVariables: Cannot fine FORM using this context "+this);
		//alert("create Variables "+array);
		for(var varName in array){
            //alert("Create Variable "+varName+"="+array[varName]);
			var newObj=document.getElementById(varName);
            if (newObj==null){
                newObj=document.createElement("input");
                newObj.setAttribute("type",'hidden');
				oForm.appendChild(newObj);
            }
            try{
                if (newObj.getAttribute("type")!="hidden")
                    newObj.setAttribute("type",'text');
            }catch(err){
                alert("cannot set type to "+newObj+":"+newObj.id);
            }
            newObj.value=array[varName];
            newObj.name=varName;
            newObj.id=varName;
            newObj.style.display="none";
        }
        return constructor;

    }
    constructor.ld=function()
    {
			//return;	   
		  	var objL=document.getElementById("loading");
			if (objL){
				objL.style.display="block";
				objL.style.position="absolute";
				objL.style.top="10px";
				objL.style.left="10px";		  	
				//document.getElementById("body").style.display="none";
				Boot.loading();
			}
	}
	constructor.setCookie=function( name, value, expires, path, domain, secure )
	{
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
		
		/*
		if the expires variable is set, make the correct
		expires time, the current script below will set
		it for x number of days, to make it for hours,
		delete * 24, for minutes, delete * 60 * 24
		*/
		if ( expires )
		{
		expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		
		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
	}
	constructor.getCookie=function( check_name ) {
		// first we'll split this cookie up into name/value pairs
		// note: document.cookie only returns name=value, not the other components
		var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; // set boolean t/f default f
	
		for ( i = 0; i < a_all_cookies.length; i++ )
		{
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split( '=' );
	
	
			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
			// if the extracted name matches passed check_name
			if ( cookie_name == check_name )
			{
				b_cookie_found = true;
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 )
				{
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				// note that in cases where cookie is initialized but no value, null is returned
				return cookie_value;
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		if ( !b_cookie_found )
		{
			return null;
		}
	}	
	
	constructor.setAnchor=function(anchor)
	{
		this.setCookie("anchor",anchor,	30,"/","");    
	}
	
	constructor.gotoAnchor=function(anchor)
	{
	    var anchor=this.getCookie("anchor");
	    if (anchor!=null){
	        //alert("going to anchor");
			window.location.hash=anchor;    
			this.setAnchor(null);
		}
	}
	constructor.call=function(obj, f, msg, array)
	{
		if (array){
		    //alert("creating array"+array);
			this.createVariables(obj,array);    
		}
		return this.initGFCall(obj,f,null,msg);
	}
	
	constructor.initCall=function(obj,wf,msg)
	{   
		if (msg){
		    var c=confirm(msg);
		    if (!c) return false;
		}
		var oForm=this.detectParent(obj,"FORM");
		if (oForm){
			this.ld();
			if(obj.getAttribute("loader")!="off")
				Boot.loading();
//            this.prepareFieldsForPOST(oForm);
			if (!oForm.wf){
				var oWF=document.createElement("input");

				oWF.setAttribute("type","hidden");
				oWF.id="wf";
				oWF.name="wf";
				oWF.value=wf;
				oForm.appendChild(oWF);				
			}else
				oForm.wf.value=wf;
			oForm.submit();
			return true;
		}else{
		    throw("initCall: Cannot fine FORM using this context "+this);
		}
	}
	/**
		igfm=instanceGenericFormMethods(generic form) function
		igf=instanceGenericForm(as declared in module class)
		msg=message;
	*/
	constructor.initGFCall=function(obj,igfm,igf,msg)
	{
		var vars=new Array();
		if(igfm) vars["igfm"]=igfm;
		if (igf) vars["igf"]=igf;
		this.createVariables(obj,vars);
		return this.initCall(obj,"dGFCall",msg);	    
	}

	constructor.confirmLink=function(obj,msg)
	{
		var r=confirm(msg)
		if (r)
		{
			link=obj.getAttribute("link");
			//alert(link);

			window.location=link;
		}
	}

	constructor.confirmIfrLink=function(obj,msg,target)
	{
		var r=confirm(msg)
		if (r)
		{
			link=obj.getAttribute("link");
			//alert(link);
		    var obj=document.getElementById(target);
			if (!obj) alert("Error");
		    obj.frameborder="yes";
		    obj.scrolling="yes";
			
			obj.style.display="inline";
	//		window.location=link;
	//		alert(target);
			window.open(link,target);
		}
	}
    constructor.getUniqueId=function()
    {
        return Math.random()*78902;
    }
    
	constructor.print=function(obj,id,addPrefix){
		this.printByIfr(id,addPrefix);     
	}
    
	constructor.printByIfr=function(id,addPrefix)
    {
        var obj=document.getElementById("printIFR");
        if (!obj)
        {
            obj=document.createElement("IFRAME");
            document.body.appendChild(obj);
        }
        obj.frameborder="no";
        obj.scrolling="no";
        obj.style.width="1px";
        obj.style.height="1px";
        if (addPrefix)
			obj.src="print.php?print_"+id+"&"+Math.random();
		else
			obj.src="print.php?"+id+"&"+Math.random();
        //alert("d");
    }

    constructor.setSize=function(parentFrameId)
    {
    	var obj=document.getElementById("bdy");
    	var h=obj.offsetHeight+133;
    	//var h=document.body.offsetHeight;
    	// var h=document.body.;
    	
    	var objFrame=parent.document.getElementById(parentFrameId);
    	if (objFrame)
    	{
    		objFrame.height=h;
    		objFrame.focus();
    	}
    }
    
	return constructor;	
})();



