/*
     Copyright 2005 Speartek, Inc.

     All rights reserved.

     All materials contained in this file are protected by United States copyright law
     and may not be reproduced, distributed, transmitted, displayed, published or
     broadcast without the prior written permission of Speartek, Inc.
     You may not alter or remove this notice.
*/
var eGift = 0;
var eRcpt = 1;
function ClickCancel(Destination,isTop){
	//if a cancel button is clicked do this.
	//isTop is optional.	
	if (Destination.length > 0 && confirm('Press \'OK\' to Cancel')){								
		if (arguments[1])
			top.location.href = Destination;							
		else
			location.href = Destination;	
	}
}//end of ClickCancel
function jsTrim(inFieldValue)
{				
	return inFieldValue.trim();
}//end of jsTrim
function NumericCheck(inForm,inElement)
{
	var re = /\,|\$|\./gi;
	var formItem,formItem;
	var i,returnNum;	
	formItem = (GetVal(inForm, inElement)); 

	formItemre = formItem.replace(re, "");
	//check to see if number is numeric or base 10
	if ( (parseInt(formItemre,10) !=formItemre) && formItemre != "" ) {
		alert(inElement + " must be numeric");
		return false; 
	}	
	
	re = /\,|\$/gi;
	formItemre = formItem.replace(re,"")
	formItemre = formItemre.split(".");
	if (formItemre.length > 2 ) {
		alert(inElement + " has too many decimal points");
		return false;		
	}
	returnNum = "";
	for(i=0;i<formItemre.length;i++){		
		if (i>0){returnNum = returnNum + '.'}
		returnNum = returnNum + formItemre[i];
	}
	
	SetVal(inForm,inElement,returnNum);
	return true;		
}//end of NumericCheck()

function drawGroups()
{
	var strHTML = '';
	for(var i = 0; i < GROUPS_TYPES.length; i++)
	{
		strHTML = strHTML + '<table width="50%" border="0" cellspacing="0" cellpadding="4" class="TableWithBorder"><tbody><tr><td class="Primary" style="text-align: left;"><span name="GroupsType' + GROUPS_TYPES[i][1].toString() + '" id="GroupsType' + GROUPS_TYPES[i][1].toString() + '"><b>' + GROUPS_TYPES[i][0] + ' Groups</b><br />';
		for(var j = 0; j < aryGroups.length; j++)
		{
			if(aryGroups[j][1] == GROUPS_TYPES[i][1])
			{
				strHTML = strHTML + '<input name="Groups" id="Groups" type="checkbox" value="' + aryGroups[j][0] + ':' + aryGroups[j][1] + '" />' + aryGroups[j][2] + '<br />';
			}
		}
		strHTML = strHTML +  '</span></tbody><table>';
	}
	GetTag('GroupsTD').innerHTML = strHTML;
	selectGroups();
}

function displayGroups()
{
	var selectedType = GetTag('GroupsTypes').options[GetTag('GroupsTypes').selectedIndex].value;
	for(var i = 0; i < GROUPS_TYPES.length; i++)
	{
		NoDisplay('GroupsType' + GROUPS_TYPES[i][1].toString());
	}
	
	if(parseInt(selectedType) > 0)
	{	
		Display(GetTag('GroupsType' + selectedType.toString()));
	}
}

function selectGroups()
{
	var aryCheckbox = document.getElementsByName('Groups');

	for(var j = 0; j < aryCheckbox.length; j++)
	{
		for(var i = 0; i < arySelectedGroups.length; i++)
		{
			if(aryCheckbox[j].value == arySelectedGroups[i])
			{
				aryCheckbox[j].checked = true;
			}
		}
	}
}

function isPublicCheck()
{
	var isChecked = false;
	var aryViewGroups = GetTags('GroupsType' + tGetTypeID(GROUPS_TYPES, 'Public').toString());
	for(var i=0; i<aryViewGroups.length; i++)
	{
		var aryInputs = aryViewGroups[i].getElementsByTagName('input');
		for(var j=0; i<aryInputs.length; i++)
		{
			if(aryInputs[i].checked) isChecked = true;
		}
	}
	return isChecked;
}

//Functions Used on the public trans checkout
function CheckCustomerQuestions()
{
	var DocT = document.transaction;

	if (DocT.CustomerQuestion1A != null)
	{
		if (jsTrim(DocT.CustomerQuestion1A.value).length == 0)
		{
			alert('The Question \n\n' + DocT.CustomerQuestion1Q.value + '\n\nis blank');
			return false;
		}
	}
	if (DocT.CustomerQuestion2A != null)
	{
		if (jsTrim(DocT.CustomerQuestion2A.value).length == 0)
		{
			alert('The Question \n\n' + DocT.CustomerQuestion2Q.value + '\n\nis blank');
			return false;
		}
	}
	return true;
}

