// STARTS: FORM LEVEL VALIDATIONS
function validate_form(frmLabels, frmControls, errMessages)
{
	alert("yesy");
	// don't check old browsers on client side
	if (!document.body || !document.body.innerHTML)
		return true;
	
	clear_errors(FRM_LABELS);
	a_errors = [];
	ctr	="";
	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]};	
			 if(!ctr) ctr  = control;  
			}
	/*	else if (control.id == 'UserFirstname' && !isText(control)){
			a_errors[a_errors.length] = {'t': '-Please input a valid <strong>First Name</strong>(Only Alphabetical characters allowed).', 'f': '', 'i': 'UserFirstname'}; 		 
			if(!ctr) ctr  = control;  }*/
		else if (control.id == 'UserLastname' && !isText(control)){
			a_errors[a_errors.length] = {'t': '-Please input a valid <strong>Contact Last Name</strong>(Only Alphabetical characters allowed).', 'f': '', 'i': 'UserLastname'}; 		 
			if(!ctr) ctr  = control; }
		else if (control.id == 'UserState' && !isText(control)){
			a_errors[a_errors.length] = {'t': '-Please input a valid <strong>State</strong>(Only Alphabetical characters allowed).', 'f': '', 'i': 'UserProvince'}; 		 
			if(!ctr) ctr  = control; } 
 		else if (control.id == 'UserCity' && !isText(control)){
			a_errors[a_errors.length] = {'t': '-Please input a valid <strong>City</strong>(Only Alphabetical characters allowed).', 'f': '', 'i': 'UserCity'}; 		 
			if(!ctr) ctr  = control; }	
		else if (control.id == 'UserPhone' && !checkInternationalPhone(control.value)){
			a_errors[a_errors.length] = {'t': '-Please input a valid <strong>Phone Number</strong>.', 'f': '', 'i': 'UserPhone'};
				if(!ctr) ctr  = control; }
		else if (control.id == 'UserZipcode' && !isInteger(control)){
			a_errors[a_errors.length] = {'t': '-Please input a valid <strong>Zipcode</strong>.', 'f': '', 'i': 'UserZipcode'};	
			if(!ctr) ctr  = control; 	}
		else if (control.id == 'UserEmail' && !isValidEmailFormat(control)){
			a_errors[a_errors.length] = {'t': '-Please input a valid <strong>Email </strong>.', 'f': '', 'i': 'UserEmail'};	
			if(!ctr) ctr  = control; 	}
		else if (control.id == 'UserUsername' && !isProperText(control)){
			a_errors[a_errors.length] = {'t': '-Please input a valid <strong>Username </strong>.', 'f': '', 'i': 'UserUsername'};	
			if(!ctr) ctr  = control; 	}		
		else if (control.id == 'UserPassword' && !chkPWD('UserPassword','UserPassword2')){
			a_errors[a_errors.length] = {'t': '<strong>-Password And Confirmed Password</strong> does not match .', 'f': '', 'i': 'UserPassword2'}; 
			if(!ctr) ctr  = control; }
	}
	
	/* Compare Start Date and End Date #Start */
	/*var startDate = "";
	var endDate	  = "";	
	var startDateTime = document.getElementById("EventStartDate").value; 
	var endDateTime   = document.getElementById("EventEndDate").value; 
	if(startDateTime != "" && endDateTime != "")
	{
		 startDateArr   = startDateTime.split(" ");
		 startDate		= startDateArr[0];
		 endDateArr     = endDateTime.split(" ");	
		 endDate	    = endDateArr[0];
		 if(!isValidStartEndDates(startDate,endDate))
		 {
			a_errors[a_errors.length] = {'t': "- Start date can not be greater than end date.", 'f': "", 'i': "EventEndDate"};
			if(!ctr) ctr  = document.getElementById("EventStartDate");
		 }
	}*/
	/* Compare Start Date and End Date #End */

	// display error messages
	if (a_errors.length)
	{		 
		error(a_errors);
		ctr.focus();
		return false;
	}
	//return chkPWD(UserPassword,UserConfirmpassword);
	report('Submitted, ','Adding Record to the Database...');
	return true;
/*	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("default_id").focus();

	s_message = '<table border=0  bgcolor="#FFFFCE" align=center width=100% cellspacing=0 cellpadding=0><tr><td valign="top" style="padding-top:2px; font-size: 12px; text-align: right; letter-spacing:2px;"><b>Instructions:&nbsp;&nbsp;</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 = 
		  '<div id=error_msg><table cellpadding="0" cellspacing="0" width="100%" border=1 bordercolor=#E7867B>'
		+ '<tr><td bgcolor="#2252A7">'
		+ '<table cellpadding="0" cellspacing="0" border="0" width="100%">'
		+ '<tr><td bgcolor="#2252A7" style="color: red; ">'
		+ '<div style="overflow: auto; width:100%; ">' + s_message + '</div>'
		+ '</td></tr>'
		+ '</table>'
		+ '</td></tr>'
		+ '</table></div>';
		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.....





/**************************************Phone validation Start By Sarita*************************************/

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
			var bracket=3
			strPhone=trim(strPhone)
			if(strPhone.indexOf("+")>1) return false
			if(strPhone.indexOf("-")!=-1)bracket=bracket+1
			if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
			var brchr=strPhone.indexOf("(")
			if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
			if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
			s=stripCharsInBag(strPhone,validWorldPhoneChars);
			return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateForm(){
	var Phone=document.frmSample.txtPhone
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }
/**************************************Phone validation End By Sarita*************************************/











