function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}
	else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function numbersonly(e){
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8 && unicode!=9){
		if (unicode<48||unicode>57) {
			return false;
		}
	}
}

function clean_num(elem) {
	var tmp;
	tmp=elem.value;
	if (tmp=='') tmp='0';
	tmp=tmp*1;
	elem.value=tmp;
}

function sum_all() {
	var cnt=1;
	var suma=0;
	while (cnt<=12) {
		var tmp='t'+cnt;
		var val=document.tool.elements[tmp].value;
		val=val*1;
		suma+= val;
		if (val!=0) {
			tmp='cumu'+cnt;
			document.getElementById(tmp).innerHTML=suma+'%';
		}	
		cnt++;
	}
	document.getElementById('totalsum').innerHTML=suma+'%';
}

function val_sum_all() {
	var cnt=1;
	var suma=0;
	while (cnt<=12) {
		var tmp='t'+cnt;
		var val=document.tool.elements[tmp].value;
		val=val*1;
		suma+= val;
		cnt++;
	}
	if (suma!=100) {
		alert('The total must equal 100%.\nThe sum of your percentages is '+suma+'%');
		return false;
	}
	return true;
}

function load_opts(index) {
	if (index==null) { index=0; }
	var mSel=document.demog.userSubregion;
	var userRegion = new Array();
	switch (index) {
		case 0:
			userRegion[0] = 'Australia';
			userRegion[1] = 'China';
			userRegion[2] = 'Japan';
			userRegion[3] = 'New Zealand';
			userRegion[4] = 'Northern Asia Pacific';
			userRegion[5] = 'Southern Asia Pacific';
			break;
		case 1:
			userRegion[0] = 'Africa';
			userRegion[1] = 'Eastern Europe';
			userRegion[2] = 'France';
			userRegion[3] = 'Germany';
			userRegion[4] = 'Italy';
			userRegion[5] = 'Netherlands';
			userRegion[6] = 'Scandanavia';
			userRegion[7] = 'Spain';
			userRegion[8] = 'United Kingdom';
			break;
		
		case 2:
			userRegion[0] = 'Global';
			break;
		
		case 3:
			userRegion[0] = 'Carribean';
			userRegion[1] = 'Brazil';
			userRegion[2] = 'Argentina';
			userRegion[3] = 'Peru';
			userRegion[4] = 'Columbia';
			break;
		
		case 4:
			userRegion[0] = 'Canada';
			userRegion[1] = 'Mexico';
			userRegion[2] = 'USA';
			break;
	}
	for (var cnt=mSel.options.length-1; cnt>=0; cnt--) {
		mSel.options[cnt]=null;
	}
	for (var i=0; i<userRegion.length; i++) {
		var oOption = document.createElement('option');
		oOption.value = i;
		oOption.text = userRegion[i];
		try {
			mSel.add(oOption, null);
		}
		catch(ex) {
			mSel.add(oOption);
		}
	}
}

function val_index(tpe) {
	if (tpe=='word') {
		var user = document.login.uid;
		var pwd = document.login.passwd;
		if ((user.value=='') || (pwd.value=='')) {
			alert('Please make sure that you fill both the USERNAME and PASSWORD fields.');
			return false;
		}
	}
	if (tpe=='code') {
		var pwd = document.login.passwd;
		if (pwd.value=='') {
			alert('Please make sure that you fill the PASSCODE field.');
			return false;
		}
	}
	return true;
}

