//validate Question and Details
function showError(divId,errorText,x,y){
			var error = document.getElementById(divId);
			window.scrollTo(x,y);
			error.innerHTML = errorText;
}
//trim functions

	function preprocess16(val)
		{
			if(val=='submit')
					{
					document.myform.submit.value=val;
				}



 }

   function preprocess20(val1)
                {
                                        document.myform.preview.value=val1;

 }

	function emailCheck()
		{
		
				
	
//			var emailFilter=/^.+@.+\..{2,3}$/;
							
			
			
			
			var emailLength=document.getElementsByName("friendEmail[]").length;


			for(var j=0;j<emailLength;j++){
				var emailValue=	document.getElementsByName("friendEmail[]")[j].value;			

				var emailFilter=/^.+@.+\..{2,3}$/;

				if(emailValue=="" || !(emailFilter.test(emailValue)))
					{
						alert("Please enter a valid email address.");
     						   return false;
					}
			}
     					   return true;
		}


function jtrim(value) {
	return LTrim(RTrim(value));
}
function trim(value) {
	return LTrim(RTrim(value));
}
// Removes leading whitespaces
function LTrim(value) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim(value) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
function enterAnswerValidate()
        {
	var quest=document.myform.QUESTION.value;
		if(trim(quest)=="" || quest.length < 10 ){
		document.myform.QUESTION.focus();
		showError("errorMsg","Please enter Question at lest 10 character",0,0);
			return false;
}
}
    
function enterQuestionValidate()
	{
/*var dropdown=document.myform.mydropdown.value;	*/
var quest=document.myform.QUESTION.value;
var questionTitle=document.myform.questionTitle.value;
var questionTag=document.myform.questionTag.value;


if(trim(questionTitle)=="" || questionTitle.length<10){
document.myform.questionTitle.focus();
showError("errorMsg","Enter the question title (10 characters minimum) ",0,0);
return false;
}

if(trim(questionTag)=="" || questionTag.length < 2 ){
document.myform.questionTag.focus();
showError("errorMsg","1 Topic min, 5 Topics max",0,0);
return false;
}
/*
if(dropdown==""){			
document.myform.mydropdown.focus();
showError("errorMsg","Please select Category",0,0);			
return false;
}

if(trim(quest)=="" || quest.length < 10 ){
document.myform.QUESTION.focus();
showError("errorMsg","Please enter Question at lest 10 character",0,0);
return false;
}
*/
}

// test the character limit
function textCounter(field, countfield, maxlimit)
	{
		if (field.value.length > maxlimit) // if too long the characters will not enter
		field.value = field.value.substring(0, maxlimit);
	else 
		countfield.value = maxlimit - field.value.length;
	}


// validate the empty field of answer	
    function answerValidateform()
     {
        if(""==document.form.ANSWER.value)
          {
        alert("please enter ur answer");
        document.form.ANSWER.focus();
        return false;
          }

     }

// generate textarea on click event of button
      function generateRow(isLoggedIn)
         {
                if(isLoggedIn=="0"){
                        var location = "../index.php";
                        window.location = location;
                    }
                 else{
                        var d=document.getElementById("div");
                        var content="<form name='form' action='' method='POST' onsubmit='return answerValidateform();'>";
                        content+="<table width='80%' border='0'><tr><td colspan='2'>";
                        content+="<textarea  name='ANSWER' cols='80' rows='10' class='textArea' wrap='hard'></textarea>";
                content+="</td></tr><tr><td width='20%'>";
                content+="<input type='image' src='images/submit.gif'></td>";
                content+="<td width='80%'>";
                            content+="<input type='image' src='images/Discard.gif' onclick='hideRow();'>";
                            content+="</td></tr></table><input type='hidden' name='anssubmit'  value=''/>";
                            content+="</form>";
                d.innerHTML=content;
                     }
          }

  function addRowToTable()
	{
	var emailValid1='';

	emailValid1=emailCheck();


	if(emailValid1){

		  var tbl = document.getElementById('tblSample');
		  var lastRow = tbl.rows.length;
		  // if there's no header row in the table, then iteration = lastRow + 1
		  var iteration = lastRow + 1;
		  var row = tbl.insertRow(lastRow);

		  // left cell
		  var cellLeft = row.insertCell(0);
		  cellLeft.className='homeSectionContentDetailsemail' ;
		  cellLeft.width="3%";
		  var textNode = document.createTextNode('Associated file ' + iteration);
		// cellLeft.appendChild(textNode);

  // right cell
  var cellRight = row.insertCell(1);
  cellRight.width="20%";
	  	
  var el = document.createElement('input');
  el.type = 'text';
  el.align='left';	

	el.name = 'friendEmail[]';
  el.id = 'row'+iteration;
  el.className ='reg_txtfield';
//  el.onkeypress = keyPressTest;
  cellRight.appendChild(el);
}
}

function removeRowFromTable()
{
  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
   tbl.deleteRow(lastRow - 1);
}

















//hide the textarea on click event of button 
          function hideRow()
          {
                var d2=document.getElementById("div");
                var content="<input type='image' src='images/answer.gif' onclick=generateRow('{$isLoggedIn}');>";
                d2.innerHTML=content;
          }

// best answer rewarded textarea          
		function sz(t)
		 {
			a = t.value.split('\n');
			b=1;
			for (x=0;x < a.length; x++) {
				if (a[x].length >= t.cols) b+= Math.floor(a[x].length/t.cols);
			}
				b= a.length;
				if (b > t.rows) t.rows = b;
		 }

	
