﻿/* bad browser check */
function isIeLessThan7()
{
	if (navigator.appName == "Microsoft Internet Explorer")
	{ 
		temp=navigator.appVersion.split('MSIE');

		// Parse the string for the "6" in 6.0
		ieVer=parseInt(temp[1]);

		// Is it less than 7?
		if (ieVer < 7)
			return true;
		else
			return false;
	}
	else
		return false;
}
/* END bad browser check */

function toggleSaveRemove(vin)
{
	if (document.getElementById('save'+vin).style.display!='none')
	{
	document.getElementById('save'+vin).style.display='none';
	document.getElementById('delete'+vin).style.display='block';
	}
	else
	{
	document.getElementById('save'+vin).style.display='block';
	document.getElementById('delete'+vin).style.display='none';
	}
}


/* Window Boundary object*/  
// Object to hold Window Boundary values 
var WindowBounds = new Object();
    WindowBounds.PageWidth  = 0;
    WindowBounds.PageHeight = 0;
    WindowBounds.VisibleTop = 0;
    WindowBounds.VisibleLeft = 0;
    WindowBounds.VisibleWidth  = 0;
    WindowBounds.VisibleHeight = 0;
        
// function to calculate window boundary object values
function GetWindowBounds()
{
    if( window.innerHeight && window.scrollMaxY ) // Firefox 
	{
		WindowBounds.PageWidth = window.innerWidth + window.scrollMaxX;
		WindowBounds.PageHeight = window.innerHeight + window.scrollMaxY;
	}
	else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
	{
		WindowBounds.PageWidth = document.body.scrollWidth;
		WindowBounds.PageHeight = document.body.scrollHeight;
	}
	else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
	{ 
		WindowBounds.PageWidth = document.body.offsetWidth + document.body.offsetLeft; 
		WindowBounds.PageHeight = document.body.offsetHeight + document.body.offsetTop; 
	}

    if (document.documentElement && document.documentElement.scrollTop)
    {
        WindowBounds.VisibleTop      = document.documentElement.scrollTop;
        WindowBounds.VisibleLeft     = document.documentElement.scrollLeft;
        WindowBounds.VisibleWidth    = document.documentElement.clientWidth;
        WindowBounds.VisibleHeight   = document.documentElement.clientHeight;
    } 
    else if (document.body)
    {
        WindowBounds.VisibleTop      = document.body.scrollTop;
        WindowBounds.VisibleLeft     = document.body.scrollLeft;
		WindowBounds.VisibleWidth    = document.body.clientWidth;
        WindowBounds.VisibleHeight   = document.body.clientHeight;
    } 
    else
    {
        WindowBounds.VisibleTop      		= window.pageYOffset;
        WindowBounds.VisibleLeft     		= window.pageXOffset;	
        WindowBounds.VisibleWidth    		= window.innerWidth;
        WindowBounds.VisibleHeight   		= window.innerHeight;
    }
	
	if (navigator.userAgent.indexOf('Safari') > -1)
	{
		WindowBounds.VisibleHeight = window.innerHeight;
	}
   	return WindowBounds;
    
}

/* END Window Boundary object*/



/* Simple JavaScript string trim function */
function Trim(s) {
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) {
    s = s.substring(1,s.length);
  }

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')) {
    s = s.substring(0,s.length-1);
  }
  return s;
}
/* END Simple JavaScript string trim function */


/* Simple JavaScript div show/hide toggle */
function toggleDiv(id)
{
    if (document.getElementById(id).style.display == "block")
        document.getElementById(id).style.display = "none";
    else
        document.getElementById(id).style.display = "block";
        
}
function toggleDivWithImage(id, imgId)
{
	
    if (document.getElementById(id).style.display == "block")
    {
        document.getElementById(id).style.display = "none";
        document.getElementById(imgId).src="images/minus2plus.gif";
	}
    else
    {
        document.getElementById(id).style.display = "block";
        document.getElementById(imgId).src="images/plus2minus.gif";
	}
}
/* END Simple JavaScript div show/hide toggle */



