var reload = false;
var popDown = new Array();
var activePopup;

function createCheckBox(name, value, onChange, id){
    //alert(id);
    var element = document.createElement('input');
	element.setAttribute('type', 'checkbox');
	element.setAttribute('name', name);
	element.setAttribute('value', value);          
	element.setAttribute('onchange', onChange);
	element.setAttribute('id', id);                                     
    return element;  
}

function setReload(reload){
  reload = reload;
}

function getReload(){
  return reload;
}

function addPopDown(id){
  popDown.push(id);

}  

function getPopDown(){
  var $return = popDown;
  popDown = Array();
  return $return;
}

function setActivePopup(id){
  if(activePopup && activePopup != id){
	addPopDown(activePopup);
  }
  activePopup = id;  
}

function getActivePopup(){
  return activePopup;
}    

function alertReturnMessage(obj){
  updateMissingCounters();
  alert(obj.message);
}

function getElementByTypeAndParentId(value, parentId,  type){        
  if(!type){
	type = 'id';
  }  

  if(parentId){      
	var parent = document.getElementById(parentId);
	var allHTMLTags = parent.getElementsByTagName('*');           
  } else {
	var allHTMLTags = document.getElementsByTagName('*');
  }  
  var returnElements = new Array();         

  for (i=0; i<allHTMLTags.length; i++) { 
	switch(type){
	  case 'id':
		if (allHTMLTags[i].id==value){          
		  returnElements.push(allHTMLTags[i]);           
		}  
		break;
	  default:
		if (allHTMLTags[i].className==value){          
		  returnElements.push(allHTMLTags[i]);           
		}  
		break;                
	}               
  }
  if(returnElements.length==1){
	return returnElements[0];
  } else {
	return returnElements;
  }      
}  

function getElementByIdAndParentId(id, parentId){    
  return getElementByTypeAndParentId(id, parentId);
}

function getElementByClass(className, parentId) {
  return getElementByTypeAndParentId(className, parentId, 'className');
}

function popDownOtherPopups(otherPopups){
  if(!otherPopups){
	return;
  }        
  for($i=0; $i <= otherPopups.length; $i++){
	oldPopupId = otherPopups[$i];
	if($(oldPopupId)!== null && $(oldPopupId).style.display == 'block'){                 
	  $(oldPopupId).style.zIndex = 99;        
	}      
  }                               
}


function showPopUp(id){
	hiddenId= 'hidden'+id;
	posId= 'pos'+id;
	$(hiddenId).style.display='block';
	alignShownPopup(hiddenId);
	positionizeShownPopup(hiddenId,posId);
}


function alignShownPopup(popupId){              
  $bodySize = window.getSize();   
  if($(popupId) == null){
	return;
  }
  if($(popupId).style.display=='none' || $(popupId).style.display == ''){
	$style = $(popupId).style.display; 
	$(popupId).style.display='block';
	$popupSize = $(popupId).getSize();
	$(popupId).style.display = $style;          
  } else {                                                           
	$popupSize = $(popupId).getSize();
  }     
  $(popupId).x = ($bodySize.x - $popupSize.x)/2;    
  $(popupId).y = ($bodySize.y - $popupSize.y)/2;           
  $(popupId).setPosition($(popupId));   
  $(popupId).style.display = 'block';  
  
  //popDownOtherPopups(otherPopups);
  
  $(popupId).style.zIndex = 100;               
  $(popupId).addEvents({
	 'mouseenter' : function(){        
	  setActivePopup(popupId);
	  //popDownOtherPopups(getPopDown());
	  this.style.zIndex = 100;
	 }
  });                     
}
function positionizeShownPopup(dragId,clickId){
	var drag = new Drag(dragId,
	{
	  handle: clickId           
	}      
	);                         
}  

function alignPopup(popupId, otherPopups){              
  $bodySize = window.getSize();   
  if($(popupId) == null){
	return;
  }
  if($(popupId).style.display=='none' || $(popupId).style.display == ''){
	$style = $(popupId).style.display; 
	$(popupId).style.display='block';
	$popupSize = $(popupId).getSize();
	$(popupId).style.display = $style;          
  } else {                                                           
	$popupSize = $(popupId).getSize();
  }     
  $(popupId).x = ($bodySize.x - $popupSize.x)/2;    
  $(popupId).y = ($bodySize.y - $popupSize.y)/2;           
  $(popupId).setPosition($(popupId));   
  $(popupId).style.display = 'block';  
  
  popDownOtherPopups(otherPopups);
  
  $(popupId).style.zIndex = 100;               
  $(popupId).addEvents({
	 'mouseenter' : function(){        
	  setActivePopup(popupId);
	  popDownOtherPopups(getPopDown());
	  this.style.zIndex = 100;
	 }
  });                     
}

function positionizePopup(dragId, clickId){
	var drag = new Drag(dragId,
	{
	  handle: clickId           
	}      
	);                         
}        

function createUniquePopup(type, id){
  if(document.getElementById('hidden'+type+id)){    
	return;   
  }    
  var elem = document.getElementById('hidden'+type);    
  
  var popup = elem.cloneNode(true);
  
  popup.setAttribute('id', popup.id + id);    
  document.body.appendChild(popup);     
  var popup = document.getElementById('hidden'+type+id);       
  var elem = getElementByIdAndParentId('pos'+type, 'hidden'+type+id);    
  elem.setAttribute('id', elem.getAttribute('id')+ id);
  var elem = getElementByIdAndParentId('close', 'hidden'+type+id);    
  elem.setAttribute('onclick', 'closePopUp(\'hidden'+type+id+'\')');    
  if(type=="ContactEdit"){
	var elem = getElementByIdAndParentId('closePopup', 'hidden'+type+id);
	elem.setAttribute('onclick', 'closePopUp(\'hidden'+type+id+'\')');     
  }        
}

function closePopUp(elId, action){
	
  if(action){
	eval(action);    
  }
  if(getReload()==false){
	el = document.getElementById(elId);
	el.style.display = 'none';    
  } else {
	window.location.reload();
	window.location.href = window.location.href;
  }
}  
