//<%--
//********************************************************************
//*-------------------------------------------------------------------
//* Licensed Materials - Property of IBM
//*
//* WebSphere Commerce
//*
//* (c) Copyright IBM Corp.  2007
//* All Rights Reserved
//*
//* US Government Users Restricted Rights - Use, duplication or
//* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//*
//*-------------------------------------------------------------------
//*
//--%>

	dojo.require("wc.widget.RefreshArea");
	
	dojo.require("dojo.parser");
	
	dojo.require("dojox.collections.ArrayList");

	dojo.require("wc.widget.RefreshArea");
	dojo.require("wc.render.RefreshController");
	dojo.require("wc.render.Context");

	
Common={
	//All the global variables declarations will be here
	errorMessages: {},
	
	getRenderContextProperty : function(/*wc.render.Context*/context, /*String*/propertyName){
		//summary: retrieves the value of the property from a render context
		//description: This function retrieves the value of the property whose name is propertName
		//	from the given context. 
		//returns: null if the context is null. undefined if the property is not found. 
		//	otherwise, the value of the property int he given context. 
		console.debug("enter getRenderContextProperty with propertyName = "+propertyName);
		if(context == null){
			console.debug("context is null. Return null...");
			return null;
		}
		
		var result = context.properties[propertyName]
		console.debug("the found property value is: "+result);
		
		return result;	
	}, 
	
	loadAddressContentFromURL: function(/*String*/contentURL, /*String*/areaID, /*String*/method, /*Object?*/params){
		//summary: loads the response from invoking a given URL to the area that has the given ID. 
		//description: This function invokes the given contentURL, and then updates the area whose ID is 
		//	the given areaID with the response content. 
		//contentURL: the URL used to get new content
		//areaID: the ID of the area that will be updated. The area can be anything that supports
		//	innerHTML, such as table, and div. 
		//method: The HTTP method used for invoking the given URL. The default is GET.
		//params: Optional parameters used when invoking URL
		var contentArea = document.getElementById(areaID);
		if(!contentArea){
			return;	
		}
		
		dojo.io.bind({
			url: contentURL,
			method: method?method:'GET',
			load: function(type, data, evt){
				contentArea.innerHTML = data;
				cursor_clear();	
			},
			mimetype: "text/html", 
			params: params
		});
		cursor_wait();
	}, 
	
	loadAddressContentFromForm: function(form, areaID, params){
		//summary: loads the response from submitting a given form to the area that has the given ID. 
		//description: This function submits a given form, and then updates the area whose ID is 
		//	the given areaID with the response content. 
		//form: the form that is to be submitted
		//areaID: the ID of the area that will be updated. The area can be anything that supports
		//	innerHTML, such as table, and div. 
		var contentArea = document.getElementById(areaID);
		if(!contentArea){
			return;	
		}
		
		dojo.io.bind({
			formNode: form, 
			load: function(type, data, evt){
				contentArea.innerHTML = data;
				cursor_clear();	
			},
			mimetype: "text/html", 
			params: params
		});
		cursor_wait();
	}, 
	
	
	getErrorFields: function(serviceResponse){
		if(!serviceResponse){
			return [];	
		}
		
		var result = serviceResponse.errorMessageParam;
		
		if(dojo.isArrayLike(result)){
			return result;	
		}else{
			return [result];	
		}
	}, 
	
	
	hideErrorNodeNew: function(/*String*/formName){
		var inputs = document.getElementsByTagName('li'); 
		for(var k=0;k<inputs.length;k++)
		{
			var input = inputs[k];
			var fieldName = input.id;
			if(fieldName != ""){
				var errorMessageElementId = fieldName+"_error"+"_message";
				input.className="";
				if(document.getElementById(errorMessageElementId))
					document.getElementById(errorMessageElementId).style.display="none";
			}
			
		} 
	},
	
	displayCommonErrorArea: function(/*String*/formName, /*JSONObject*/lowesErrors, userFriendlyNames, fieldsHasToHideFriendlyName, errorAreaName, fieldsOrdered){
		var lowesUserFriendlyNames;
		dojo.forEach(userFriendlyNames, function(lowesFieldName){
			lowesUserFriendlyNames = lowesFieldName;
		});
		var lowesFieldsHasToHideFriendlyName;
		dojo.forEach(fieldsHasToHideFriendlyName, function(lowesFieldToHide){
			lowesFieldsHasToHideFriendlyName = lowesFieldToHide;
		});
		

		var innerText = "<ul>";
		dojo.forEach(lowesErrors, function(lowesField){
			var fieldName;
			dojo.forEach(fieldsOrdered, function(fieldOrdered){
				for (var key in lowesField){
					if(key == fieldOrdered){
						fieldName = "";
						for(var fieldKey in lowesUserFriendlyNames){
							if( fieldKey == key){
								if(lowesFieldsHasToHideFriendlyName[fieldKey]!='1' &&
										lowesUserFriendlyNames[fieldKey]!=""){
									fieldName = lowesUserFriendlyNames[fieldKey]+"." ;
								}
							}
						}
		    			innerText += "<li>"+lowesField[key]+" "+fieldName+"</li>";
		
						// also display the field-specific error message
		    			var errorElementId = formName+"_"+key+"_error";
		    			var elementId = formName+"_"+key;
		    			var errorMessageElementId = errorElementId+"_message";
		    			if (document.getElementById(elementId) != null) {
			    			document.getElementById(elementId).className="error";
		    				document.getElementById(errorMessageElementId).style.display="block";
		    				document.getElementById(errorMessageElementId).innerHTML=lowesField[key] + " " + fieldName;    			
						} 
					}   				
	    		}
	    	})
		});	
		innerText += "</ul>";
		document.getElementById(errorAreaName).innerHTML=innerText;
		document.getElementById(errorAreaName).style.display = "block";
	},
	
	reportServiceError: function(/*String*/formName, /*JSONObject*/serviceResponse, /*String*/errorAreaName, /*String[]*/fieldsOrdered){
	
		this.hideErrorNodeNew(formName);
		var errors = Common.getErrorFields(serviceResponse);
		var lowesErrors = "";
		var userFriendlyNames = "";
		var fieldsHasToHideFriendlyName = "";
		dojo.forEach(errors, function(field){
			for (var key in field){
				if (field[key] != 'null' && key == 'errorFieldNames') {
					lowesErrors = field[key];
					if(dojo.isArrayLike(lowesErrors)){
						lowesErrors = lowesErrors;	
					}else{
						lowesErrors =  [lowesErrors];	
					}					  	  
				}
				if (field[key] != 'null' && key == 'userFriendlyFieldNames') {
					userFriendlyNames = field[key];
					if(dojo.isArrayLike(userFriendlyNames)){
						userFriendlyNames = userFriendlyNames;
					}else{
						userFriendlyNames =  [userFriendlyNames];
					}					  	  
				}
				if (field[key] != 'null' && key == 'fieldsHasToHideFriendlyName') {
					fieldsHasToHideFriendlyName = field[key];
					if(dojo.isArrayLike(fieldsHasToHideFriendlyName)){
						fieldsHasToHideFriendlyName = fieldsHasToHideFriendlyName;
					}else{
						fieldsHasToHideFriendlyName =  [fieldsHasToHideFriendlyName];
					}					  	  
				}
				
				
			}
		});
		
		this.displayCommonErrorArea(formName,lowesErrors,userFriendlyNames,fieldsHasToHideFriendlyName, errorAreaName, fieldsOrdered);				
	},
	
	initIframe:function(id) {
		// Set the ID of the iframe to resize
		iframe_id = id;		
		lastHash = '';
		function checkForMessages(){
			if(location.hash != lastHash){
				lastHash = location.hash;
				var params = lastHash.substring(1).split('&');
				for( var i = 0, l = params.length; i < l; ++i ) {
		          var parts = params[i].split( '=' );
		          switch( parts[0] ) {
		          case 'height':
		            h = parseInt( parts[1] );
		            break;
		          }
		        }
		        if( typeof( h ) == 'number' ) {
		          Common.sizeIframeHeight(iframe_id,h);	
		        }
			}
		 }			
		iframe_interval = setInterval(checkForMessages, 200);
	},
	sizeIframeHeight:function(id,h) {
		var $iframe = dojo.byId(id);
		if(!$iframe)return false;
		dojo.style($iframe,'height', h + 'px');
		dojo.style($iframe,'overflow','hidden');
		return true;
	}, 
	stopIframe:function() {
		clearInterval(iframe_interval);
		iframe_id = null;
	}

	
};
	
	