function val_demog() {
	var txx_flds = new Array();
	txx_flds[0] = document.demog.clientName;
	txx_flds[1] = document.demog.userLast;
	txx_flds[2] = document.demog.userFirst;
	txx_flds[3] = document.demog.userMI;
	txx_flds[4] = document.demog.userEmail;
	txx_flds[5] = document.demog.userPwd;
	txx_flds[6] = document.demog.userRole;
	txx_flds[7] = document.demog.userIndustry;
	txx_flds[8] = document.demog.careerYears;
	txx_flds[9] = document.demog.userTitle;
	txx_flds[10] = document.demog.currentYears;
	txx_flds[11] = document.demog.userComp;
	txx_flds[12] = document.demog.compPercent;
	txx_flds[13] = document.demog.userQuotatarget;
	txx_flds[14] = document.demog.userQuota;
	txx_flds[15] = document.demog.userAwards;
	txx_flds[16] = document.demog.userAccounts;
	txx_flds[17] = document.demog.salesCampaigns;
	
	var cnt=0 ;
	while (cnt<txx_flds.length) {
		if(cnt!=3){
			if (txx_flds[cnt].value=='') {
				var tmp = 'Please make sure to fill in all of the required fields before continuing';
				alert(tmp);
				txx_flds[cnt].focus();
				return false;
			}
			if (txx_flds[cnt].label=='--Select One--') {
				var tmp = 'Please make sure to fill in all of the required fields before continuing';
				alert(tmp);
				txx_flds[cnt].focus();
				return false;
			}
		}
		cnt++;
	}
	
	if (!isValidEmail(txx_flds[4].value)) {
		txx_flds[4].focus();
		txx_flds[4].select();
		return false;
	}
	
	if (!chkbox('cC',100,'\"Compensation Components\"')) {
		return false;
	}
	
	if (!chkbox('sI',3,'\"Industries that you primarily sell to\"')) {
		return false;
	}
	return true;
}

function isValidEmail(str) {
	if ((str.indexOf(".")>2) && (str.indexOf("@")>0)) {
		return true;
	}
	alert('Please enter a correct email address');
	return false;
}

function chkbox(nme,max,fld) {
	var bx;
	var cnt=0;
	var i=0;
	while(bx=document.demog[nme+i]) {
		cnt += bx.checked;
		i++;
	}
	if (cnt>max) {
		alert('Please choose '+max+' checkboxes or less in the '+fld+' field.');
		document.demog[nme+'0'].focus();
		return false;
	}
	if (cnt==0) {
		alert('Please choose at least one checkbox in the '+fld+' field.');
		document.demog[nme+'0'].focus();
		return false;
	}
	return true;
}

