/*********************************************************************************************************************************/
/**************************************************** 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 = [
		'lblRAddress',
		'lblRCity',
		'lblRState',
		'lblRZip',
		'lblCName',
		'lblCAddress',
		'lblCCity',
		'lblCState',
		'lblCZip',
		'lblCPhone1',
		'lblLName',
		'lblLAddress',
		'lblLCity',
		'lblLState',
		'lblLZip',

        'lblLPhone1',
		'lblComplaintReason',
		'lblComplaintSeverity',
		'lblComplaintTitle',
		'lblLandlordAttempt',
		'lblOngoingProblem',
		'lblSelfFault',
//		'lblFaultResponsibility',
		'lblResolution',
		'lblAuthorize'
	];
	
	var FRM_LABELS1 = [
		'lblLName1',
		'lblLAddress1',
		'lblLCity1',
		'lblLState1',
		'lblLZip1'	  ];	
	
	
	var FRM_CONTROLS = [
		'txtRAddress',
		'txtRCity',
		'cmbRState',
		'txtRZip',
		'txtCName',
		'txtCAddress',
		'txtCCity',
		'cmbCState',
		'txtCZip',
		'txtCPhone1',
		'txtLName',		
		'txtLAddress',
		'txtLCity',
		'cmbLState',
		'txtLZip',
        'txtLPhone1',
		'cmbComplaintReason',
		'cmbComplaintSeverity',
		'txtGeneralDescription',
		'cmbLandlordAttempt',
		'cmbOngoingProblem',
		'cmbSelfFault',
//		'txtFaultResponsibility',
		'txtResolution',
		'chkAuthorize'		
	];
	

	var FRM_CONTROLS1 = [
 		'txtLName1',		
		'txtLAddress1',
		'txtLCity1',
		'cmbLState1',
		'txtLZip1'];


  var ERR_MESSAGES = [
		'Rental\'s Address is a required field.',
		'Rental\'s City is a required field.',
		'Rental\'s State is a required field.',
		'Rental\'s Zip code is a required field.',
		'Renter\'s Name is a required field.',
	    'Renter\'s Address is a required field.',
     	'Renter\'s City is a required field.',
		'Renter\'s State is a required field.',
		'Renter\'s Zip code is a required field.',
		'Renter\'s Phone is a required field.',
		'Landlord\'s Name is a required field.',
		'Landlord\'s Address is a required field.',
		'Landlord\'s City is a required field.',
		'Landlord\'s State is a required field.',
		'Landlord\'s Zip Code is a required field.',
			
		'Landlord\'s Phone is a required field.',
		'Reason for filing complaint is a required field.',
		'Complaint Severity is a required field.',
		'Complain Title is a required field.',
		'Has Landlord attempted to resolve problem... is a required field.',
		'Is this an ongoing problem... is a required field.',
		'Do you take any responsibility... is a required field.',
	//	'Who do you feel is at fault... is a required field.',
		'What would be a FAIR resolution for this problem... is a required field.',
		'You need to check the authorize checkbox before proceeding with the next step.'
	];
  
  
  var ERR_MESSAGES1 = [		
		'Second Landlord\'s Name is a required field.',
		'Second Landlord\'s Address is a required field.',
		'Second Landlord\'s City is a required field.',
		'Second Landlord\'s State is a required field.',
		'Second Landlord\'s Zip Code 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;
		
//		if(!document.getElementById("chkAuthorize").checked)
			//a_errors[a_errors.length] = {'t': 'Authorizatation is required.', 'f': 'lblAuthorize', 'i': 'chkAuthorize'};

		clear_displayMessage(FRM_LABELS);
		a_errors = [];
		
		
							
		for (i in frmControls)
		{
		   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': 'Email is Incorrect.', 'f': 'lblEmail', 'i': 'txtEmail'};
			else if (control.type == 'checkbox' && !control.checked)
				{
					a_errors[a_errors.length] = {'t': 'You need to check the authorize checkbox before proceeding with the next step', 'f': 'lblAuthorize', 'i': 'chkAuthorize'};				
				}				
			
		}
		
		
		var frmLabels1 = FRM_LABELS1,frmControls1 = FRM_CONTROLS1, errMessages1 = ERR_MESSAGES1;
		
		if(document.getElementById('extra_info').style.display=="block")
		{
			   
		 for (i in frmControls1)
		  {
		   control = document.getElementById(frmControls1[i]); 
		   
			if (!control.value || control.value==0)		//This checks whether the Control(textbox) contains some value or not
				{a_errors[a_errors.length] = {'t': errMessages1[i], 'f': frmLabels1[i], 'i': frmControls1[i]};			
				}
			else if (control.id == 'txtEmail1' && !isValidEmail(control.value))
				a_errors[a_errors.length] = {'t': 'Email is Incorrect.', 'f': 'lblEmail1', 'i': 'txtEmail1'};			
		  }
		}
		
		
	
		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 = '';
			}
		}
	
		for (var i = 0; i < frmLabels.length; i++)
		{
			e_caption = get_element(frmLabels[i]);
			if (!e_caption)
				continue;
			e_caption.className = 'normal';
		}
		
		
		for (var i = 0; i <  FRM_LABELS1.length; i++)
		{
			e_caption = get_element( FRM_LABELS1[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: FORM LEVEL CHECKS ****************************************************/