function IsValidZipCode()
{
	var blnIsNumber = true;
	var regZipTest5 = /\d{5}/;
	var regZipTest9 = /\d{5}-\d{4}/;
	var myForm = document.transaction
	var ZipNumber = myForm.BillingAddressZipcode.value;
	var Alert = '';
	var strSelectedCtry = myForm.BillingAddressCountry.options[myForm.BillingAddressCountry.selectedIndex].value.toLowerCase();
	window.status = ZipNumber.length == 10 && strSelectedCtry == 'us';
	switch(true)
	{
		case !regZipTest5.test(ZipNumber) && strSelectedCtry == 'us' && ZipNumber.length == 5:
			alert('Billing ZIP Code must be 5 or 9 digit');
			myForm.BillingAddressZipcode.focus();
			return false;
			break;
		case !regZipTest9.test(ZipNumber) && strSelectedCtry == 'us' && ZipNumber.length == 10:
			alert('Billing ZIP Code must be 5 or 9 digit');
			myForm.BillingAddressZipcode.focus();
			return false;
			break;
		case strSelectedCtry == 'us' && ZipNumber.length != 5 && ZipNumber.length != 10:
			alert('Billing ZIP Code must be 5 or 9 digits.');
			myForm.BillingAddressZipcode.focus();
			return false;
			break;
	}
	
	if(myForm.ChangeAddress.selectedIndex == 1)
	{
		ZipNumber = myForm.ShippingAddressZipcode.value;
		strSelectedCtry = myForm.ShippingAddressCountry.options[myForm.ShippingAddressCountry.selectedIndex].value.toLowerCase();
		switch(true)
		{
			case !regZipTest5.test(ZipNumber) && strSelectedCtry == 'us' && ZipNumber.length == 5:
				alert('Shipping ZIP Code must be 5 or 9 digits');
			myForm.ShippingAddressZipcode.focus();
				return false;
				break;
			case !regZipTest9.test(ZipNumber) && strSelectedCtry == 'us' && ZipNumber.length == 10:
				alert('Shipping ZIP Code must be 5 or 9 digits');
			myForm.ShippingAddressZipcode.focus();
				return false;
				break;
			case strSelectedCtry == 'us' && ZipNumber.length != 5 && ZipNumber.length != 10:
				alert('Shipping ZIP Code must be 5 or 9 digits.');
			myForm.ShippingAddressZipcode.focus();
				return false;
				break;
		}
	}
	return true;

}

function ShowShipping()
{
	switch(document.transaction.ChangeAddress.selectedIndex)
	{
	case 0:
		NoDisplay('ShippingTR');
		break;
	
	case 1:
		Display('ShippingTR');
		break;
	
	//if no Index is passed, shipping should display (preserve custom script functionality)
	default:
		if(document.transaction.ChangeAddress.selectedIndex)
		{
			NoDisplay('ShippingTR');
		}else
		{
			//if no Index is passed, shipping should display (preserve custom script functionality)
			Display('ShippingTR');
		}
		break;
	}
}

function DrawCertificates(intCount)
{
	var i;
	for(i=5; i>intCount; i--)
	{
		//alert('GiftCertificateNumTR' + i)
		NoDisplay('GiftCertificateNumTR' + i);
		NoDisplay('GiftCertificateCodeTR' + i);
	}
	for(i=1; i<=intCount; i++)
	{
		Display('GiftCertificateNumTR' + i);
		Display('GiftCertificateCodeTR' + i);
	}
}



function CheckCertificates()
{
	var NumCerts = document.transaction.NumCerts.options[document.transaction.NumCerts.selectedIndex].value;
	var CertName = '';
	var CertCode = '';
	var Alert = '';
	var Status = true;
	
	for(var i=1; i<=NumCerts; i++)
	{
		CertName = GetVal('transaction', 'CertificateNumber' + i);
		CertCode = GetVal('transaction', 'CertCode' + i);
		for(var j=i+1; j<=NumCerts; j++)
		{
			if( (GetVal('transaction', 'CertificateNumber' + j) == CertName) && (GetVal('transaction', 'CertCode' + j) == CertCode) )
			{
				Alert = Alert + 'You have entered the Certificate Number ' + CertName + ' with the Certificate Code ' + CertCode + ' multiple times, please remove one.\n';
				Status = false;
			}
		}
	}

	if(Status)
	{
		document.transaction.BillingPersonFirst.disabled = false;
		document.transaction.BillingPersonLast.disabled = false;
		return true;
	}
	else
	{
		alert(Alert);
		return false;
	}
}

