var popups_enabled;
var popup_opened = "";

function switchPicture(element, newsrc) {
	var middle_x = element.x + (element.width/2);
	var middle_y = element.y + (element.height/2);
	var loading = new Image();
	var loading_div = document.createElement("div");
	loading.src = "modules/waw/main/images/loading.gif";
	loading_div.style.position = "absolute";
	loading_div.style.top = middle_y+"px";
	loading_div.style.left = middle_x+"px";
	loading_div.appendChild(loading);
	window.document.body.appendChild(loading_div);
	
	var new_picture = new Image();
	new_picture.onload = function(){
		window.document.body.removeChild(loading_div);
		element.src = newsrc;
	};
	new_picture.src = newsrc;
}

function waw_alert(who, what) {
	elem = document.createElement("div");
	elem.innerHTML = elem.innerHTML + "<p><h1>"+who+"</h2>"+what;
	document.documentElement.appendChild(elem);
}

function submitFormTo(formname, action) {
	$(formname).action = 'index.php';
	if (!$(formname).waw_req_action) {
		var waw_req_action = document.createElement('input');
		waw_req_action.type = "hidden";
		waw_req_action.name = 'waw_req_action';
		waw_req_action.value = action;
		$(formname).appendChild(waw_req_action);
	} else {
		$(formname).waw_req_action.value = action;
	}
	
	if (!$(formname).waw_cs_encoding) {
		var waw_cs_encoding = document.createElement('input');
		waw_cs_encoding.type = "hidden";
		waw_cs_encoding.name = 'waw_cs_encoding';
		waw_cs_encoding.value = csvalue;
		$(formname).appendChild(waw_cs_encoding);
	} else {
		$(formname).waw_cs_encoding.value = csvalue;
	}
	$(formname).submit();
}

function isInternetExplorer() {
   var userAgent = navigator.userAgent.toLowerCase();
   if (document.all && userAgent.indexOf('msie')!=-1) {
      return true;
   }
   else {
      return false;
   }
}

function getInputFormValue(composant) {
  switch(composant.type) {
    case 'text':
    case 'password':
    case 'textarea':
    case 'radio':
      return(composant.value);
      break;
    case 'checkbox':
      if(composant.checked) {
      	return '1';
      } else {
      	return '0';
      }
      break;
    default:
       if (composant.type.indexOf("select") != -1) {
         val = "";
         for(k=0;k<composant.length;k++)
           if (composant.options[k].selected)
             val = composant.options[k].value;
         return val;
       }
  }
}

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	// Tested with relative and nested objects
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	// Return left postion
	return oLeft
}

function getElementHeight(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}

function getElementWidth(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	// Tested with relative and nested objects
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	// Return top position
	return oTop
}

function placeModalPopupBackground(id) {
	popup_opened = id;
	var dialog_back = document.getElementById("waw_dialog_back");
	var parent = document.getElementById(id);
	if (parent != null) {
		if (dialog_back != null) {
			var x = getAbsoluteLeft(id);
			var y = getAbsoluteTop(id);

			var height = parent.offsetHeight;
			var width = parent.offsetWidth;

			dialog_back.style.position = "absolute";

			dialog_back.style.top = y+"px";
			dialog_back.style.left = x+"px";

			dialog_back.style.height = height+"px";
			dialog_back.style.width = width+"px";
			
			var popup = dialog_back.getElementsByTagName("div")[0];

			popup.style.position = "relative";
			popup.style.top = (height/2)-popup.offsetHeight/2+"px";
			popup.style.left = (width/2)-popup.offsetWidth/2+"px";
			
		} else {
			waw_alert("dialog placing", "unable to find the dialog");
		}	
	} else {
		waw_alert("dialog placing", "unable to find desired parent");	
	}
}


function waw_resize() {
	if (document.getElementById(popup_opened) != null) {
		placeModalPopupBackground(popup_opened);
	}
}