function sumlcl() {
	var role=document.testvalues.role.value*1;
	var t= new Array();
	t[0]=document.testvalues.t1.value*1;
	t[1]=document.testvalues.t2.value*1;
	t[2]=document.testvalues.t3.value*1;
	t[3]=document.testvalues.t4.value*1;
	t[4]=document.testvalues.t5.value*1;
	t[5]=document.testvalues.t6.value*1;
	t[6]=document.testvalues.t7.value*1;
	t[7]=document.testvalues.t8.value*1;
	t[8]=document.testvalues.t9.value*1;
	t[9]=document.testvalues.t10.value*1;
	t[10]=document.testvalues.t11.value*1;
	t[11]=document.testvalues.t12.value*1;
	var tt = new Array();

	var sum=0, sumTAR=0;

	switch (role) {
		case 0:
			var tarname='tAM';
			break;
		case 1:
			var tarname='tSR';
			break;
	}
	for (var i=0; i<12;i++) {
		var tmp=i+1;
		sum += t[i];
		tt[i] = document.getElementById(tarname+tmp).innerHTML*1;
		sumTAR += tt[i];
	}
	document.getElementById('cumutotal').innerHTML=sum+'%';

//////ROW 1
	
	var t1t2=t[0]+t[1];
	var t3t4=t[2]+t[3];
	var t5t6=t[4]+t[5];
	var t7t8=t[6]+t[7];
	var t9t10=t[8]+t[9];
	var row1=t1t2+t3t4+t5t6+t7t8+t9t10;

	var tt1t2=tt[0]+tt[1];
	var tt3t4=tt[2]+tt[3];
	var tt5t6=tt[4]+tt[5];
	var tt7t8=tt[6]+tt[7];
	var tt9t10=tt[8]+tt[9];
	var trow1=tt1t2+tt3t4+tt5t6+tt7t8+tt9t10;

//write text percentages

	document.getElementById('cumu'+tarname).innerHTML=sumTAR+'%';
	document.getElementById('t1t2').innerHTML=t1t2+'%';
	document.getElementById('tt1t2').innerHTML=tt1t2+'%';
	document.getElementById('t3t4').innerHTML=t3t4+'%';
	document.getElementById('tt3t4').innerHTML=tt3t4+'%';
	document.getElementById('t5t6').innerHTML=t5t6+'%';
	document.getElementById('tt5t6').innerHTML=tt5t6+'%';
	document.getElementById('t7t8').innerHTML=t7t8+'%';
	document.getElementById('tt7t8').innerHTML=tt7t8+'%';
	document.getElementById('t9t10').innerHTML=t9t10+'%';
	document.getElementById('tt9t10').innerHTML=tt9t10+'%';
	document.getElementById('row1').innerHTML=row1+'%';
	document.getElementById('trow1').innerHTML=trow1+'%';

//set max size per row

	var row1max = trow1;
	if (row1 > trow1) row1max=row1;

//get row height

	var tdheight=document.getElementById('tdrow1').height*1;
	
	irow1=row1/row1max*tdheight;
	document.getElementById('irow1').height=irow1;
	itrow1=trow1/row1max*tdheight;
	document.getElementById('itrow1').height=itrow1;

	it1t2=t1t2/row1max*tdheight;
	document.getElementById('it1t2').height=it1t2;
	itt1t2=tt1t2/row1max*tdheight;
	document.getElementById('itt1t2').height=itt1t2;

	it3t4=t3t4/row1max*tdheight;
	document.getElementById('it3t4').height=it3t4;
	itt3t4=tt3t4/row1max*tdheight;
	document.getElementById('itt3t4').height=itt3t4;

	it5t6=t5t6/row1max*tdheight;
	document.getElementById('it5t6').height=it5t6;
	itt5t6=tt5t6/row1max*tdheight;
	document.getElementById('itt5t6').height=itt5t6;

	it7t8=t7t8/row1max*tdheight;
	document.getElementById('it7t8').height=it7t8;
	itt7t8=tt7t8/row1max*tdheight;
	document.getElementById('itt7t8').height=itt7t8;

	it9t10=t9t10/row1max*tdheight;
	document.getElementById('it9t10').height=it9t10;
	itt9t10=tt9t10/row1max*tdheight;
	document.getElementById('itt9t10').height=itt9t10;

//////ROW 2
	
	var t1=t[0];
	var t3=t[2];
	var t5=t[4];
	var t7=t[6];
	var t9=t[8];
	var row2=t1+t3+t5+t7+t9;

	var tt1=tt[0];
	var tt3=tt[2];
	var tt5=tt[4];
	var tt7=tt[6];
	var tt9=tt[8];
	var trow2=tt1+tt3+tt5+tt7+tt9;

//write text percentages

	document.getElementById('t1').innerHTML=t1+'%';
	document.getElementById('tt1').innerHTML=tt1+'%';
	document.getElementById('t3').innerHTML=t3+'%';
	document.getElementById('tt3').innerHTML=tt3+'%';
	document.getElementById('t5').innerHTML=t5+'%';
	document.getElementById('tt5').innerHTML=tt5+'%';
	document.getElementById('t7').innerHTML=t7+'%';
	document.getElementById('tt7').innerHTML=tt7+'%';
	document.getElementById('t9').innerHTML=t9+'%';
	document.getElementById('tt9').innerHTML=tt9+'%';
	document.getElementById('row2').innerHTML=row2+'%';
	document.getElementById('trow2').innerHTML=trow2+'%';

//set max size per row

	var row2max = trow2;
	if (row2 > trow2) row2max=row2;

//get row height

	var tdheight=document.getElementById('tdrow2').height*1;
	

	irow2=row2/row2max*tdheight;
	document.getElementById('irow2').height=irow2;
	itrow2=trow2/row2max*tdheight;
	document.getElementById('itrow2').height=itrow2;

	it1=t1/row2max*tdheight;
	document.getElementById('it1').height=it1;
	itt1=tt1/row2max*tdheight;
	document.getElementById('itt1').height=itt1;

	it3=t3/row2max*tdheight;
	document.getElementById('it3').height=it3;
	itt3=tt3/row2max*tdheight;
	document.getElementById('itt3').height=itt3;

	it5=t5/row2max*tdheight;
	document.getElementById('it5').height=it5;
	itt5=tt5/row2max*tdheight;
	document.getElementById('itt5').height=itt5;

	it7=t7/row2max*tdheight;
	document.getElementById('it7').height=it7;
	itt7=tt7/row2max*tdheight;
	document.getElementById('itt7').height=itt7;

	it9=t9/row2max*tdheight;
	document.getElementById('it9').height=it9;
	itt9=tt9/row2max*tdheight;
	document.getElementById('itt9').height=itt9;

//////ROW 3
	
	var t2=t[1];
	var t4=t[3];
	var t6=t[5];
	var t8=t[7];
	var t10=t[9];
	var row3=t2+t4+t6+t8+t10;

	var tt2=tt[1];
	var tt4=tt[3];
	var tt6=tt[5];
	var tt8=tt[7];
	var tt10=tt[9];
	var trow3=tt2+tt4+tt6+tt8+tt10;

//write text percentages

	document.getElementById('t2').innerHTML=t2+'%';
	document.getElementById('tt2').innerHTML=tt2+'%';
	document.getElementById('t4').innerHTML=t4+'%';
	document.getElementById('tt4').innerHTML=tt4+'%';
	document.getElementById('t6').innerHTML=t6+'%';
	document.getElementById('tt6').innerHTML=tt6+'%';
	document.getElementById('t8').innerHTML=t8+'%';
	document.getElementById('tt8').innerHTML=tt8+'%';
	document.getElementById('t10').innerHTML=t10+'%';
	document.getElementById('tt10').innerHTML=tt10+'%';
	document.getElementById('row3').innerHTML=row3+'%';
	document.getElementById('trow3').innerHTML=trow3+'%';

//set max size per row

	var row3max = trow3;
	if (row3 > trow3) row3max=row3;

//get row height

	var tdheight=document.getElementById('tdrow3').height*1;
	
	irow3=row3/row3max*tdheight;
	document.getElementById('irow3').height=irow3;
	itrow3=trow3/row3max*tdheight;
	document.getElementById('itrow3').height=itrow3;

	it2=t2/row3max*tdheight;
	document.getElementById('it2').height=it2;
	itt2=tt2/row3max*tdheight;
	document.getElementById('itt2').height=itt2;

	it4=t4/row3max*tdheight;
	document.getElementById('it4').height=it4;
	itt4=tt4/row3max*tdheight;
	document.getElementById('itt4').height=itt4;

	it6=t6/row3max*tdheight;
	document.getElementById('it6').height=it6;
	itt6=tt6/row3max*tdheight;
	document.getElementById('itt6').height=itt6;

	it8=t8/row3max*tdheight;
	document.getElementById('it8').height=it8;
	itt8=tt8/row3max*tdheight;
	document.getElementById('itt8').height=itt8;

	it10=t10/row3max*tdheight;
	document.getElementById('it10').height=it10;
	itt10=tt10/row3max*tdheight;
	document.getElementById('itt10').height=itt10;

//////ROW 4
	
	var t11=t[10];
	var t12=t[11];
	var row4=t11+t12;

	var tt11=tt[10];
	var tt12=tt[11];
	var trow4=tt11+tt12;

//write text percentages

	document.getElementById('t11').innerHTML=t11+'%';
	document.getElementById('tt11').innerHTML=tt11+'%';
	document.getElementById('t12').innerHTML=t12+'%';
	document.getElementById('tt12').innerHTML=tt12+'%';
	document.getElementById('row4').innerHTML=row4+'%';
	document.getElementById('trow4').innerHTML=trow4+'%';

//set max size per row

	var row4max = trow4;
	if (row4 > trow4) row4max=row4;

//get row height

	var tdheight=document.getElementById('tdrow4').height*1;
	
	irow4=row4/row4max*tdheight;
	document.getElementById('irow4').height=irow4;
	itrow4=trow4/row4max*tdheight;
	document.getElementById('itrow4').height=itrow4;

	it11=t11/row4max*tdheight;
	document.getElementById('it11').height=it11;
	itt11=tt11/row4max*tdheight;
	document.getElementById('itt11').height=itt11;

	it12=t12/row4max*tdheight;
	document.getElementById('it12').height=it12;
	itt12=tt12/row4max*tdheight;
	document.getElementById('itt12').height=itt12;
}