//JS Cart Prototypes and functions
function JSCart(Parent, Name, OrderNum, Subtotal, Shipping, Tax, CertBalance, PromoAmt, Total)
{
	this.Parent = Parent;
	this.OrderNum = OrderNum;
	this.Subtotal = Subtotal;
	this.Shipping = Shipping;
	this.Tax = Tax;
	this.CertBalance = CertBalance;
	this.PromoAmt = PromoAmt;
	this.Total = Total;
	this.Products = new Array();
	this.BillingAddress = new Array();
	this.ShippingAddress = new Array();
	this.AbsoluteObjectName = Name;
	this.TotalPayments = (typeof(arguments[9]) != 'undefined')?arguments[9]:'$0.00';
	this.BalanceDue = (typeof(arguments[10]) != 'undefined')?arguments[10]:false;
	this.hideZeroShip = (typeof(arguments[11]) != 'undefined')?arguments[11]:false;
	this.hideZeroTax = (typeof(arguments[12]) != 'undefined')?arguments[12]:false;
}

JSCart.prototype.AddProduct = function(ObjectID, Quantity, Name, Price, ExtendedPrice, Taxable, Weight, PartNumber, Description, SKU)
{
	this.Products.push(new Product('Products[' + this.Products.length + ']', this, ObjectID, Quantity, Name, Price, ExtendedPrice, Taxable, Weight, PartNumber, Description, SKU));
}

JSCart.prototype.AddShippingAddress = function(objDef)
{
	this.ShippingAddress.push(objDef);
}

JSCart.prototype.AddBillingAddress = function(objDef)
{
	this.BillingAddress.push(objDef);
}

function Product(ABSName, Parent, ObjectID, Quantity, Name, Price, ExtendedPrice, Taxable, Weight, PartNumber, Description, SKU)
{
	this.Parent = Parent;
	this.AbsoluteObjectName = ABSName;
	this.ObjectID = ObjectID;
	this.Quantity = Quantity;
	this.Name = Name;
	this.Price = Price;
	this.ExtendedPrice = ExtendedPrice;
	this.Taxable = Taxable;
	this.Weight = Weight;
	this.PartNumber = PartNumber;
	this.Description = Description;
	this.SKU = SKU
	this.Modifiers = new Array();
	
}

