/************************************************************************************************************************************/
/**************************************************** STARTS: GLOBAL VARIABLES ******************************************************/
/************************************************************************************************************************************/

	var secs;											// funciton InitializeTimer()
	var timerID = null;									// funciton InitializeTimer()
	var timerRunning = false;							// funciton InitializeTimer()
	var elementDiv = "divErrMsgJS";
	var delay = 2000;									// funciton InitializeTimer()
	
	
	var FRM_LABELS = [
		'lblEmail',
		'lblFirstName',
		'lblLastName',
		'lblPDAddress',
		'lblPDCity',
		'lblPDState',
		'lblPDZip',
		'lblVarificationCode'
	];
	
	var FRM_CONTROLS = [
		'txtEmail',
		'txtFirstName',
		'txtLastName',
		'txtPDAddress',
		'txtPDCity',
		'cmbPDState',
		'txtPDZip',
		'txtVarificationCode'
	];
	
	var ERR_MESSAGES = [
		'Email is a required field.',
		'First Name is a required field.',
		'Last Name is a required field.',
		'Address is a required field.',
		'City is a required field.',
		'State is a required field.',
		'Zip Code is a required field.',
		'Word Verification is a required field.'
	];
	
/************************************************************************************************************************************/
/**************************************************** END: GLOBAL VARIABLES ******************************************************/
/************************************************************************************************************************************/


/************************************************************************************************************************************/
/************************************************* STARTS: FORM LEVEL VALIDATIONS ***************************************************/
/************************************************************************************************************************************/
	function validate_form(frmLabels, frmControls, errMessages)
	{
		// don't check old browsers on client side
		if (!document.body || !document.body.innerHTML)
			return true;
		
		clear_displayMessage(FRM_LABELS);
		a_errors = [];
	
		count = frmControls.length;
		for (var i = 0; i < count; i++)
		{
			control = document.getElementById(frmControls[i]);
			if (!control.value || control.value==0)		//This checks whether the Control(textbox) contains some value or not
				a_errors[a_errors.length] = {'t': errMessages[i], 'f': frmLabels[i], 'i': frmControls[i]};
			else if (control.id == 'txtEmail' && !isValidEmail(control.value))
				a_errors[a_errors.length] = {'t': 'Invalid Email Address.', 'f': 'lblEmail', 'i': 'txtEmail'};
		}
		if (document.getElementById('divCreditCardDetails').style.display == "inline" && document.getElementById('CC_type').value==0)
			a_errors[a_errors.length] = {'t': 'Please select a valid Card Type.', 'f': 'lblCC_type', 'i': 'CC_type'};
		if (document.getElementById('divCreditCardDetails').style.display == "inline" && !document.getElementById('CC_Number').value)
			a_errors[a_errors.length] = {'t': 'Card Number is a required field.', 'f': 'lblCC_Number', 'i': 'CC_Number'};
	
		if (a_errors.length)							// display error messages
		{
			display_Message_box(a_errors);
			return false;
		}
//		displayStatus('Sending...&nbsp;', 0);
	}
	
	function clear_displayMessage(frmLabels)
	{
		var errorboxes = ['divErrMsgJS', 'divErrMsgPHP'];
		for (var index = 0; index < errorboxes.length; index++)
		{
			e_errbox = get_element(errorboxes[index]);
	
			if (!e_errbox)
				continue;
			
			if (e_errbox && e_errbox.innerHTML)
			{
				e_errbox.style.display = 'none';
				e_errbox.innerHTML = '';
			}
		}
	
		frmLabels[frmLabels.length] = "lblCC_type";
		frmLabels[frmLabels.length] = "lblCC_Number";
		for (var i = 0; i < frmLabels.length; i++)
		{
			e_caption = get_element(frmLabels[i]);
			if (!e_caption)
				continue;
			e_caption.className = 'normal';
		}
	}

	function display_Message_box (a_errors)
	{
		e_errbox = get_element('divErrMsgJS');
		s_message = '';
	
		// set focus
		document.getElementById(a_errors[0]['i']).focus();
	
		s_message = '<table border=0 width=94% cellspacing=0 cellpadding=0><tr><td valign="top" style="padding-top:2px; font-size: 11px; color: red; text-align: left;"><b>Errors: </b></td> <td><ul style="margin-left: 0px; display: inline;">';
		for (n_i = 0; n_i < a_errors.length; n_i++)
		{
			s_message += '<li style="color: red; text-align: left;">' + a_errors[n_i]['t'] + '</li>';
		}
		s_message += '</ul></td></tr></table>';
		
		if (e_errbox && e_errbox.innerHTML != null)
		{
			e_errbox.innerHTML = 
			  '<table cellpadding="0" cellspacing="0" width="80%" style="border:1px #CCCCCC solid;">'
			+ '<tr><td bgcolor="#CCCC33">'
			+ '<table cellpadding="0" cellspacing="0" border="0" width="100%">'
			+ '<tr><td bgcolor="#FFFFCC" style="color: red; padding-left:4px;">'
			+ '<div style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 51px">' + s_message + '</div>'
			+ '</td></tr>'
			+ '</table>'
			+ '</td></tr>'
			+ '</table>';
			e_errbox.style.display = 'inline';
			elementDiv = "divErrMsgJS";
			InitializeTimer();
		}
		for (n_i = 0; n_i < a_errors.length; n_i++)
			if (a_errors[n_i]['f'])
			{
				e_caption = get_element(a_errors[n_i]['f']);
				if (!e_caption)
					continue;
				e_caption.className = 'error';
			}
	}
	
	function get_element(s_id)
	{
		return (document.all ? document.all[s_id] : (document.getElementById ? document.getElementById(s_id) : null));
	}
	
	/** STARTS: Removes the Error Message box from the page after a specific predefined time span **/
	function InitializeTimer()
	{
		secs = 5;											// Set the length of the timer, in seconds
		StopTheClock();
		StartTheTimer();
	}
	
	function StopTheClock()
	{
		if(timerRunning)
			clearTimeout(timerID);
		timerRunning = false;
	}
	
	function StartTheTimer()
	{
		if (secs==0)
		{
			StopTheClock();
			document.getElementById(elementDiv).style.display = 'none';
		}
		else
		{
			// self.status = secs
			secs = secs - 1;
			timerRunning = true;
			timerID = self.setTimeout("StartTheTimer()", delay);
		}
	}
	/** ENDS: Removes the Error Message box from the page after a specific predefined time span */
/************************************************************************************************************************************/
/*************************************************** END: FORM LEVEL VALIDATIONS ****************************************************/
/************************************************************************************************************************************/

/************************************************************************************************************************************/
/*********************************************** START: GENERAL PAGE LEVEL FUNCTIONS ************************************************/
/************************************************************************************************************************************/
	function togglePaymentOptions(e)
	{
		if(e.value == "CC")
		{
			document.getElementById('divCreditCardDetails').style.display = "inline";
			document.getElementById('btnPayment').value = "Make Payment";
		}
		else
		{
			document.getElementById('divCreditCardDetails').style.display = "none";
			document.getElementById('btnPayment').value = "Request Bill";
		}
		
	}
/************************************************************************************************************************************/
/*********************************************** END  : GENERAL PAGE LEVEL FUNCTIONS ************************************************/
/************************************************************************************************************************************/
