//misc
var preloadFlag = false;

//FUNCTIONS START
function changeImages()	{
	if (document.images && (preloadFlag == true)){
		for (var i=0; i<changeImages.arguments.length; i+=2){
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function imgOn (imgName){
	if (document.images)
		document[imgName].src = eval(imgName + "on.src");
}

function imgOff (imgName){
	if (document.images)
		document[imgName].src = eval(imgName + "off.src");
}

function SubmitTopNav(form)
{
	var url = form.selContactLens.options[form.selContactLens.selectedIndex].value;
	if (url.length > 0) {
		document.location.href= url;		//form.submit();
	}
}

function setDefaultFocus()
{
	if (!document.getElementById) return;
	if (!document.forms[0]) return;
	
	try
	{
		for(var f = 0; f < document.forms.length; ++f)
		{
			var frm = document.forms[f];
			
				for(var e = 0; e < frm.elements.length; ++e)
				{
					var fld = frm.elements[e];
					if(fld.type == "hidden")
						continue;
					else if(fld.tabIndex <= 0)
						continue;
					else if(fld.getAttribute("nodefaultfocus") != null)
						continue;
					else if(fld.value.length >0)
						continue;
					else
					{
						fld.focus();
						return;
					}
				}
		}
	}
	catch(ex)
	{
		return;
	}
}

function areTheseElementsFilled()
{
	for(var i=0; i<arguments.length; ++i)
	{
		var arg = arguments[i];
		if(arg.value.length == 0)
		{
			var szMsg = arg.getAttribute("empty_msg");
			if(szMsg == null)
				szMsg = arg.form.getAttribute("empty_msg");
			if(szMsg != null) alert(szMsg);
			arg.focus();
			return false;
		}
	}
	return true;
}

// ####################################
// SHOPPING CART...
// ####################################
function submitCartPlaceOrder(p_szDitac, p_szTrx) {
	document.frmRecalc.ditac.value = p_szDitac;
	document.frmRecalc.trx.value = p_szTrx;

	document.frmRecalc.submit();
}

// ####################################
// FIND MY DOCTOR FORM....
// ####################################

// note: these can and should be expanded for a subsequent feature to do more than simple bool for empty values

function submitDoctorInfo() {
	if (formElementIsEmpty(document.CLPINFO.txtPatientName_0) || formElementIsEmpty(document.CLPINFO.txtDoctorName_0) || formElementIsEmpty(document.CLPINFO.txtDoctorPhone_0)) {
		alert('To submit your information, you must provide your full name, doctor or store name, and phone. Please enter them and try again.');
	}
	else {
		document.CLPINFO.fSubmitDoctorInfo.value = 'True';
		document.CLPINFO.submit();
	}
}

function submitDoctorSearch() {
	if (formElementIsEmpty(document.CLPINFO.SearchCity) || formElementIsEmpty(document.CLPINFO.SearchState)) {
		alert('To search, you must provide a city and state. Please enter them and try again.');
	}
	else {
		document.CLPINFO.fSearchForDoctor.value = 'True';
		document.CLPINFO.submit();
	}
}

function formElementIsEmpty(p_oElement) {
	var l_f = true;
	if (p_oElement != null) {
		var l_szValue = Trim(p_oElement.value);
		if (l_szValue.length > 0) {
			l_f = false;
		}
	}
	return l_f;
}

// for applying the doctor information from the previous page to the current page
function copyPreviousDrInfo() {
	document.CLPINFO.txtDoctorName_0.value = document.CLPINFO.txtPreviousDoctorName.value;
	document.CLPINFO.txtDoctorPhone_0.value = document.CLPINFO.txtPreviousDoctorPhone.value;
}

// for selecting the informaton from the Doctor Search results
function updateParent(p_szName, p_szPhone) {
	document.CLPINFO.txtDoctorName_0.value = p_szName;
	document.CLPINFO.txtDoctorPhone_0.value = p_szPhone;

	var l_checkboxSameDoc = document.CLPINFO.cbPrevDr;
	if (l_checkboxSameDoc != null) {
		l_checkboxSameDoc.checked = false;
	}

	// reload the page to the "Find My Doctor" anchor
	document.location = document.location.toString();
}

// STRING TRIMMING FUNCIONS (http://www.apriori-it.co.uk/Trim.asp)
function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1) {
		return "";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if (TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}
}
function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0) {
		return "";
	}
	var iTemp = v_length -1;

	while (iTemp > -1) {
		if (VALUE.charAt(iTemp) == w_space) {
		}
		else {
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	}
	return strTemp;
}
function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if (v_length < 1){
		return "";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;
	while(iTemp < v_length) {
		if (VALUE.charAt(iTemp) == w_space) {
		}
		else {
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	}
	return strTemp;
}

// <Bill Me Later functionality>

// Select Credit Card as the payment type
function selectPaymentTypeCC(p_nTypeCC)
{
	document.getElementById('paymentmethod').value = p_nTypeCC;

	// reset the credit card fields - there is the case where the customer hits the back button and could see the BML info
	if (document.getElementById('txtCCNumber').value == '5049990000000000')
	{
		var l_oCCTypes = document.getElementById('selCCType');
		var l_oCCOption;
		for(var i = 0; i < l_oCCTypes.childNodes.length; i++)
		{
			l_oCCOption = l_oCCTypes.childNodes[i];
			if (i == 0)
			{
				l_oCCOption.setAttribute('selected', 'selected');
			}
			else
			{
				l_oCCOption.setAttribute('selected', '');
			}
		}

		setDropdownValue('selCCExpMonth', 0);								// The first month from the month dropdown
		setDropdownValue('selCCExpYear', 0);								// The first year from the expiration year dropdown

		document.getElementById('txtCCNumber').value = '';
		document.getElementById('txtCCNameOnCard').value = '';
	}

	// show credit card fields
	toggleVisibleContent('dvCreditcard', true);

	// show the billing address form
	toggleVisibleContent('dvBillAddress', true);

	// hide BML
	toggleVisibleContent('dvBML', false);
}

// Select Bill Me Later as the payment type
function selectPaymentTypeBML(p_nTypeBML)
{
	document.getElementById('paymentmethod').value = p_nTypeBML;

	// hide credit card fields
	toggleVisibleContent('dvCreditcard', false);

	// show the billing address form
	toggleVisibleContent('dvBillAddress', true);

	// show BML
	toggleVisibleContent('dvBML', true);
}

// when customer submits payment information
function doPaymentSubmit(p_oForm)
{
	// required for Address Verification Service (AVS), these forms need this value set to 'true'...
	var l_oAVS;
	for (var k = 0; k < document.forms.length; k++)
	{
		l_oAVS = document.forms[k].jsenabled;
		if (l_oAVS != null)
		{
			l_oAVS.value = 'true';
		}
	}

	var l_fBMLChecked = false;
	for (i=0; i < p_oForm.paymentmethod.length; i++)
	{
		if (p_oForm.paymentmethod[i].checked)
		{
			switch(p_oForm.paymentmethod[i].value)
			{
				case '11': 
					l_fBMLChecked = true;
			}
		}
	}

	// For Bill Me Later, update the credit card fields appropriately.
	// Note: this has to be done on submit, because doing earlier risks getting wiped out by Google toolbar and the like...
	if (l_fBMLChecked)
	{
		// Deselect whatever credit card type they may have previously selected...
		var l_oCCTypes = document.getElementById('selCCType');
		var l_oCCOption;
		for(var i = 0; i < l_oCCTypes.childNodes.length; i++)
		{
			l_oCCOption = l_oCCTypes.childNodes[i];
			l_oCCOption.setAttribute('selected', '');
		}

		// Add Bill Me Later as a credit card type to the dropdown, and select it...
		var optionBML = document.createElement('option');
		optionBML.setAttribute('value','11');
		optionBML.setAttribute('selected', 'selected');
		optionBML.innerHTML = 'Bill Me Later'
		l_oCCTypes.appendChild(optionBML);

		// Select the first month from the month dropdown, and the last year from the expiration year dropdown...
		setDropdownValue('selCCExpMonth', 0);								
		setDropdownValue('selCCExpYear', -1);

		// Enter the official Bill Me Later credit card number...
		document.getElementById('txtCCNumber').value = '5049990000000000';

		// Enter the official name on the Bill Me Later credit card...
		document.getElementById('txtCCNameOnCard').value = 'Bill Me Later';
	}
}

// hide or unhide a given block of content
function toggleVisibleContent(p_szID, p_fShow)
{
	var l_oElement = document.getElementById(p_szID);
	
	if (l_oElement != null)
	{
		if (p_fShow == true)
		{
			l_oElement.style.visibility = 'visible';
			l_oElement.style.display = 'block';
		}
		else
		{
			l_oElement.style.visibility = 'hidden';
			l_oElement.style.display = 'none';
		}
	}
}

//***********************************************************************************************************
//Function Name	:	getDropdownValue
//Purpose		:	To retrieve either the first or the last value in a dropdown form field.
//Prameters		:	p_szElementID	- the element ID
//					p_nOrdinal		- if -1, get the last; otherwise get the value of this ordinal
//***********************************************************************************************************
// TODO -- 1. verify that it IS a dropdown, 2. verify the length
function getDropdownValue(p_szElementID, p_nOrdinal)
{
	var l_sz;
	l_sz = '';

	var l_oElement;	
	l_oElement = document.getElementById(p_szElementID)

	if (l_oElement != null)
	{
		if (p_nOrdinal >= 0)
		{
			l_sz = l_oElement.options[p_nOrdinal].value;
		}
		else
		{
			l_sz = l_oElement.options[l_oElement.length - 1].value;
		}
	}

	return l_sz;
}

function setDropdownValue(p_szElementID, p_nOrdinal)
{
	var l_oElement;
	l_oElement = document.getElementById(p_szElementID)

	if (l_oElement != null)
	{
		l_oElement.value = getDropdownValue(p_szElementID, p_nOrdinal);
	}
}

// IVD has different forms surrounding the place order buttin, so we need to capture the user selection and pass through as a hidden field...
function handle90DayCheckboxOnPlaceOrder(p_oCheckbox)
{
	var l_oHiddenField;
	for (var k = 0; k < document.forms.length; k++)
	{
		l_oHiddenField = document.forms[k].elements[p_oCheckbox.name];
		if (l_oHiddenField != null)
		{
			if (l_oHiddenField.uniqueID != p_oCheckbox.uniqueID)
			{
				if (p_oCheckbox.checked == true)
				{
					l_oHiddenField.value = 'on';
				}
				else
				{
					l_oHiddenField.value = '';
				}
			}
		}
	}

}
// </Bill Me Later functionality>
