	
	var formData = new Object();
	var outlineInfo = new Object() ; 
	var windowOpen = false ;
	var contactFormOpen = false ; 
	
	/**
	 * This function returns the top and left offset of any object in the page
	 * thanks to the great http://www.quirksmode.org/js/findpos.html
 	*/
	function findPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}

	//  Fill formData variable for testing or debug. 
	function fillFormdata() 
	{	
		formData.username = 'alex' ; 
		formData.userlastname = 'alex' ; 
		formData.maptype = 'countryOutline' ; 
		formData.link = 'link' ; 
		formData.email = 'alex@alex.com'
		formData.loc = 'test_paraguay' ; 
		formData.lat = '9.4490618268814194' ; 
		formData.long = '-62.05078125'
		formData.maxlat = '0' ;  
		formData.maxlong = '-56.074218754' ; 
		formData.zoom = 6 ; 
		formData.zindex ='' ; 
		formData.path = 'countries' ; 
		formData.imgPath = 'http://localhost/OpenAtlas/projects/pmp/countries/test_paraguay6-20-30-22-32.gif' ; 
		formData.desc = 'desc' ; 
		formData.keywords = '' ;
		
		formData.newImg = new Image(); 
	    formData.newImg.src = formData.imgPath ;
	    
	    outlineInfo.imgUrl = "http://al.ex34.free.fr/gmap/imgs/china-map2.jpg" ; 
		outlineInfo.coImg = new Image();  
		outlineInfo.coImg.src = outlineInfo.imgUrl ; 
	}
	
	// Show image in the formulare 
	function afficheImgForm(imgUrl)
	{
		$('formImg').width = 100 ; 
		$('formImg').height = 100 ; 
		$('formImg').src  = imgUrl ; 
		$('formImgDiv').show();
		formData.imgPath = imgUrl ; 	
		formData.newImg = new Image(); 
	    formData.newImg.src = formData.imgPath ;
	}

//swaps the manuel positionner image and retreives values
function swapImg(path)
	{  
	    newimage = new Image(); 
	    newimage.src = path;
	    dd.elements.over.defw = newimage.width;
	    dd.elements.over.w = newimage.width; 
	    dd.elements.over.defh = newimage.height;
	    dd.elements.over.h = newimage.height; 
	    //document.over.width = newimage.width;	
	    dd.elements.over.swapImage(newimage.src);   
	    dd.elements.over.resizeTo(newimage.width,newimage.height);
	    formData.imgPath = path;
    }
