/*** JAVASCRIPT - CHECKS & VALIDATIONS FOR PAGE : "admin/users/add"	***/

var FRM_LABELS = [
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	''
	
];

var FRM_CONTROLS = [
	'UserFirstname',
	'UserLastname',
	'UserEmail',
	'UserPhone',
	'UserAddress',
	'UserCity',
	'UserZipcode',
	'UserState',
	'UserUsername'
];

var ERR_MESSAGES = [
	'First Name is required field.',
	'Last Name is required field.',
	'Valid Email is required.',
	'Valid phone number is required.',
	'Valid Address is required.',
	'Valid city name is required.',
	'Valid ZIP code is required',
	'State is required field.',
	'Username is required field.'
	
	
];

// 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_errors(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 == 'UserFirstname' && !isProperText(control))
			a_errors[a_errors.length] = {'t': 'Please input a valid <strong>UserFirstname</strong>.', 'f': '', 'i': 'UserFirstname'};
		else if (control.id == 'UserEmail' && !isValidEmailFormat(control))
			a_errors[a_errors.length] = {'t': 'Please input a valid <strong>Email </strong>.', 'f': '', 'i': 'UserEmail'};
		else if (control.id == 'UserPhone' && !isInteger(control))
			a_errors[a_errors.length] = {'t': 'Please input a valid <strong>Phone number</strong>.', 'f': '', 'i': 'UserPhone'};
		//else if (control.id == 'UserAddress') //&& !isProperText(control)
		//	a_errors[a_errors.length] = {'t': 'Please input a valid <strong>Address</strong>.', 'f': '', 'i': 'UserAddress'};
		else if (control.id == 'UserCity' && !isProperText(control))
			a_errors[a_errors.length] = {'t': 'Please input a valid <strong>City</strong>.', 'f': '', 'i': 'UserCity'};
		else if (control.id == 'UserZipcode' && !isInteger(control))
			a_errors[a_errors.length] = {'t': 'Please input a valid <strong>Zip Code</strong>.', 'f': '', 'i': 'UserZipcode'};
		else if (control.id == 'UserState' && !isProperText(control))
			a_errors[a_errors.length] = {'t': 'Please input a valid <strong>State</strong>.', 'f': '', 'i': 'UserState'};
		//else if (control.id == 'UserUsername') //&& !isProperText(control)
		//	a_errors[a_errors.length] = {'t': 'Please input a valid <strong>Username</strong>.', 'f': '', 'i': 'UserState'};
		
	
	}

	// display error messages
	if (a_errors.length)
	{
		error(a_errors);
		return false;
	}
	return chkPWD(UserPassword,UserConfirmPassword);
	report('Submitted, ','Adding Record to the Database...');
/*	o_form.submit(); 				where o_form was document.formname. which i avoid using. :D **/
}
/*** ENDS::: FORM LEVEL VALIDATIONS ***/


/*** STARTS::: FORM LEVEL VALIDATIONS - FUNCTIONS ***/
function clear_errors(frmLabels)
{
	var errorboxes = ['divMessageJS'];
	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 = '';
		}
	}

	for (var i = 0; i < frmLabels.length; i++)
	{
		e_caption = get_element(frmLabels[i]);
		if (!e_caption)
			continue;
		e_caption.className = 'normal';
	}
}


function error (a_errors)
{
	e_errbox = get_element('divMessageJS');
	s_message = '';

	// set focus
	document.getElementById(a_errors[0]['i']).focus();

	s_message = '<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td valign="top" style="padding-top:2px; font-size: 12px; color: red; text-align: left; letter-spacing:2px;"><b>Instructions: </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="100%">'
		+ '<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%; ">' + s_message + '</div>'
		+ '</td></tr>'
		+ '</table>'
		+ '</td></tr>'
		+ '</table>';
		e_errbox.style.display = 'inline';
	}
	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 report (heading, s_message)
{
	e_msgbox = get_element('divErrMsgJS');
	if (e_msgbox && e_msgbox.innerHTML != null)
	{
		e_msgbox.innerHTML =
		  '<table cellpadding="0" cellspacing="0" border="0" width="100%">'
		+ '<tr><td bgcolor="#CCCC33">'
		+ '<table cellpadding="6" cellspacing="1" border="0" width="100%">'
		+ '<tr><td bgcolor="#FFFFCC" style="font-size: 11.5px; color: green;">'
		+ '<b>' + heading + '</b> <span style="color: black">' + s_message + '</span>'
		+ '</td></tr>'
		+ '</table>'
		+ '</td></tr>'
		+ '<tr><td><img src="/img/pixel.gif" width="1" height="4px" border="0"><br></td></tr>'
		+ '</table>';
		e_msgbox.style.display = 'inline';
	}
}


function get_element (s_id)
{
	return (document.all ? document.all[s_id] : (document.getElementById ? document.getElementById(s_id) : null));
}
// ENDS  : FORM LEVEL VALIDATIONS - FUNCTIONS

// STARTS: following code is to remove the error message which is displayed in the DIV section in .tpl file...
	var secs
	var timerID = null
	var timerRunning = false
	var delay = 1000
	
	function InitializeTimer()
	{
		// Set the length of the timer, in seconds
		secs = 15
		StopTheClock()
		StartTheTimer()
	}
	
	function StopTheClock()
	{
		if(timerRunning)
			clearTimeout(timerID)
		timerRunning = false
	}
	
	function StartTheTimer()
	{
		if (secs==0)
		{
			StopTheClock()
			// Here, we make the DIV:
			document.getElementById("divMessageJS").style.display = 'none';
			document.getElementById("divMessagePHP").style.display = 'none';
		}
		else
		{
	//      self.status = secs
			secs = secs - 1
			timerRunning = true
			timerID = self.setTimeout("StartTheTimer()", delay)
		}
	}
// ENDS  : following code is to remove the error message which is displayed in the DIV section in .tpl file.....