function pageResize(href)
{
	var pageSize = document.all["PageSize"].value;
	if (pageSize == null || pageSize == "")
	{
		pageSize = 10;
	}
	var newhref = href + "&PageSize=" + pageSize
	document.location.href = newhref;
	return false; 
}

function toggleChildren(td, childId)
{
	var tr = document.getElementById(childId);
	var display = tr.style.display;
	
	if (display == "none")
	{
		tr.style.display = "inline";
		td.innerHTML = "<img src='../images/minus_icon.gif'>"
	}
	else
	{
		tr.style.display = "none";
		td.innerHTML = "<img src='../images/plus_icon.gif'>"
	}
}

function displayError(msg, el)
	{
	msg = "You must " + msg + " before continuing. Please try again."
	alert(msg)
	if (el != null) el.focus()
	return false
	}
	
function displayErrorES(msg, el)
	{
	msg = "Usted debe " + msg + " antes de continuar. Por favor hágalo de nuevo."
	alert(msg)
	if (el != null) el.focus()
	return false
	}
/*-------------------------------------------------
CUSTOM VALIDATOR FUNCTIONS
-------------------------------------------------*/

function ValidateInteger(source, arguments)
{
    var obj = document.getElementById(source.controltovalidate);
    obj.value = obj.value.replace(/,/gi, "");
    arguments.Value = arguments.Value.replace(/,/gi, ""); 
	arguments.IsValid = validInteger(arguments.Value);
}

function ValidateNumber(source, arguments)
{
	arguments.IsValid = validNumber(arguments.Value);
}

function ValidateDate(source, arguments)
	{
	var Test = new Date(arguments.Value)
	arguments.IsValid = ! isNaN(Test)
	}

function ValidateEmail(source, arguments)
	{
	arguments.IsValid = validEmail(arguments.Value);
	}
	
function ValidateUrl(source, arguments)
{
	RE = /^https?:\/\//i
	el = document.getElementById(source.controltovalidate);
	if (! RE.test(arguments.Value)) el.value = "http://" + arguments.Value;
	RE = /^https?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/
	arguments.IsValid = RE.test(el.value);
}

function ValidateZipCode(source, arguments)
{
	arguments.IsValid = validZipCode(arguments.Value);
}

function ValidateFutureDate(source, arguments)
{
	Date1 = new Date(arguments.Value);
	Date2 = new Date();
	if (Date1 > Date2)
		arguments.IsValid = true;
	else
		arguments.IsValid = false;
}

function ValidateTime(source, arguments)
{
	RE = /^(1[0-2]|[1-9])\:[0-5][0-9] ?(a|p)m$/i
	arguments.IsValid = RE.test(arguments.Value);
}

function ValidateUSPhone(source, arguments)
{
	Phone = arguments.Value;
	TmpVal = "";
	for (var i = 0; i < Phone.length; i++)
	{
		if (Phone.charAt(i) >= "0" && Phone.charAt(i) <= "9")
			TmpVal += Phone.charAt(i);
	}
	if (TmpVal.length != 10)
		arguments.IsValid = false;
	else
	{
		Phone = TmpVal.substr(0, 3) + "-" + TmpVal.substr(3, 3) + "-" + TmpVal.substr(6, 4);
		RE = /^[1-9][0-9]{2}\-[1-9][0-9]{2}\-[0-9]{4}$/
		if (RE.test(Phone))
		{
			document.forms[0].elements[source.controltovalidate].value = Phone;
			arguments.IsValid = true;
		}
		else
			arguments.IsValid = false;
	}
}
	
function ValidateEmailArray(source, arguments)
	{
	var isValid = true;
	aryStr = arguments.Value.split(",")
	for (var i = 0; i < aryStr.length; i++)
		{
		if (! validEmail(Trim(aryStr[i])))
			{
			isValid = false
			break
			}
		}
	arguments.IsValid = isValid;
	}
	
function ValidateVIN(source, arguments)
{
	arguments.IsValid = validVIN(arguments.Value);
}

function ValidateImageExtension(source, arguments)
{
	arguments.IsValid = validImageExtension(arguments.Value);
}

function ValidateImageExtensionWithFlash(source, arguments)
{
	arguments.IsValid = validImageExtension(arguments.Value, true);
}

function ValidatePDFExtension(source, arguments)
{
	arguments.IsValid = validPDFExtension(arguments.Value);
}

function ValidateDocumentExtension(source, arguments)
{ 
	if (arguments.Value != "")
	{
		arguments.IsValid  = validDocumentExtension(arguments.Value);
	}
}

