// JavaScript Document

function resetSurvey(s)
{
  if(confirm("By clicking yes the survey will have to be refilled. Are you sure you want to continue?"))
  {
  //var f = document.getElementById(frm)
  document.frmsurvey.reset()
  }
}

function hideallids(no,name)
{
	//loop through the array and hide each element by id
	for (var i=0;i<no;i++){
	if(name!= null) // if a second parameter is passed hide its name as well 
		hidediv(name+i);
	else
	  hidediv(i);
	}		  
}

function hidediv(id) 
{
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
			document.id.style.backgroundcolor  ='#ffffff';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
			document.all.id.style.backgroundcolor ='#ffffff';
		}
	}
}
  

/* ----------------------------------------------------------------
   
*/
function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}




function togglediv(id) {
	//safe function to show an element with a specified id

//check if visible
if (document.getElementById(id).style.display =='none')
{
  showdiv(id);
}
else
  hidediv(id);
}


/* ----------------------------------------------------------------
update the internal HTML of tag
*/
function updateTagValue(name,value)
{
//
  document.getElementById(name).innerHTML = value;
 //setDivStyle(name);
}


function addValue(obj,val)
{
//alert(obj);
	//updateTagValue(obj,val);
	var out = document.getElementById(obj);//.value;
	out.value = val;
	
	child_grandTotal();
}

/* ----------------------------------------------------------------
update the internal HTML of tag
*/
function getTagValue(name)
{
//alert(name);
return document.getElementById(name).firstChild.nodeValue;
}


/* ----------------------------------------------------------------
   change value of an object
*/
function changeValue(output,val)
{
	try 
	{
	  var out = document.getElementById(output);//.value;
	  div_val= out.value;
		total1 = parseInt(div_val) + parseInt(val);
		out.value = total1;
		updateTagValue(div,total1);
		grandTotal();
	}
	catch(err)
	{
		//alert(err);
	}
}

/* ----------------------------------------------------------------
   add to total section
*/
function addSection(div,val,output)
{
	try 
	{
	  var out = document.getElementById(output);//.value;
	  div_val= out.value;
		total1 = parseInt(div_val) + parseInt(val);
		out.value = total1;
		updateTagValue(div,total1);
		grandTotal();
	}
	catch(err)
	{
		//alert(err);
	}
}

/* ----------------------------------------------------------------
   
*/
function addSection_test_Child(obj,div,val)
{
	subtract = getCheckedValue(obj);
	
	//alert("val "+ val+ "  subtract "+subtract);
	
	
  if(subtract == (val*-1))
  {
    //alert("No to subtract "+subtract);
  	//make the number negative
  	abc = parseInt(subtract)*-1;
		addSection(div,abc,'grand_total');
	}
	//else
	// alert("Val to add "+val);
//	addSection(div,(val*-1),'grand_total');
	
}

/* ----------------------------------------------------------------
   add to total section
*/
function addSection_Child(div,val)
{
	//addSection(div,val,'grand_total');
}

/* ----------------------------------------------------------------
   add to total section
*/
function addSection_A(div,val)
{
	addSection(div,val,'total_score_section_a');
}

/* ----------------------------------------------------------------
   add to total section
*/
function addSection_B(div,val)
{
	//addSection(div,val,'total_score_section_b');
}

/* ----------------------------------------------------------------
   add to total section
*/
function addSection_C(div,val)
{
	//addSection(div,val,'total_score_section_c');
}


/*
*******************************************************************
*/

function addValue_A(obj,val)
{
//alert(obj);
	//updateTagValue(obj,val);
	var out = document.getElementById(obj);//.value;
	out.value = val;
	
	addValue_section_A();
}

/* ----------------------------------------------------------------
   add to total section
*/
function addValue_section_A()
{
	try 
	{
	ret = 0;
		var Length = document.frm.txt_a.length;//Obj.length;

		for(var i = 0; i < Length; i++) 
		{
				ret =parseInt(ret) + parseInt(document.frm.txt_a[i].value);
		}
		updateTagValue('total_section_a',ret);
		
		
		//updateTagValue('total_section_b',ret);
		
			var out11 = document.getElementById('total_score_section_a');//.value;
		  out11.value = ret;
	
	grandTotal();
		//alert(ret);
	}
	catch(err)
	{
		alert(err + ' child_grandTotal');
	}
}