Product.prototype.AddModifier = function(ObjectID, Quantity, Name, Price, ExtendedPrice, Taxable, Weight, PartNumber, Description, SKU)
{
	this.Modifiers.push(new Modifier('QtyPriceGroups[' + this.QtyPriceGroups.length + ']', this, ObjectID, Quantity, Name, Price, ExtendedPrice, Taxable, Weight, PartNumber, Description, SKU));
}
function Modifier(ABSName, Parent, ObjectID, Quantity, Name, Price, ExtendedPrice, Taxable, Weight, PartNumber, Description, SKU)
{
	this.Parent = Parent;
	this.AbsoluteObjectName = ABSName;
	this.ObjectID = ObjectID;
	this.Quantity = Quantity;
	this.Name = Name;
	this.Price = Price;
	this.ExtendedPrice = ExtendedPrice;
	this.Taxable = Taxable;
	this.Weight = Weight;
	this.PartNumber = PartNumber;
	this.Description = Description;
	this.SKU = SKU
}
//end js Cart prototypes and functions
function ToggleRcpt(oCart, tagDisplay, eRcptType, sURL)
{
	var aryHTML = new Array();
	aryHTML[aryHTML.length] = '<table width="100%" border="0" cellpadding="2" cellspacing="0" class="TableWithBorder">';
	aryHTML[aryHTML.length] = '<tr><td><div id="ReceiptHead"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td style="text-align: left;"><b>' + (parseInt(eRcptType) == eGift?' Gift Receipt':' Sales Receipt') + ' for ' + sURL + '</b></td><td style="text-align: right;"><b>Order number ' + oCart.OrderNum + '</td></b></tr></table></div></td></tr>';
	aryHTML[aryHTML.length] = '<tr><td colspan="2" style="text-align: center;"><div id="ReceiptBody" align="center"><table width="90%" border="0" cellpadding="2" cellspacing="0">';
	aryHTML[aryHTML.length] = '<tr><td class="Secondary" style="text-align: center;"><b>Qty.</b></td><td class="Secondary" style="text-align: left;"><b>Product</b></td><td class="Secondary" style="text-align: left;"><b>Description</b></td>' + (parseInt(eRcptType) == eRcpt?'<td class="Secondary" style="text-align: center;"><b>Price</b></td>':'') + '</tr>';
	for(var i=0; i<oCart.Products.length; i++)
	{
		aryHTML[aryHTML.length] = '<tr><td class="PrimaryBoldText" style="text-align: center; vertical-align: top;">' + oCart.Products[i].Quantity + '</td><td class="PrimaryBoldText" style="text-align: left; vertical-align: top;">' + oCart.Products[i].SKU + '</td><td class="PrimaryBoldText" style="text-align: left;">' + oCart.Products[i].Name + '</td>' + (parseInt(eRcptType) == eRcpt?'<td class="PrimaryBoldText" style="text-align: center;">' + oCart.Products[i].ExtendedPrice + '</td>':'') + '</tr>';
	}
	if(parseInt(eRcptType) == eRcpt)
	{
		aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="4">&nbsp;</td></tr>';
		aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="4"><div id="ReceiptSubtotal" align="right"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td class="Primary" style="text-align: right; width: 90%;"><b>Subtotal:</b></td><td class="Primary">' + oCart.Subtotal + '</td></tr></table></div></td></tr>';
		if(oCart.Shipping != '$0.00' || !oCart.hideZeroShip) aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="4"><div id="ReceiptShipping" align="right"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td class="Primary" style="text-align: right; width: 90%;"><b>Shipping & Handling:</b></td><td class="Primary">' + oCart.Shipping + '</td></tr></table></div></td></tr>';
		if(oCart.Tax != '$0.00' || !oCart.hideZeroTax) aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="4"><div id="ReceiptTax" align="right"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td class="Primary" style="text-align: right; width: 90%;"><b>Tax:</b></td><td class="Primary">' + oCart.Tax + '</td></tr></table></div></td></tr>';
		if(oCart.CertBalance != '$0.00') aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="4" align="right"><div id="ReceiptCert"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td class="Primary" style="text-align: right; width: 90%;"><b>Certificate Money Applied:</b></td><td class="Primary">' + oCart.CertBalance + '</td></tr></table></div></td></tr>';
		if(oCart.PromoAmt != '$0.00') aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="4" align="right"><div id="ReceiptPromo"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td class="Primary" style="text-align: right; width: 90%;"><b>Promotional Discounts Applied:</b></td><td class="Primary">' + oCart.PromoAmt + '</td></tr></table></div></td></tr>';
		aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="4"><div id="ReceiptTotal" align="right"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td class="Primary" style="text-align: right; width: 90%;"><b>Total:</b></td><td class="Primary">' + oCart.Total + '</td></tr></table></div></td></tr>';
		if(oCart.TotalPayments != '$0.00') aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="4" align="right"><div id="ReceiptPayments"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td class="Primary" style="text-align: right; width: 90%;"><b>Total Payments:</b></td><td class="Primary">' + oCart.TotalPayments + '</td></tr></table></div></td></tr>';
		if(oCart.BalanceDue) aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="4" align="right"><div id="ReceiptBalance"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td class="Primary" style="text-align: right; width: 90%;"><b>Balance Due:</b></td><td class="Primary">' + oCart.BalanceDue + '</td></tr></table></div></td></tr>';
		
	}
	if(typeof(oCart.BillingAddress) != 'undefined' && oCart.BillingAddress.length > 0)
	{
        aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="4"><div id="BillingReceiptAddressDiv" align="right"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td class="Primary" colspan="2">&nbsp;</td></tr>';
        aryHTML[aryHTML.length] = '<tr><td class="Secondary" style="width: 45%;"><b>Billing Information</b></td><td class="Secondary"><span class="NoPrint">Do Not Display this when printing <input type="checkbox" name="noPrintBilling" id="noPrintBilling" value="1" onclick="if(this.checked){document.getElementById(\'BillingReceiptAddressDiv\').className = \'NoPrint\'}else{document.getElementById(\'BillingReceiptAddressDiv\').className = \'\'}" /></span></td>';
        for(var i=0; i<oCart.BillingAddress.length; i++)
        {
            if(oCart.BillingAddress[i]._CompanyName.length > 0)
                aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.BillingAddress[i]._CompanyName + '</b></td></tr>';
            aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.BillingAddress[i]._FirstName + ' ' + oCart.BillingAddress[i]._LastName + '</b></td></tr>';
            aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.BillingAddress[i]._Address1 + '</b></td></tr>';
            if(oCart.BillingAddress[i]._Address2.length > 0)
                aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.BillingAddress[i]._Address2 + '</b></td></tr>';
            if(oCart.BillingAddress[i]._Address3.length > 0)
                aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.BillingAddress[i]._Address3 + '</b></td></tr>';
            aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.BillingAddress[i]._City + ', ' + oCart.BillingAddress[i]._State + ' ' + oCart.BillingAddress[i]._PostalCode + ' ' + oCart.BillingAddress[i]._Country + '</b></td></tr>';
        }
        aryHTML[aryHTML.length] = '</tbody></table></div></td></tr>';
	}
	if(typeof(oCart.ShippingAddress) != 'undefined' && oCart.ShippingAddress.length > 0)
	{
        aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="4"><div id="ShippingReceiptAddressDiv" align="right"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td class="Primary" colspan="2">&nbsp;</td></tr>';
        aryHTML[aryHTML.length] = '<tr><td class="Secondary" style="width: 45%;"><b>Shipping Information</b></td><td class="Secondary"><span class="NoPrint">Do Not Display this when printing <input type="checkbox" name="noPrintBilling" id="noPrintBilling" value="1" onclick="if(this.checked){document.getElementById(\'ShippingReceiptAddressDiv\').className = \'NoPrint\'}else{document.getElementById(\'ShippingReceiptAddressDiv\').className = \'\'}" /></span></td>';
        for(var i=0; i<oCart.ShippingAddress.length; i++)
        {
            if(oCart.ShippingAddress[i]._CompanyName.length > 0)
                aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.ShippingAddress[i]._CompanyName + '</b></td></tr>';
            aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.ShippingAddress[i]._FirstName + ' ' + oCart.ShippingAddress[i]._LastName + '</b></td></tr>';
            aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.ShippingAddress[i]._Address1 + '</b></td></tr>';
            if(oCart.ShippingAddress[i]._Address2.length > 0)
                aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.ShippingAddress[i]._Address2 + '</b></td></tr>';
            if(oCart.ShippingAddress[i]._Address3.length > 0)
                aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.ShippingAddress[i]._Address3 + '</b></td></tr>';
            aryHTML[aryHTML.length] = '<tr><td class="Primary" colspan="2"><b>' + oCart.ShippingAddress[i]._City + ', ' + oCart.ShippingAddress[i]._State + ' ' + oCart.ShippingAddress[i]._PostalCode + ' ' + oCart.ShippingAddress[i]._Country + '</b></td></tr>';
        }
        aryHTML[aryHTML.length] = '</tbody></table></div></td></tr>';
	}
	aryHTML[aryHTML.length] = '</table></td></tr><tr><td colspan="2" style="text-align: center;"><div class="noprint"><input type="button" name="printGift" id="printGift" value="Print Receipt" class="FormButtons" onclick="window.print();" />  <input type="button" name="showGift" id="showGift" value="Close Window" class="FormButtons" onclick="window.close();" /></div></td></tr></table>';
	GetTag(tagDisplay).innerHTML = aryHTML.join('');
}
//End Functions Used on the public trans checkout
function DisableClicks(tags)
{
	for(var i=0; i<arguments.length; i++)
		GetTag(arguments[i]).disabled = true;
}

function disableButtons(formObj)
{
	aryTags = GetTagsByName('input')
	for(i=0; i<aryTags.length; i++)
	{
		switch(aryTags[i].type.toUpperCase())
		{
			case 'SUBMIT':
			case 'BUTTON':
				aryTags[i].disabled = true;
				break;
		}
	}
	return true;
}

function luhnCheck(vCCNum)
{
    vCCNum = vCCNum.replace(/[^\d]/g, '');
    if (!this.isCCNum(vCCNum))
    {
        return false;
    }
    var no_digit = vCCNum.length;
    var oddoeven = no_digit & 1;
    var sum = 0;
    for (var count = 0; count < no_digit; count++)
    {
        var digit = parseInt(vCCNum.charAt(count));
        if (!((count & 1) ^ oddoeven))
        {
            digit *= 2;
            if (digit > 9)
                digit -= 9;
        }
        sum += digit;
    }
    return (sum % 10 == 0)?true:false;
}

function isCCNum(sVal)
{
    sVal = sVal.toString();
    var CCTest = /[\d]/g;
    if (sVal.length == 0)
        return false;
    return CCTest.test(sVal);
}