/*********************************************************************************************************************************/
	function addressCopy()
	{	
		if(document.getElementById('chkAddcpy').checked)
		{
			document.getElementById('txtCAddress').value=document.getElementById('txtRAddress').value;
			document.getElementById('txtCCity').value=document.getElementById('txtRCity').value;
			document.getElementById('cmbCState').value=document.getElementById('cmbRState').value;
			document.getElementById('txtCZip').value=document.getElementById('txtRZip').value;
			document.getElementById('txtCUnitNo').value=document.getElementById('txtRUnitNo').value;
		}
		else
		{
			document.getElementById('txtCAddress').value="";
			document.getElementById('txtCCity').value="";
			document.getElementById('cmbCState').value="0";
			document.getElementById('txtCZip').value="";
			document.getElementById('txtCUnitNo').value="";
			document.getElementById('txtCAddress').focus();
		}
	}
/*********************************************************************************************************************************/
/*************************************************** END: FORM LEVEL CHECKS ******************************************************/
/*********************************************************************************************************************************/

/********************** START: Validation to Check max length of Textbox  **********************/
	function check(i)
	{
		var spanChar = "";
		var spanError = "";
		var maxLength = "";

		if(i.id == "txtAdditionalDetails")
		{
			spanChar = "spanCharComments";
			spanError = "spanCommentError";
			maxLength = 2500;
		}
		if(i.id == "txtMemoToAgent")
		{
			spanChar = "spanCharMemo";
			spanError = "spanMemoError";
			maxLength = 500;
		}
		if(i.id == "txtGeneralDescription")
		{
			spanChar = "spanShortDescChar";
			spanError = "spanShortDescError";
			maxLength = 75;
		}
		if(i.id == "txtResolution")
		{
			spanChar = "spanFairResoChar";
			spanError = "spanFairResoError";
			maxLength = 250;
		}

		document.getElementById(spanChar).innerHTML = i.value.length;

		if(i.value.length > maxLength)
		{
			val = i.value.substring(0, maxLength);
			document.getElementById(spanError).style.display = "block";
			i.value = val;
			InitializeTimerComments();
		}
	}

	/** STARTS: Removes the Error Message box from the page after a specific predefined time span **/
		function InitializeTimerComments()
		{
			secs = 5;											// Set the length of the timer, in seconds
			StopTheClockComments();
			StartTheTimerComments();
		}
		
		function StopTheClockComments()
		{
			if(timerRunning)
				clearTimeout(timerID);
			timerRunning = false;
		}
		
		function StartTheTimerComments()
		{
			if (secs==0)
			{
				StopTheClockComments();
				document.getElementById("spanCommentError").style.display = 'none';
				document.getElementById("spanMemoError").style.display = 'none';
				document.getElementById("spanShortDescError").style.display = 'none';
				document.getElementById("spanFairResoError").style.display = 'none';
			}
			else
			{
				// self.status = secs
				secs = secs - 1;
				timerRunning = true;
				timerID = self.setTimeout("StartTheTimerComments()", delay);
			}
		}
		/** ENDS: Removes the Error Message box from the page after a specific predefined time span */
		

       /********************** START: Validation to Check max length of Textbox  **********************/
	   function addContact()
	   {
		 if(document.getElementById("add_info").checked)
		  document.getElementById("extra_info").style.display="block";
		  else
		   document.getElementById("extra_info").style.display="none";
		 
		  
	   }
	   
	   function onlyNumbers(evt)
       {
			var e = event || evt; // for trans-browser compatibility
			var charCode = e.which || e.keyCode;
		
			if (charCode > 31 && (charCode < 48 || charCode > 57))
				return false;
		
			return true;
       }

	/************ END *************/