﻿// JScript File

function CallServer(Argument)
{
	// to take advantage of this function, you must drag a LinkButton control on the form.
	// Remove any text and leave the button titled 'LinkButton1'  It will not be visible
	// because there is no text.  With that control on the form, you are guaranteed to have
	// asp.net create the __doPostBack function as well as have an object associated with 
	// 'Control' that we hardcode in here as the _EVENTTARGET.  Whatever you pass in as the 
	// 'Argument' parameter of this function will be accessible through the request parameter
	//  Request("__EVENTARGUMENT")
	
	__doPostBack('Control',Argument);
	window.event.cancelBubble = true;
	window.event.returnValue = false;
}

function ChangeAll(Identifier, State)
{
	for (var n=0; n < document.forms[0].length; n++)
	{
		if ((document.forms[0].elements[n].type=='checkbox') && ((document.forms[0].elements[n].id.indexOf(Identifier) >= 0) || (document.forms[0].elements[n].value.indexOf(Identifier) >= 0)) && (document.forms[0].elements[n].disabled==false))
			document.forms[0].elements[n].checked=State;
	}
}

function SetAttribute(Identifier, Attribute, Value)
{
	for (var n=0; n < document.forms[0].length; n++)
	{
		if (document.forms[0].elements[n].id.indexOf(Identifier) >= 0)
		{
			document.forms[0].elements[n].setAttribute(Attribute,Value);
		}
	}
}

function ChangeAllCombos(Identifier, Index)
{
	for (var n=0; n < document.getElementsByTagName('select').length; n++)
	{
		document.getElementsByTagName('select')[n].selectedIndex=Index;
	}
}

function ShowHideAvailabilityLegend()
{
	if (document.getElementById('pnlAvailability').style.visibility=='visible') 
	{
		document.getElementById('pnlAvailability').style.visibility='hidden';
		document.getElementById('lnkAvailability').innerHTML = "» View Availability Legend";
	}
	else 
	{
		document.getElementById('pnlAvailability').style.visibility='visible';
		document.getElementById('lnkAvailability').innerHTML = "» Hide Availability Legend";
	}
}

function ShowHideScanLegend()
{
	if (document.getElementById('pnlScanPrint').style.visibility=='visible') 
	{
		document.getElementById('pnlScanPrint').style.visibility='hidden';
		document.getElementById('lnkScanPrint').innerHTML = "» View Scanned Ticket Legend";
	}
	else 
	{
		document.getElementById('pnlScanPrint').style.visibility='visible';
		document.getElementById('lnkScanPrint').innerHTML = "» Hide Scanned Ticket Legend";
	}
}

function ShowEditWindow(PageName, WindowName)
{
	return window.open(PageName, WindowName, 'status=yes,toolbars=no,scrollbars=yes,resizable=yes,width=800,height=800').focus();void 0;
}

function hideCalendar(oCalendar)
{
	oCalendar.hide();
	oCalendar.get_element().blur();
}

function ParseCardSwipe(scanText, CardName, CardNumber, CardMonth, CardYear, CardList, CardListCount)
{

	if (scanText.substring(0,1) != "%")
		return;
	
	if ((!(scanText)) || (scanText.length == 0) || (scanText.indexOf('^') <=0))
	{
		alert('Card read error!\n\nPlease Try Again.'); 
		CardName.value = "";
		CardNumber.value = "";
		CardMonth.options[0].selected = true;
		CardYear.options[0].selected = true;
		for (var i=0; i < CardListCount; i++)
		{
			document.getElementById(CardList.id + "_" + i).checked = false;
		}
		CardName.focus();
		return;
	}
	
	// the value for scanText will be in the format of:
	// {credit card number}^{last name}/{first name}^{expire year}{expire month}
	
	// so now we parse...
	
	var arText = scanText.split("^");
	var ccNum = TrimString(arText[0].replace('%B','').replace('%b',''));
	var name = arText[1];
	
	var arName = name.split("/");
	var firstName = TrimString(arName[1]);
	var lastName = TrimString(arName[0]);
	
	var expireDate = arText[2];
	var expireYear = '20' + arText[2].substring(0,2);
	var expireMonth = arText[2].substring(2,4);
	
	CardName.value = firstName + " " + lastName;
	CardNumber.value = ccNum;
	
		
	for(var i = 0; i < CardMonth.options.length; i++)
	{
		var cmbValue = CardMonth.options[i].value;
		if (cmbValue.length == 1)
			cmbValue = '0' + cmbValue;
		
		if(cmbValue == expireMonth)
			CardMonth.selectedIndex = i;
	}
	
	for(var i = 0; i < CardYear.options.length; i++)
	{
		if(CardYear.options[i].value == expireYear)
			CardYear.selectedIndex = i;
	}
	
	var firstDigit = ccNum.substring(0,1);
	var CardType;
	
	switch (firstDigit)
	{
		case '3':	CardType = "AMERICAN EXPRESS";break;
		case '4':	CardType = "VISA";break;
		case '5':	CardType = "MASTERCARD";break;
		case '6':	CardType = "DISCOVER";break;
		default:	alert('Invalid Card Scanned.');break;
	}
	
	for (var i=0; i < CardListCount; i++)
	{
		var arValue = document.getElementById(CardList.id + "_" + i).value.split("|");

		if (TrimString(arValue[2].toUpperCase()) == CardType)
		{
			document.getElementById(CardList.id + "_" + i).checked = true;
			break;
		}
	}
	
	
}

