
var intMaxCartSize	= 25;
var strSelectedIDs;
var intCartSize;

//*************************************************************************
function fnSubmitCart(objForm)
{
 	intCartSize = objForm.cart_count.value;
 	strCartValues = objForm.cart_values.value;
 	//alert ('cart size:' + intCartSize);	
 	//alert ('cart values:' + strCartValues);
	if (intCartSize < 1)
	{
		alert ('You have not selected any items yet!');
	}
	else
	{
		document.cookie = ('ShopCart=' + (strCartValues) + '; path=/;');
		//alert ('cookie saved:' + getCookie('ShopCart'));
		window.location.href = '/view/page/investor/923';		
	}
	
}
//*************************************************************************

//*************************************************************************
function fnSetInternalUserCookie()
{
 	document.cookie = ('internal_user=yes; path=/;');
		//alert ('cookie saved:' + getCookie('ShopCart'));
}
//*************************************************************************

//*************************************************************************
function fnRefineSearch(objForm)
{
 	strCartValues = objForm.cart_values.value; 		
	document.cookie = ('ShopCart=' + (strCartValues) + '; path=/;');
	//alert ('cookie saved:' + getCookie('ShopCart'));
	window.location.href = '/view/page/investor/414';	
}
//*************************************************************************


//*************************************************************************
function fnGoToMainPageWithIds(objForm)
{
 	strSelectedIDs = objForm.cart_values.value;
	document.cookie = 'ShopCart=' + (strSelectedIDs) + '; path=/;';	
	window.location.href = '/view/page/investor/414';	
}
//*************************************************************************

//*************************************************************************
function fnClearCart()
{
 	strSelectedIDs = '';
	document.cookie = 'ShopCart=' + (strSelectedIDs) + '; path=/;';		
}
//*************************************************************************



//*************************************************************************
function fnOrderDocument(intDocumentID, objForm)
{
	intMaxCartSize	= 25;	
	strSelectedIDs = objForm.cart_values.value;
	intCartSize = objForm.cart_count.value;
	//alert('add:' + intDocumentID);
	if (intCartSize < intMaxCartSize)
	{
		//Add the Document ID to the cookie value, only if it is not already present
		if (strSelectedIDs == '' )
		{
			strSelectedIDs = intDocumentID;		
		}
		else 
		if ( (strSelectedIDs.indexOf(intDocumentID + '|') == (-1)) && (strSelectedIDs.indexOf('|' + intDocumentID) == (-1)) )
		{	
			strSelectedIDs = strSelectedIDs + "|" + intDocumentID;			
		}	
		document.cookie = ('ShopCart=' + (strSelectedIDs) + '; path=/;');
		//alert(getCookie('ShopCart'));
		fnSubmitForm( objForm );
	}
	else
	{
		alert('You can order a maximum of ' + intMaxCartSize + ' documents at a time.  You have currently met this maximum.');
	}

}
//*************************************************************************

//*************************************************************************
function fnDeleteDocument (intDocumentID, objForm)
{	
	//Remove the Document ID from the cookie value
	strSelectedIDs = objForm.cart_values.value;
	//alert('remove:' + intDocumentID);
	if (strSelectedIDs == intDocumentID )
	{
		strSelectedIDs = '';
	}
	else
	{
		strSelectedIDs = strSelectedIDs.replace(('|' + intDocumentID), '');
		strSelectedIDs = strSelectedIDs.replace((intDocumentID + '|'), '');
		
		if (strSelectedIDs.indexOf('|') == (0))
		{
			//Remove the first character if it is a pipe (|)
			strSelectedIDs = strSelectedIDs.substring(1, strSelectedIDs.length)
		}
		if (strSelectedIDs.lastIndexOf('|') == (strSelectedIDs.length - 1))
		{
			//Remove the last character if it is a pipe (|)
			strSelectedIDs = strSelectedIDs.substring(0, strSelectedIDs.length - 1)
		}
	}
				
	document.cookie = 'ShopCart=' + (strSelectedIDs) + '; path=/;';				
	fnSubmitForm( objForm );
	
}

//*************************************************************************


//*************************************************************************
function fnDeleteDocumentFromOrderFormPage (intDocumentID, objForm, strDocumentName)
{	
	if (confirm("Are you sure you want to delete the item:\n" + strDocumentName + "\nfrom you order?"))
	{
		fnDeleteDocument (intDocumentID, objForm);
	}	

}
//*************************************************************************


//*************************************************************************
function fnOrderDocument2(intDocumentID, objForm)
{
	intMaxCartSize	= 25;	
    intCartSize = objForm.cart_count.value;
	
	//alert('intCartSize:' + intCartSize );
	//alert('intMaxCartSize:' + intMaxCartSize);
	
	if (intCartSize < intMaxCartSize)
	{
		objForm.operation.value = 'add';
		objForm.cart_value.value = intDocumentID;	
		fnSubmitForm( objForm );
	}
	else
	{	
		alert('You can order a maximum of ' + intMaxCartSize + ' documents at a time.  You have currently met this maximum.');
	}
	
	
}
//*************************************************************************


