/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;
var click_row = null;
var click_rownum = 0;

function Mod(a, b) {
    return a - Math.floor(a / b) * b;
}

function Div(a, b) {
    return Math.floor(a / b);
}
/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   integer  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColorUG, theDefaultColorG, thePointerColor, theMarkColor)
{
    var theCells = null;
    var theDefaultColor;
    
    if(Mod(theRowNum+1,2) == 0){
      theDefaultColor = theDefaultColorG;
    } else {
      theDefaultColor = theDefaultColorUG;
    }

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 1.1 Sets the mouse pointer to pointer on mouseover and back to normal otherwise.
    // if (theAction == "over" || theAction == "click") {
    //     theRow.style.cursor='pointer';
    // } else {
    //     theRow.style.cursor='default';
    // }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].bgcolor) != 'undefined') {
        currentColor = theCells[0].bgcolor;
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
    if (currentColor.indexOf("rgb") >= 0)
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }
    
    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
	        if(click_row!=null&&click_row!=theCells){
			    // clear last marked row
		        var c = null;
            if(Mod(click_rownum+1,2) == 0){
              selectedDefaultColor = theDefaultColorG;
            } else {
              selectedDefaultColor = theDefaultColorUG;
            }
            
		        // with DOM compatible browsers except Opera
		        if (domDetect) {
		            for (c = 0; c < rowCellsCnt; c++) {
		                click_row[c].setAttribute('bgcolor', selectedDefaultColor, 0);
		            } // end for
		        }
		        // 5.2 ... with other browsers
		        else {
		            for (c = 0; c < rowCellsCnt; c++) {
		                click_row[c].style.backgroundColor = selectedDefaultColor;
		            }
		        }
            
 			      marked_row[click_rownum] = false;

            if(click_row == theCells){
	  	        click_row = null;
		  	      click_rownum = null;
              newColor = null;
            } else {
              marked_row[theRowNum] = true;
              click_row = theCells;
              click_rownum = theRowNum;
              newColor = theMarkColor;
  			    }
          } else {
            marked_row[theRowNum] = true;
            click_row = theCells;
            click_rownum = theRowNum;
            newColor = theMarkColor;
          }
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
	        if(click_row!=null&&click_row!=theCells){
			    // clear last marked row
		        var c = null;
            if(Mod(click_rownum+1,2) == 0){
              selectedDefaultColor = theDefaultColorG;
            } else {
              selectedDefaultColor = theDefaultColorUG;
            }

		        // with DOM compatible browsers except Opera
		        if (domDetect) {
		            for (c = 0; c < rowCellsCnt; c++) {
		                click_row[c].setAttribute('bgcolor', selectedDefaultColor, 0);
		            } // end for
		        }
		        // 5.2 ... with other browsers
		        else {
		            for (c = 0; c < rowCellsCnt; c++) {
		                click_row[c].style.backgroundColor = selectedDefaultColor;
		            }
		        }

 			      marked_row[click_rownum] = false;

            if(click_row == theCells){
	  	        click_row = null;
		  	      click_rownum = null;
              newColor = null;
            } else {
              marked_row[theRowNum] = true;
              click_row = theCells;
              click_rownum = theRowNum;
              newColor = theMarkColor;
  			    }
          } else {
            marked_row[theRowNum] = true;
            click_row = theCells;
            click_rownum = theRowNum;
            newColor = theMarkColor;
			    }
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        // deselect of a marked row is deaktivated to prevent flickering
        // when selecting and deselecting a checkbox
        if (theAction == 'click'&&false) {
          if(thePointerColor != ''){
            newColor = thePointerColor;
          } else {
            newColor = theDefaultColor;
          }
          if(typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum]){
            marked_row[theRowNum] = true;
          } else {
            marked_row[theRowNum] = false;
  	        click_row = null;
	  	      click_rownum = null;
          }
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function


 function istEmail(elm) {
     if (elm.value.indexOf("@") != "-1" &&
         elm.value.indexOf(".") != "-1") {
         return true;
     }
     else {
         return false;
     }
 }

function ShowDeleteFile(id){
	fileInput = document.getElementById(id);
	fileDelete = document.getElementById(id+'_delete');
	fileDeleteLbl = document.getElementById(id+'_delete_label');
	if(fileInput&&fileDelete&&fileDeleteLbl){
		fileInput.style.display = 'none';
		fileDeleteLbl.style.display = 'inline';
		fileDelete.checked = true;
	}
}