function TrimString(strInput) 
{
  strInput = strInput.replace( /^\s+/g, "" );	// strip leading
  return strInput.replace( /\s+$/g, "" );		// strip trailing
}

function EnterScan(txt, e)
{

	if (!e) e = window.event;
	
	var code;
	
	if (e.keyCode) code = e.keyCode;
	if (e.which) code = e.which;
	if (e.charCode) code = e.charCode;
	
	if (code == 13)
	{
		txt.blur();
		e.returnValue = false;
		e.cancel=true;
	}
}	

function PopupPic(sPicURL) { 
	window.open("ImageDisplay.aspx?img="+sPicURL, "", "resizable=1,HEIGHT=200,WIDTH=200");void 0;
}

function TabKey(txt, e)
{
    if (!e) e = window.event;
	
	var code;
	
	if (e.keyCode) code = e.keyCode;
	if (e.which) code = e.which;
	if (e.charCode) code = e.charCode;
	
	if (code == 9)
    {
        txt.selection = document.selection.createRange();
        txt.selection.text = String.fromCharCode(9);
        e.returnValue = false;
        e.cancel = true;
    }
}

function RecalculateMultipay(fullAmount, amountPaid, selectedAmount, targetSuffix)
{
    var result = fullAmount - amountPaid - selectedAmount;
    document.getElementById('spn_' + targetSuffix).innerHTML = "$" + result.toFixed(2);
	document.getElementById('hdn_' + targetSuffix).value =  result.toFixed(2);
	
	//var paidBalance = fullAmount-result;
	
	UpdateMultipayImage(fullAmount, targetSuffix);

}


function UpdateMultipayImage(fullAmount, targetSuffix)
{	

	
	var workingHeight = 73;
	
	if ((document.getElementById('img_' + targetSuffix + '_top')) && (document.getElementById('img_' + targetSuffix + '_top')) &&
		(document.getElementById('img_' + targetSuffix + '_empty')) && (document.getElementById('img_' + targetSuffix + '_full')) &&
		(document.getElementById('hdn_' + targetSuffix)) )
	{
	
		var unpaidBalance = document.getElementById('hdn_' + targetSuffix).value.replace(',','');
		
		var paidBalance = fullAmount-unpaidBalance;
	
		if (paidBalance == fullAmount)
			{document.getElementById('img_' + targetSuffix + '_top').src = '/resources/4.0/images/multipay_topfull.gif';}
		else
			{document.getElementById('img_' + targetSuffix + '_top').src = '/resources/4.0/images/multipay_topempty.gif';}
		
		
		var normalized = parseInt((paidBalance/fullAmount) * workingHeight);
			
		document.getElementById('img_' + targetSuffix + '_empty').style.height = workingHeight-normalized + 'px';
		document.getElementById('img_' + targetSuffix + '_full').style.height = normalized + 'px';	
	}

}

function HideOtherMultipay(all, thisone)
{
	for (var i = 0; i < all.length; i++)
	{
//		document.getElementById(all[i]).className = 'MultipayContainer_hidden';
//		document.getElementById(thisone).className = 'MultipayContainer';

		document.getElementById(all[i]).style.display = 'none';
		document.getElementById(thisone).style.display = 'block';
	}
}