//*************************************************************************
function fnDeleteDocument2(intDocumentID, objForm)
{
	objForm.operation.value = 'remove';
	objForm.cart_value.value = intDocumentID;
	//alert(objForm.cart_value.value);
	fnSubmitForm( objForm );
}
//*************************************************************************




//*************************************************************************
function fnClearCart2(objForm)
{

	//If a NULL is passed in, then do not refresh the form.  Just clear the cookie
	
	if ( !( (objForm == undefined) || (objForm == null) || (objForm == '') ) )
	{	
		if (confirm("Are you sure you want to delete all ordered items?"))
		{
			//Remove All Document IDs from the cookie value
			strSelectedIDs = '';
			document.cookie = 'ShopCart=' + (strSelectedIDs) + '; path=/;';	

			fnSubmitForm( objForm );
		}
	}
	else
	{
		//Remove All Document IDs from the cookie value
		strSelectedIDs = '';
		document.cookie = 'ShopCart=' + (strSelectedIDs) + '; path=/;';	
	}

}
//*************************************************************************


//*************************************************************************
function fnSubmitForm(objForm)
{
	objForm.submit();
}
//*************************************************************************



//*************************************************************************
function fnSendFinalOrder(objForm)
{

var blnDataErrorOccured = false;
var strErrorMessage		= '';

	if (objForm.txtFirstName.value == "") 
	{
		strErrorMessage = strErrorMessage + "\nYou must supply a First Name.\n";
		blnDataErrorOccured = true;
	}	
	
	if (objForm.txtLastName.value == "") 
	{
		strErrorMessage = strErrorMessage + "\nYou must supply a Last Name.\n";
		blnDataErrorOccured = true;
	}	
			
	if (objForm.txtFirmName.value == "") 
	{
		strErrorMessage = strErrorMessage + "\nYou must supply a Firm Name.\n";
		blnDataErrorOccured = true;	
	}			
	
	if (objForm.txtAddress1.value == "") 
	{
		strErrorMessage = strErrorMessage + "\nYou must supply an Address.\n";
		blnDataErrorOccured = true;	
	}	
			
	if (objForm.txtCity.value == "") 
	{
		strErrorMessage = strErrorMessage + "\nYou must supply a City.\n";
		blnDataErrorOccured = true;	
	}	
	
	var val = objForm.txtState.value;
	if (( val == "") || (val.length != 2)) 
	{
		strErrorMessage = strErrorMessage + "\nYou must supply a valid state abbreviation\n";
		blnDataErrorOccured= true;	
	}		
	
	val = objForm.txtZip.value;
	if (!validZipCode(val)) 
	{
		strErrorMessage = strErrorMessage + "\nYou must supply a valid zip code in the form nnnnn or nnnnn-nnnn\n";
		blnDataErrorOccured = true;	
	}

	val = objForm.txtPhone.value;
	if ((val.length != 0) && !validPhoneNum(val)) {
		strErrorMessage = strErrorMessage + "\nA phone number is not required, however, if you choose to\n" +
		      "supply one, it must be in the format nnn-nnn-nnnn\n";
		blnDataErrorOccured = true;	
	}
			 
	if (blnDataErrorOccured == true)
	{
		alert(strErrorMessage);
	}		
	else
	{	
		strSelectedIDs = objForm.cart_values.value;
		document.cookie = 'ShopCart=' + (strSelectedIDs) + '; path=/;';		
		//alert(getCookie('ShopCart'));
		objForm.elements['submitted'].value = '1';
		fnSubmitForm(objForm);
	}
}
//*************************************************************************


//*************************************************************************
function validPhoneNum(value)
{
	if (value.length != 12) 
		{return false;}

	for (i = 0; i < value.length; i++)
	{
		if ((i == 3) || (i == 7))
		{
			if (value.charAt(i) != '-') {return false;}	
		} 
		else if (! validInteger(value.charAt(i), 0, 9)) 
		{
			return false;		
		}
	}

	return true;
}
//*************************************************************************


//*************************************************************************
function validZipCode(value)
{
   if ((value.length != 5) && (value.length != 10)) {return false;}

	for (i = 0; i < value.length; i++)
	{
		if (i == 5)
		{
			if (value.charAt(i) != '-') 
			{
				return false;
			}	
		} 
		else if (! validInteger(value.charAt(i), 0, 9)) 
		{
			return false;		
		}
	}

	return true;
}
//*************************************************************************


//*************************************************************************
function validInteger(IntStr, MinValue, MaxValue)
{
	var WorkStr = new String(IntStr);
	
	if (WorkStr.length == 0) return false;
	
	if (isNaN(IntStr)) return false;
	
	var IntValue = parseInt(IntStr, 10);
	if (MaxValue < MinValue) return true;
	return ((IntValue >= MinValue) && (IntValue <= MaxValue));
}
//*************************************************************************



function getCookie(NameOfCookie)
{
	if (document.cookie.length > 0) 
	{ 
		begin = document.cookie.indexOf(NameOfCookie+"="); 
		if (begin != -1) 
		{ 
			begin += NameOfCookie.length+1; 
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); } 
	}
	return null; 
}