//value retreival for manuel positionner
function SwapGeoLabel()
	{
		formData.zoom = mapform.getZoom();
		formData.maptype = 'donatelogo' ; 
		//North West ****************************************
		var p = findPos(document.getElementById('map'));
		var xy = new Object();
		xy.x = dd.elements.over.x - p[0];
		xy.y = dd.elements.over.y - p[1];
		var ll = mapform.fromContainerPixelToLatLng(xy);

		//South East *************************************
		var xy2 = new Object();
		xy2.x = dd.elements.over.x+dd.elements.over.w;
		xy2.y = dd.elements.over.y+dd.elements.over.h;
		var ll2 = mapform.fromContainerPixelToLatLng(xy2);
		
        formData.lat = ll.y;
        formData.long = ll.x;
		formData.maxlat = ll2.y;
        formData.maxlong = ll2.x;
	}
	// Get coordinated of the outline choose by drawer.
	function getOutlineCoordinate() 
	{
		var params = 'id='+formData.coId ;
				
		new Ajax.Request(cpath+'modules/'+moduleName+'/scripts/getOutlineInfos.php', { method:'post',
			postBody: params,
		  	onCreate: function(requestObj){
		    	//alert('create :[	'+ params) ; 
		    },
		  	onSuccess: function(requestObj){	
		  					  		
				var str = requestObj.responseText ; 
				str.evalScripts() ;			
				
				formData.zoom = outlineInfo.zoom ; 
		        formData.lat = outlineInfo.lat;
		        formData.long = outlineInfo.long ;
				formData.maxlat = outlineInfo.maxlat ;
		        formData.maxlong = outlineInfo.maxlong  ;
		        formData.loc = outlineInfo.loc  ;
		    },
		    onFailure: function(requestObj){		    
		    	alert('an error occured.' ) ; 
		    }
		});		
	}
	
	// 
	function compareImageSize() 
	{	    	        	        
	   if(	outlineInfo.coImg.width  != formData.newImg.width || 
	   		outlineInfo.coImg.height != formData.newImg.height ){
	   		return false ;    		
	   	}else {
	   		return true ;
	   	}	
	}

	//
	function validateForm(formtype)
	{	
		formData.agrement = $('agrement').checked ; 
		
		if( isFormValid() ){
			if(formtype && formtype=="manuelpos"){
				Dialog.okCallback();
				dd.elements.over.show(); 
				$("saveMenu").show();
			}
			else{
		       	if( compareImageSize() ){ 	
		       		// Close the form window 
					Dialog.okCallback();
		       		
					formData.zoom = outlineInfo.zoom ; 
			        formData.lat = outlineInfo.lat;
			        formData.long = outlineInfo.long ;
					formData.maxlat = outlineInfo.maxlat ;
			        formData.maxlong = outlineInfo.maxlong  ;
			        
			        SaveMap() ; 
				}else {
					pmpInfo("<img src=\""+cpath+"/gfx/icons/warning.gif\"/><br/>  Your image doesn't have the same size as the corresponding country outline. <br/>You must use our outlines (menu > get Outline).<br/> Do not change the size.") ; 
				}
			}	
			// Show Debug Window
			if(debugMode){
				var debugText ; 
			
				$H(formData).each(function(node){
						debugText += node.key+"  [" + node.value +"]<br>"; 		
					});
				$('debugWindow').show();	
				$('debugWindow').update(debugText) ; 
			}				
		}//end if 	
	}// end validateForm()	
	
	/***
	 *  The function check formData fields.
	 */
	function isFormValid() 
	{	
		var errorMsg = "" ; 
		
		formValidator.add( {"value": formData.username, "command":"filled", "msg":"<li>Your name is empty</li>"}) ; 
		formValidator.add( {"value": formData.userlastname, "command":"filled", "msg":"<li>Your last name is empty</li>"}) ; 
		formValidator.add( {"value": formData.email, "command":"email", "msg":"<li>email isn't valid</li>"}) ; 
		formValidator.add( {"value": formData.loc, "command":"filled", "msg":"<li>Location is empty</li>"}) ; 
		formValidator.add( {"value": formData.imgPath, "command":"filled", "msg":"<li>Url for the image is empty</li>"}) ; 
		
	
		errorMsg = formValidator.checkIt() ;		
		
		formValidator.clear() ; 
		
		if( !formData.agrement ) 
			errorMsg +='<li style="color:red">You must agree this is your personnal creation and you give PMP the rights to use it for this unique application.</li>' ; 
		
		if( errorMsg == "" ) {
			return true ; 
		}else {
			pmpInfo( '<ul>'+errorMsg+'</ul>' ) ; 
			return false ; 		
		}
	}
	
	// save new map
	function SaveMap(formtype)
	{
		var msg = '<div id="formWrapper" style="background-color:#B3B300; padding:20px 10px 10px 20px; ">\
					<fieldset style="background-color: white; margin-bottom: 10px;">\
					<div class="mandatory"><label><b>Name</b> : '+ formData.username +'</label></div>\
					<div class="mandatory"><label><b>Lastame</b> : '+ formData.userlastname +'</label></div>\
					<div class="mandatory"><label><b>email</b> : '+ formData.email +'</label></div>\
					<div class="mandatory even"><label><b>Location</b> : '+ formData.loc +'</label></div>\
					<div class="mandatory"><label><b>Url</b> : '+ formData.imgPath +'</label></div>\
					<div class="mandatory"><label><b>desc</b> : '+ formData.desc +'</label></div>\
					<p class="mandatory"><strong>Do you confirm the above informations ?</strong></p>\
					</fieldset></div>'; 
			 
		Dialog.confirm(msg, 
					 {className: "mac_os_x",  width:325, height:250, title: 'Confirm ',
					 onOk: function (win){ 
					 	if(formtype && formtype=="manuelpos")
					 		SwapGeoLabel();	
					 	ajaxAddMap() ; 
					 } ,
					 draggable:true, 	
					 resizable:true,
					 destroyOnClose:true  }).setZIndex(150) ; 	
	 			 
	}
	
	function showFormData()
	{
		// Show Debug Window
		var debugText ; 
	
		$H(formData).each(function(node){
				debugText += node.key+"  [" + node.value +"]<br>"; 		
			});
		$('debugWindow').show();	
		$('debugWindow').update(debugText) ; 
	}

//Some Ajax Functions
//********************************
	var ajaxRes = false;
	var mapType = "pmpmap";
	function ajaxAddMap()
	{				
		new Ajax.Request(cpath+'modules/'+moduleName+'/scripts/addmaps.php', { method:'post',
			postBody: $H(formData).toQueryString() ,
		  	onCreate: function(requestObj){
				Dialog.cancelCallback();
				$('loaderInfoMsg').innerHTML='Please wait few seconds. Work in progress ...'
				Effect.BlindDown('loaderInfo');
		    },
		  	onSuccess: function(requestObj){
				hideWindows() ;
				Effect.BlindUp('loaderInfo');
				if(debugMode) $('debugWindow').update( requestObj.responseText );
							
				var str = requestObj.responseText ;
				str.evalScripts() ; 
				
	        	if( ajaxRes  ){
		        	pmpInfo('Your map is now saved	') ; 
		        	showLayer( mapType ) ; 
		        	formData = new Object();  
		        }else {
		        	pmpInfo('Technical Problem to add your map.') ; 		        
		        }
		    },
		    onFailure: function(requestObj){
		    	alert('an error occured.') ; 
		    }
		});	
	}
	
	/*function ajaxGetOutlines()
	{		
		new Ajax.Updater('getoutline', cpath+'modules/'+moduleName+'/scripts/getOutlines.php', { method:'get' });	
	}*/
	
	// The function hide the windows used during 
	function hideWindows()
	{
		dd.elements.over.hide(); 
		$("saveMenu").hide();
        if(debugMode) $('debugWindow').hide(); 
        showHideInFoSection();
	}
	
	function showHideInFoSection(info ,forceShow)
	{	
		if( !windowOpen ){
			if(info && ($('infoSection').style.display == 'none' || forceShow)){
				new Ajax.Updater('infoSwap', cpath+'modules/'+moduleName+'/templates/'+info+'.php?date="+new Date().getTime()', { method:'get',asynchronous:true, evalScripts:true});	
				if($('infoSection').style.display == 'none'){
					Effect.BlindDown('infoSection');
					Effect.BlindUp('topmenu');
				}
			}
			else {
				Effect.BlindUp('infoSection');
				Effect.BlindDown('topmenu');
			}
		}
	}
	