/* Dynamic select options */
function setOptionsWithValues(id, options, values)
{
	clearOptions(id);
	for (i=0; i<options.length; i++)
	{
		appendOptionLastWithValue(id, options[i], values[i]);
	}

}
function setOptions(id, options)
{
	clearOptions(id);
	for (i=0; i<options.length; i++)
	{
		appendOptionLast(id, options[i]);
	}
}
function appendOptionLastWithOptionId(id, option, optionId)
{
	var elOptNew = document.createElement('option');
	elOptNew.text = option;
	elOptNew.value = option;
	elOptNew.id = optionId;
	var elSel = document.getElementById(id);
	if (elSel!=undefined)
	{
		try 
		{
			elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
		}
		catch(ex) 
		{
			elSel.add(elOptNew); // IE only
		}
	}
}
function appendOptionLastWithValue(id, option, value)
{
	var elOptNew = document.createElement('option');
	elOptNew.text = option;
	elOptNew.value = value;
	var elSel = document.getElementById(id);
	if (elSel!=undefined)
	{
		try 
		{
			elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
		}
		catch(ex) 
		{
			elSel.add(elOptNew); // IE only
		}
	}
}
function appendOptionLast(id, option)
{
	appendOptionLastWithValue(id, option, option);
}
function clearOptions(id)
{
	var elSel = document.getElementById(id);
	if (elSel!=undefined)
	{
		var i;
		for (i = elSel.length - 1; i>=0; i--) 
		{
			elSel.remove(i);
		}
	}
}
/* END Dynamic select options  */


/* execute the text in an input field by id */
function execInputString(id)
{
	var code = document.getElementById(id).value;
	eval(code);
}
/* END execute the text in an input field by id */
// Gets the Char Code of the pressed key ...
function getKey(e)
{ 
    var charCode
    if(e && e.which){
        e = e
        charCode = e.which
    }
    else{
        e = event
        charCode = e.keyCode 
    }
	return charCode
}

//checks if the parameter is enter 
function isKeyEnter(charCode)
{ 
    return (charCode == 13) ? true : false;
}

//checks if the parameter is a number
function isKeyNumber(charCode)
{
	return ((charCode >= 48) && (charCode <= 57)) ? true : false;
}

//SCROLLING DIVS

var gabrielsTimer;

function moveDivHorizontal(Direction, innerDivId, contentWidth, outerWidth, stepSpeed, overflowFn, movedFn) 
{
	var obj = document.getElementById(innerDivId);
	var newVal = parseInt(obj.style.left) + (stepSpeed*Direction);
	obj.style.left = ((newVal > 0) ? '0' : ((newVal < outerWidth - contentWidth) ? parseInt(outerWidth - contentWidth) : newVal)) + 'px' ;
	if ((Direction < 0 && newVal < outerWidth - contentWidth) || (Direction > 0 && newVal > 0))
		eval(overflowFn);
	else
		eval(movedFn);
	gabrielsTimer = setTimeout('moveDivHorizontal(' + Direction + ', "' + innerDivId + '", ' + contentWidth + ', ' + outerWidth + ', ' + stepSpeed + ', "' + overflowFn + '", "' + movedFn + '");', 50);
}

function moveDivHorizontalNo() 
{
	clearTimeout(gabrielsTimer);
}

// END OF SCROLLING DIVS