function VerifyPhones(source, arguments)
{
	form = document.Form1;
	arguments.IsValid = ! (form.DayPhone.value == "" && form.EveningPhone.value == "");
}

/*-------------------------------------------------
REGULAR EXPRESSION AND OTHER VALIDATORS
-------------------------------------------------*/
function validYear(val)
{
	RE = /^\d{4}$/
	if (! RE.test(val))
		return false
	else
	{
		if (val < 1900 || val > 2100)
			return false
	}
	return true
}
	
function validInteger(val)
{
	RE = /^\-?\d+$/
	return RE.test(val)
}
	
function validNumber(val)
{
	//RE = /^(\d{1,3}\,?(\d{3}\,?)*\d{3}(\.\d{0,2})?|\d{1,3}(\.\d{0,2})?|\.\d{1,2}?)$/
	RE = /^(\d{1,3}\,?(\d{3}\,?)*\d{3}(\.\d+)?|\d{1,3}(\.\d+)?|\.\d+)$/
	return RE.test(val)
}
	
function validDate(el)
{
	tmpDate = new Date(el.value)
	if (isNaN(tmpDate))
		return false
	else
	{
		Mo = tmpDate.getMonth() + 1
		if (Mo < 10) Mo = "0" + Mo.toString()
		Da = tmpDate.getDate()
		if (Da < 10) Da = "0" + Da.toString()
		Yr = tmpDate.getFullYear()
		if (Yr < 1950) Yr += 100
		el.value = Mo + "/" + Da + "/" + Yr
		return true
	}
}
	
function validEmail(val)
{
	RE = /^[A-Z0-9\.\_\-]+@([A-Z0-9\.\-]+\.)+[A-Z0-9\.\-]{2,4}$/i
	return RE.test(val)
}
	
function validEmailArray(Str)
{
	aryStr = Str.split(",")
	for (i = 0; i < aryStr.length; i++)
		{
		if (! validEmail(Trim(aryStr[i])))
			return false
		}
	return true
}
	
function validZipCode(val)
{
	RE = /^[0-9]{5}(\-[0-9]{4})?$/
	return RE.test(val)
}
	
function validUrl(val)
{
	RE = /^https?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/
	return RE.test(val);
}

function validVIN(val)
{
	RE = /^[0-9a-z]{17}$/i
	return RE.test(val);
}

function validImageExtension(val, allowFlash)
{
	if (allowFlash == true)
		RE = /\.(jpg)|(gif)|(swf)$/i
	else
		RE = /\.(jpg)|(gif)$/i
		
	return RE.test(val);
}

function validPDFExtension(val)
{
	RE = /\.pdf$/i
	return RE.test(val);
}

function validDocumentExtension(val)
{  
	if (val != "")
	{
		RE = /\.(doc)|(pdf)$/i
		return RE.test(val);
	}
}

function validHexColor(val)
{
	RE = /^\#[0-9a-f]{6}$/i;
	return RE.test(val);
}
	
function testLength(el, len)
	{
	val = el.value
	if (val.length > len)
		{
		msg = "Your input exceeds the " + len + " character limit for this field. "
			+ "Please adjust your input and try again."
		alert(msg)
		el.focus()
		return false
		}
	return true
	}
	
function DayName(DayVal)
	{
	switch (DayVal)
		{
		case 0:
			return "Sunday"
			break
		case 1:
			return "Monday"
			break
		case 2:
			return "Tuesday"
			break
		case 3:
			return "Wednesday"
			break
		case 4:
			return "Thursday"
			break
		case 5:
			return "Friday"
			break
		case 6:
			return "Saturday"
			break
		}
	}
	
function MonthName(MonthVal)
	{
	switch (MonthVal)
		{
		case 0:
			return "January"
			break
		case 1:
			return "February"
			break
		case 2:
			return "March"
			break
		case 3:
			return "April"
			break
		case 4:
			return "May"
			break
		case 5:
			return "June"
			break
		case 6:
			return "July"
			break
		case 7:
			return "August"
			break
		case 8:
			return "September"
			break
		case 9:
			return "October"
			break
		case 10:
			return "November"
			break
		case 11:
			return "December"
			break
		}
	}

function FormatTime(DateVal)
	{
	AP = "AM"
	Hr = DateVal.getHours()
	if (Hr >= 12) 
		{
		Hr -= 12
		AP = "PM"
		}
	if (Hr < 10) Hr = "0" + Hr
	Min = DateVal.getMinutes()
	if (Min < 10) Min = "0" + Min
	return Hr + ":" + Min + " " + AP
	}
	