function ShowInputFile(id){
	fileInput = document.getElementById(id);
	fileDelete = document.getElementById(id+'_delete');
	fileDeleteLbl = document.getElementById(id+'_delete_label');
	if(fileInput&&fileDelete&&fileDeleteLbl){
		if(fileDelete.checked==false){
		  fileInput.style.display = 'inline';
		  fileDeleteLbl.style.display = 'none';
		}
	}
}

function ImagePreloader(images,callback)
{
	// store the callback
	this.callback = callback;

	// initialize internal state.
	this.nLoaded = 0;
	this.nProcessed = 0;
	this.aImages = new Array;

	// record the number of images.
	this.nImages = images.length;

	// for each image, call preload()
	for ( var i = 0; i < images.length; i++ ) 
		this.preload(images[i]);
}
ImagePreloader.prototype.preload = function(image)
{
	// create new Image object and add to array
	var oImage = new Image;
	this.aImages.push(oImage);
	
	// set up event handlers for the Image object
	oImage.onload = ImagePreloader.prototype.onload;
	oImage.onerror = ImagePreloader.prototype.onerror;
	oImage.onabort = ImagePreloader.prototype.onabort;
	
	// assign pointer back to this.
	oImage.oImagePreloader = this;
	oImage.bLoaded = false;
	oImage.source = image;
	
	// assign the .src property of the Image object
	oImage.src = image;
}
ImagePreloader.prototype.onComplete = function()
{
	this.nProcessed++;
	if ( this.nProcessed == this.nImages )
		this.callback(this.aImages);
}
ImagePreloader.prototype.onload = function()
{
	this.bLoaded = true;
	this.oImagePreloader.nLoaded++;
	this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onerror = function()
{
	this.bError = true;
	this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onabort = function()
{
	this.bAbort = true;
	this.oImagePreloader.onComplete();
}

var popup_image
 
function setPopupImage(aImages){
  var maxWidth = 675;
  var maxHeight = 350;

  if(aImages[0].height>maxHeight){
    popup_image.height = maxHeight;
    popup_image.width = aImages[0].width*(maxHeight/aImages[0].height);
  } else {
    popup_image.height = aImages[0].height;
  }
  
  if((aImages[0].height<=maxHeight)&&(aImages[0].width>maxWidth)){
    popup_image.width = maxWidth;
    popup_image.height = aImages[0].height*(maxWidth/aImages[0].width);
  } else if(aImages[0].height<=maxHeight){
    popup_image.width = aImages[0].width;
  }
  
  popup_image.src = aImages[0].src;
} 
 
function ShowImagePopup(id,imgSource,w,h){
	popup = document.getElementById(id);
	popup_image = document.getElementById(id+'_image');
  if(popup&&popup_image){
    TogglePopupFormElements('hide');
    popup.style.display = 'block';
    if(!(h&&w)){
      popup_image.src = imgSource;
      img = new ImagePreloader([imgSource], setPopupImage);
    } else {
      popup_image.height = h;
      popup_image.width = w;
      popup_image.src = imgSource;
    }
  }
}

function HideImagePopup(id){
	popup = document.getElementById(id);
	popup_image = document.getElementById(id+'_image');
  if(popup&&popup_image){
    popup.style.display = 'none';
    TogglePopupFormElements('show');
    popup_image.src = '/images/inv.gif';
    popup_image.height = 350;
    popup_image.width = 675;
  }
}

function ShowPopup(id){
	popup = document.getElementById(id);
  if(popup){
    TogglePopupFormElements('hide');
    popup.style.display = 'block';
  }
}

function HidePopup(id){
	popup = document.getElementById(id);
  if(popup){
    TogglePopupFormElements('show');
    popup.style.display = 'none';
  }
}

function TogglePopupFormElements(v){
  for (var i = 1; i <= 5; i++){
  	obj = document.getElementById('popup_invisible'+i);
    if(obj){
      if(v=='hide'){
        obj.style.visibility = 'hidden';
      } else {
        obj.style.visibility = 'visible';
      }
    } else {
      break;
    }
  }
}

function table_hover(tr, InOut){
  if(InOut=='in'){
	  tr.style.backgroundColor='#FFFFCC';
	} else if(InOut=='out'){
		if(tr.className=='ug'){
		  tr.style.backgroundColor='#FFFFFF';
		} else {
		  tr.style.backgroundColor='#F0F0F0';
		}	
	}
}