/*
*******************************************************************
*/

function addValue_B(obj,val)
{
//alert(obj);
	//updateTagValue(obj,val);
	var out = document.getElementById(obj);//.value;
	out.value = val;
	
	addValue_section_B();
}

/* ----------------------------------------------------------------
   add to total section
*/
function addValue_section_B()
{
	try 
	{
	ret = 0;
		var Length = document.frm.txt_b.length;//Obj.length;

		for(var i = 0; i < Length; i++) 
		{
				ret =parseInt(ret) + parseInt(document.frm.txt_b[i].value);
		}
		updateTagValue('total_section_b',ret);
		
		
		//updateTagValue('total_section_b',ret);
		
			var out11 = document.getElementById('total_score_section_b');//.value;
		  out11.value = ret;
	
	grandTotal();
		//alert(ret);
	}
	catch(err)
	{
		alert(err + ' child_grandTotal');
	}
}


/*
*******************************************************************
*/

function addValue_C(obj,val)
{
//alert(obj);
	//updateTagValue(obj,val);
	var out = document.getElementById(obj);//.value;
	out.value = val;
	
	addValue_section_C();
}

/* ----------------------------------------------------------------
   add to total section
*/
function addValue_section_C()
{
	try 
	{
	ret = 0;
		var Length = document.frm.txt_c.length;//Obj.length;

		for(var i = 0; i < Length; i++) 
		{
				ret =parseInt(ret) + parseInt(document.frm.txt_c[i].value);
		}
		updateTagValue('total_section_c',ret);
		
		
		//updateTagValue('total_section_b',ret);
		
			var out11 = document.getElementById('total_score_section_c');//.value;
		  out11.value = ret;
	
	grandTotal();
		//alert(ret);
	}
	catch(err)
	{
		alert(err + ' child_grandTotal');
	}
}

/* ----------------------------------------------------------------
   add to total section
*/
function child_grandTotal()
{
	try 
	{
	ret = 0;
		var Length = document.frm.txt.length;//Obj.length;

		for(var i = 0; i < Length; i++) 
		{
				ret =parseInt(ret) + parseInt(document.frm.txt[i].value);
		}
		updateTagValue('total_section',ret);
		
			var out1123 = document.getElementById('grand_total');//.value;
		  out1123.value = ret;
		//updateTagValue('total_section',ret);
	}
	catch(err)
	{
		alert(err + ' child_grandTotal');
	}
}


/* ----------------------------------------------------------------
   add to total section
*/
function grandTotal()
{
	try 
	{
		
		var outa = document.getElementById('total_score_section_a');//.value;
	  div_val_a= outa.value;
	  //div_val_a=document.frm.total_score_section_a.value;
	  
	  var outb = document.getElementById('total_score_section_b');//.value;
	  div_val_b= outb.value;
	  //div_val_b=document.frm.total_score_section_b.value;
	  
	  var outc = document.getElementById('total_score_section_c');//.value;
	  div_val_c= outc.value;
	  //div_val_c=document.frm.total_score_section_c.value;
		total5 = parseInt(div_val_a) + parseInt(div_val_b)+parseInt(div_val_c);
		
		var outg = document.getElementById('grand_total_score');
	  outg.value = total5;
		updateTagValue('grand_total',total5);
	}
	catch(err)
	{
		//alert(err + ' grand total');
	}
}

/* --------------------------------------------------------------
	CHECK IF A NUMBER IS VALID
*/
function IsNumeric(sText)
{
   var ValidChars = "0123456789.-";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
/*
  	specific function that will retrieve the cate change the pic etc
  */
  function getCateg(id)
  {
  	pic = 'img_down'+id;
	div = 'categ'+id;
	togglediv(div);
	togglediv(pic);
	showSubcat('get_subcats.php?id='+id,displaySubcat,div);	
  }

  function getArticle(id)
  {
	div = 'article_'+id;
	togglediv(div);
	showSubcat('get_articles.php?id='+id,displaySubcat,div);
  }
  
// ----------------  AJAX  ---------------------
   function displaySubcat(text,div_id)
    {
      document.getElementById(div_id).innerHTML = text;
    }
    
  function showSubcat(url,callbackfunction,div)
    {
      document.getElementById(div).innerHTML = 'loading ...';
      getText(url,callbackfunction,div);
    }
  
  