function IsFutureDate(uDate)
{
	Now = new Date();
	UserDate = new Date(uDate);
	if (UserDate > Now)
		return true
	else
		return false
}

function Trim(Str)
	{
	Str = LeftTrim(Str)
	return RightTrim(Str)
	}

function LeftTrim(Str)
	{
	for (i = 0; i < Str.length; i++)
		{
		if (Str.charAt(i) != " ")
			break
		}
	return Str.substring(i, Str.length)
	}
	
function RightTrim(Str)
	{
	for (i = Str.length - 1; i >= 0; i--)
		{
		if (Str.charAt(i) != " ")
			break
		}
	return Str.substring(0, i + 1)
	}

function ClearList(list)
{
	for (i = list.options.length - 1; i > 0; i--)
	{
		list.options[i] = null;
	}
}

function showCalendar(el)
	{
	window.dateField = el
	calendar = window.open('/Lib/Includes/calendar.htm', 'cal', 'WIDTH=200,HEIGHT=250')
	}
	
function DateAdd(Interval, Offset, DateVal)
	{
	tmpDate = new Date(DateVal)
	if (isNaN(tmpDate))
		{
		msg = "Type mismatch: GLOBAL.JS: DateAdd."
		alert(msg)
		return null
		}
	else
		{
		switch (Interval.toLowerCase())
			{
			case "y" || "yy" || "yyyy":
				tmpDate.setFullYear(tmpDate.getFullYear() + Offset)
				break
			case "m" || "mm":
				tmpDate.setMonth(tmpDate.getMonth() + Offset)
				break
			case "w" || "wk":
				tmpDate.setDay(tmpDate.getDay() + Offset)
				break
			case "d" || "dd":
				tmpDate.setDate(tmpDate.getDate() + Offset)
				break
			case "h" || "hh":
				tmpDate.setHours(tmpDate.getHours() + Offset)
				break
			case "n" || "mi":
				tmpDate.setMinutes(tmpDate.getMinutes() + Offset)
				break
			case "s" || "ss":
				tmpDate.setSeconds(tmpDate.getSeconds() + Offset)
				break
			default:
				msg = "Invalid Interval: GLOBAL.JS: DateAdd."
				alert(msg)
				return null
			}
		return tmpDate
		}
	}
	
function formatShortDate(DateVal)
	{
	tmpDate = new Date(DateVal)
	if (isNaN(tmpDate))
		{
		msg = "Type mismatch: GLOBAL.JS: formatShortDate."
		alert(msg)
		return null
		}
	else
		{
		MM = tmpDate.getMonth() + 1
		DD = tmpDate.getDate()
		YY = tmpDate.getFullYear()
		return MM + "/" + DD + "/" + YY
		}
	}

function GetObjectTop(el)
{
	var _top = 0;
	while (el.offsetParent)
	{
		_top += el.offsetTop;
		el = el.offsetParent;
	}
	return _top;
}

function GetObjectLeft(el)
{
	var _left = 0;
	while (el.offsetParent)
	{
		_left += el.offsetLeft;
		el = el.offsetParent;
	}
	return _left;
}
	
/*-------------------------------------------------
SHOW PROMPT

Shows a Prompt box containing the specified message
and returns the user's input. RetVal will either be
a string or a null (if Cancel was clicked).
-------------------------------------------------*/
function showPrompt(msg, deflt, maxLen)
	{
	retVal = prompt(msg, deflt)
	if (retVal == "")
		{
		mMsg = "You must enter a value and then click the OK button. To exit without "
			+ "entering a value, click the Cancel button."
		alert(mMsg)
		showPrompt(msg, deflt)
		}
	else if (retVal != null && retVal.length > maxLen)
		{
		tmpVal = retVal.substring(0, maxLen)
		mMsg = "The maximum length for this value is " + maxLen + " characters. Your "
			+ "input exceeds this limit. To automatically truncate your input to the "
			+ "proper length, i.e. \"" + tmpVal + "\", click OK. To return and shorten "
			+ "your input, click Cancel."
		if (confirm(mMsg))
			retVal = tmpVal
		else
			showPrompt(msg, retVal, maxLen)
		}
	return(retVal)
	}
	
var nWin = null

/*-------------------------------------------------
SHOW POP UP

This function generates a new pop-up window based
on the referenced arguments. If any pop-up window
is currently open, it closes that window and opens
the requested window.
-------------------------------------------------*/

var nWin = null