function CheckUncheck(FormName, FieldName, CheckValue)
{
	if(!document.forms[FormName]) {
		return;
	}
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes) {
		return;
	}
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes) {
		objCheckBoxes.checked = CheckValue;
	}
	else {
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++) {
			if (objCheckBoxes[i].disabled == false) {
				objCheckBoxes[i].checked = CheckValue;
			}
		}
	}
}

function HideUnhide(FilterName)
{
	document.getElementById(FilterName).style.display=(document.getElementById(FilterName).style.display== 'block')?'none':'block';
}

function OnSubmitForm(clientName)
{
	var str;
  if(document.pressed == 'Batch Report')
  {
	  document.clientusers.action ="admin_b.php?client="+clientName;
  }
  else
  if(document.pressed == 'Aggregate Report')
  {
	  document.clientusers.action ="admin_a3.php?client="+clientName;
  }
  else
	if(document.pressed == 'Save Changes' || document.pressed == 'Create')
  {
	  document.clientconf.action ="admin_c.php?client="+clientName;
  }
  else
	if(document.pressed == 'Cancel')
  {
	  document.clientconf.action ="admin_main.php";
	  document.bicform.action ="admin_c.php?client="+clientName;
	  document.clientdates.action ="admin_c.php?client="+clientName;
  }
  else
	if(document.pressed == 'Finish')
  {
	  document.filewiz.action ="admin_c.php?client="+clientName;
  }
  else
	if(document.pressed == 'Reset')
  {
	  document.filewiz.action ="admin_c1.php?client="+clientName;
  }
  else
	if(document.pressed == 'Next >>')
  {
	  document.nextpage.action = clientName;
  }
  else
	if(document.pressed == '<< Back')
  {
	  document.nextpage.action =clientName;
  }
//  document.clientusers.method = "post";
  return true;
}