// SORT A TABLE
function sortTable(e, staticRowsCount, asc, columnType, toggle, evalIfAsc, evalIfDesc)
{
	//this function needs at least 1 title row that will be static (staticRow)
	//to be able to perform the sorting.
	var tableArray = new Array()
	if (!e) e=window.event;
	if (!e) return;
	var objCell = e.srcElement?e.srcElement:e.target;
	while (objCell && (objCell.tagName != 'TD') && (objCell.tagName != 'TH'))
		objCell = objCell.parentNode;
	var objTable = objCell.parentNode;
	while (objTable && (objTable.tagName != 'TABLE'))
		objTable = objTable.parentNode;
	var cellIndex = 0;
	while (objTable.rows[0].cells[cellIndex] != objCell)
		cellIndex++;
		
	//copy table content into array
	var tableArray = new Array();
	var i;
	var value;
	if (objTable.rows.length - staticRowsCount <= 1)
		return;//no sorting for 1 row
	for(i=0; i<objTable.rows.length - staticRowsCount; i++) 
		tableArray[i] = new Array();
	for(i=0; i<objTable.rows[0].cells.length; i++)
	{
		for (j=staticRowsCount; j<objTable.rows.length; j++)
		{
			value = objTable.rows[j].cells[i].innerHTML.replace(/<\/?[^>]+>/gi,'');
			tableArray[j - staticRowsCount][i] = value; 
			
			tableArray[j - staticRowsCount][i + objTable.rows[0].cells.length] = objTable.rows[j].cells[i].innerHTML;
		}
	}
	
	//Reset objToggles
	var auxToggle;
	for (i=0; i<objTable.rows[0].cells.length; i++)
	{
		if (i != cellIndex)
		{
			auxToggle = document.getElementById('_sort_' + objTable.id + '_toggle_' + i);
			if (auxToggle) 
				auxToggle.value = '';
		}
	}

	//reset sort indicator image management
	var auxIndicator
	for (i=0; i<objTable.rows[0].cells.length; i++)
	{
		auxIndicator = document.getElementById('_sort_' + objTable.id + '_ind_' + i);
		if (auxIndicator)
			auxIndicator.innerHTML = '';
	}
	
	//save new obj toggle
	var objToggle = document.getElementById('_sort_' + objTable.id + '_toggle_' + cellIndex);
	
	//save new sort indicator
	var objIndicator = document.getElementById('_sort_' + objTable.id + '_ind_' + cellIndex); 
	var ascStr = '&#9650;';
	var descStr = '&#9660';
	if (!objIndicator)
	{
		objCell.innerHTML = '<span id="_sort_' + objTable.id + '_ind_' + cellIndex + '"></span>&nbsp;' + objCell.innerHTML;
		objIndicator = document.getElementById('_sort_' + objTable.id + '_ind_' + cellIndex);
	}
	
	//toggle management and sorting
	//columnType is the type of data present in the column to be sorted.
	//n = number
	//c = char
	//s = string
	//d = date //not treated yet ////
	if (toggle && objToggle && (objToggle.value != '')) 
	{
		tableArray.reverse();
		objIndicator.innerHTML = (objToggle.value == 'asc') ? descStr : ascStr; 
	}
	else
	{
		if (asc) 
		{
			switch (columnType)
			{
			   case 'n'://number: float and integer
			    tableArray.sort(function (a,b) {return ((parseFloat(a[cellIndex]) < parseFloat(b[cellIndex]))? -1 : ((parseFloat(a[cellIndex]) == parseFloat(b[cellIndex]))?0:1));});
			    break;
			   case 'c'://char
			   case 's'://string
			   default:
				tableArray.sort(function (a,b) {return ((a[cellIndex] < b[cellIndex])? -1 : ((a[cellIndex] == b[cellIndex])?0:1));});
				break;
			}
			if (evalIfAsc && evalIfAsc != '') eval(evalIfAsc);
			objIndicator.innerHTML = ascStr; 
		}
		else 
		{
			switch (columnType)
			{
			   case 'n'://number: float and integer
			    tableArray.sort(function (a,b) {return ((parseFloat(a[cellIndex]) < parseFloat(b[cellIndex]))? 1 : ((parseFloat(a[cellIndex]) == parseFloat(b[cellIndex]))?0:-1));});
			    break;
			   case 'c'://char
			   case 's'://string
			   default:
				tableArray.sort(function (a,b) {return ((a[cellIndex] < b[cellIndex])? 1 : ((a[cellIndex] == b[cellIndex])?0:-1));});
				break;
			}
			if (evalIfDesc && evalIfDesc != '') eval(evalIfDesc);
			objIndicator.innerHTML = descStr; 
		}
	}
	
	//Save Toggle
	if (toggle)
	{
		if (!objToggle)
			objCell.innerHTML += '<input type="hidden" id="_sort_' + objTable.id + '_toggle_' + cellIndex + '" value="' + ((asc) ? 'asc' : 'desc') + '">'; 			
		else
			objToggle.value = (objToggle.value == 'asc') ? 'desc' : 'asc';
	}
	
	//copy new table html
	for(i=0; i<objTable.rows[0].cells.length; i++)
	{
		for (j=staticRowsCount; j<objTable.rows.length; j++)
		{
			objTable.rows[j].cells[i].innerHTML = tableArray[j - staticRowsCount][i + objTable.rows[0].cells.length];
		}
	}
}

// END OF SORT A TABLE


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