function ShowPopUp(URL, Name, Height, Width, Top, Left, Status, Toolbar, Menubar, Location, Resizable, Scrollbars)
{
	if (nWin == null)
	{
		nWin = window.open(URL, Name, "height=" + Height + ",width=" + Width 
			+ ",top=" + Top + ",left=" + Left + ",status=" + Status 
			+ ",toolbar=" + Toolbar + ",menubar=" + Menubar
			+ ",location=" + Location + ",resizable=" + Resizable + ",scrollbars=" + Scrollbars)
	}
	else
	{
		CloseNWin()
		ShowPopUp(URL, Name, Height, Width, Top, Left, Status, Toolbar, Menubar, Location, Resizable, Scrollbars)
	}	
}
	
function CenterHorz(width)
{
	return(parseInt(window.screenLeft + ((GetWindowWidth() - width) / 2)))
}
	
function CenterVert(height)
{
	//25 is the apprx. size of the title bar which is not part of the pop-up height
	return(parseInt(window.screenTop + ((GetWindowHeight() - (height + 25)) / 2)))
}
	
function CloseNWin()
{
   if (nWin != null)
    {
      nWin.close()
      nWin = null
    }
}
   
function CloseWindow()
{
	opener.nWin = null
	window.close()
}

function GetWindowHeight()
{
	var height = 0;
	
	if (typeof(window.innerHeight) != "undefined")
		height = window.innerHeight;
	else if (document.documentElement.clientHeight > 0)
		height = document.documentElement.clientHeight;
	else
		height = document.body.clientHeight;
		
	return height;
}

function GetWindowWidth()
{
	var width = 0;
	
	if (typeof(window.innerWidth) != "undefined")
		width = window.innerWidth;
	else if (document.documentElement.clientWidth > 0)
		width = document.documentElement.clientWidth;
	else
		width = document.body.clientWidth;
		
	return width;
}


/**********************************************
INVENTORY SEARCH LIST FUNCTIONS
**********************************************/
var origClass = "";

function InventoryRowMouseOver(el)
{
	origClass = el.className;
	el.className = "RollOverHighlight";
	var vin = el.getAttribute("Vin")
	if (eval("Photo_" + vin + ".src") != "")
		ShowInventoryPhoto(el, eval("Photo_" + vin))
}

function InventoryRowMouseOut(el)
{
	el.className = origClass;
	HideInventoryPhoto(el);
}

function ShowInventoryPhoto(el, PhotoSrc)
{

	var pic = document.getElementById("PhotoDiv");
	var picHtml = "";
	if (PhotoSrc.indexOf("NoPhoto") > -1)
	{
		picHtml = "<div class=\"NoPhoto\">No Photo<br>Available</div>"
		pic.style.backgroundColor = "#CCCCCC";
		pic.style.height = "85px";
	}
	else
	{
		picHtml =  PhotoSrc;
		pic.style.height = "85px";
	}
	 
	pic.innerHTML = picHtml;
	pic.style.left = GetPhotoLeft(el) + 3 + "px";
	pic.style.top = GetObjectTop(el) - 15 + "px";
	pic.style.display = "block";
}

function GetPhotoLeft(el)
{
	var _left = 0;
	while (el.offsetParent)
	{
		if (el.className == "ListTable") _left += el.offsetWidth;
		_left += el.offsetLeft;
		el = el.offsetParent;
	}
	return _left;
}
	
function HideInventoryPhoto(el)
{
	pic = document.getElementById("PhotoDiv");
	pic.style.display = "none";
}

function FadeImage(id, opacityStart, opacityEnd, milliseconds)
{
	var speed = Math.round(milliseconds / 100);
	var timer = 0;
	
	if (opacityStart > opacityEnd)
	{
		for (var i = opacityStart; i >= opacityEnd; i--)
		{
			setTimeout("SetOpacity(" + i + ", '" + id + "')", (timer * speed));
			timer++;
		}
	}
	
	if (opacityStart < opacityEnd)
	{
		for (var i = opacityStart; i <= opacityEnd; i++)
		{
			setTimeout("SetOpacity(" + i + ", '" + id + "')", (timer * speed));
			timer++;
		}
	}
}

function SetOpacity(opacity, id)
{
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function GetCookie(keyName)
{
	var _cookies = document.cookie.split(";");
	var _value = null;
	for (var i = 0; i < _cookies.length; i++)
	{
		if (_cookies[i].indexOf(keyName) > -1)
		{
			_split = _cookies[i].split("=");
			if (_split.length = 2)
				_value = Trim(_split[1]);
			break
		}
	}
	return _value;
}