function selAdder (yesno)
 { 
 
      nowsel = parseFloat(document.getElementById("recordsel").innerHTML) * 1;      
      if (yesno == 1) { 
           document.getElementById("recordsel").innerHTML = (nowsel+1); 
      } else { 
           document.getElementById("recordsel").innerHTML = (nowsel-1); 
      } 
 } 
  
 function BigselAdder (yesno, cnt, pgMaX) 
 { 
      nowsel = parseFloat(document.getElementById("recordsel").innerHTML) * 1; 
      pagemas = nowsel - (Math.floor(nowsel/pgMaX))*pgMaX; 
      if (yesno == 1) { 
           document.getElementById("recordsel").innerHTML = (nowsel+pgMaX - pagemas); 
      } else { 
           document.getElementById("recordsel").innerHTML = (nowsel-pgMaX + pagemas); 
      } 
}

function selAdder2 (oldsel)
{
	var objCheckBoxes = document.forms['clientusers'].elements['clnt[]'];
	if(!objCheckBoxes) {
		return;
	}
	var countCheckBoxes = objCheckBoxes.length;
	var nowsel = 0;
		for(var i = 0; i < countCheckBoxes; i++) {
			if (objCheckBoxes[i].checked == 1) {
				nowsel++;
			}
		}
	if (oldsel == 0){
		document.getElementById("recordsel").innerHTML = nowsel;
	} else {
		document.getElementById("recordsel").innerHTML = oldsel+nowsel;
	}
}

function OnSubmitAgg()
{
 	document.aggreport.action = "javascript:replace('agg_select.php', 'reportIO')";
//  document.clientusers.method = "post";
	return true;
}