var no_of_ben=0;
var no_of_des=0;
function validate_client_info(form){	
	if(isEmpty(form.company_name) || (form.company_name.value == "Enter company name here"))
	{ 
		alert("Please enter company name");
		form.company_name.focus();
		return false; 
	}
	if(isEmpty(form.email) || (form.email.value == "Enter your email address here"))
		{ 
			alert("Please enter your email address");form.email.focus();return false; 
		}
		else{
			if(!isEmailAddress(form.email))
			{
				alert("Please enter a valid email address");form.email.focus();return false;
			}		
		}
	//alert(form.email_1.value);
	if(!isEmpty(form.email_1) && (form.email_1.value != "Enter email one here"))
	{ 
		if(!isEmailAddress(form.email_1))
		{
			alert("Please enter a valid email address 1");form.email_1.focus();return false;
		}	
	}
	if(!isEmpty(form.email_2) && (form.email_2.value != "Enter email two here"))
	{ 
		if(!isEmailAddress(form.email_2))
		{
			alert("Please enter a valid email address 2");form.email_2.focus();return false;
		}	
	}
	if(!isEmpty(form.email_3) && (form.email_3.value != "Enter email three here"))
	{ 
		if(!isEmailAddress(form.email_3))
		{
			alert("Please enter a valid email address 3");form.email_3.focus();return false;
		}	
	}
	if(!isEmpty(form.email_4) && (form.email_4.value != "Enter email four here"))
	{ 
		if(!isEmailAddress(form.email_4))
		{
			alert("Please enter a valid email address 4");form.email_4.focus();return false;
		}	
	}
	gostep(1,7);
	document.getElementById("txt_des").focus();
return true; 
}
//------------------step 1 ------------------------
function validate_des_info(form1){
	if(isEmpty(form1.txt_des) || (form1.txt_des.value == "Enter brief description here"))
	{ 
		alert("Please enter brief description");
		form1.txt_des.focus();
		return false; 
	}
	gostep(2,7);
	document.getElementById("txt_decision1").focus();
return true; 
}
//------------------step 1 ------------------------
//------------------step 2 ------------------------
function validate_decision_info(form){
	
	no_of_des=0;
	var i=1;
	var k=2;
	var s=0;
	for(i=1;i<=5;i++)
	{
		if(!isEmpty(form["txt_decision"+i]) && (form["txt_decision"+i].value != ("Option "+i)))
		{ 
			s=s+1;
		}
	}
	if(s<2)
	{
		if(isEmpty(form["txt_decision"+1]) || (form["txt_decision"+1].value == ("Option "+1)))
		{ 
			alert("Please enter atleast two options");
			form["txt_decision"+1].focus();
			return false; 
		}
		if(isEmpty(form["txt_decision"+2]) || (form["txt_decision"+2].value == ("Option "+2)))
		{ 
			alert("Please enter atleast two options");
			form["txt_decision"+2].focus();
			return false; 
		}
	}
	for(i=1;i<=5;i++)
	{
		if(!isEmpty(form["txt_decision"+i]) && (form["txt_decision"+i].value != ("Option "+i)))
		{ 
			no_of_des=no_of_des+1;
		}
	}
	gostep(3,7);
	document.getElementById("txt_benefit_1").focus();
return true; 
}
//------------------step 2 ------------------------
//------------------step 3 ------------------------
function validate_benefit_info(form){
	var i=1, s=0, count=1;
	form1="frm4";
	for(i=1;i<=5;i++)
	{
		if(!isEmpty(form["txt_benefit_"+i]) && (form["txt_benefit_"+i].value != ("Factor "+i)))
		{ s=s+1; }
	}
	if(s<2)
	{
		for(i=1;i<=2;i++)
		{
			if(isEmpty(form["txt_benefit_"+i]) || (form["txt_benefit_"+i].value == "Factor "+i))
			{ 
				alert("Please enter atleast two factors");
				form["txt_benefit_"+i].focus();
				return false; 
			}
		}
	}
	no_of_ben=0;
	for(i=1;i<=5;i++)
	{
		if(!isEmpty(form["txt_benefit_"+i]) && (form["txt_benefit_"+i].value != ("Factor "+i)))
		{ no_of_ben=no_of_ben + 1; }
	}
	
	for(i=1;i<=5;i++)
	{
		document.getElementById("s_1"+i).style.display="";
		document.getElementById("s_2"+i).style.display="";
		document.getElementById("s_3"+i).style.display="";
		document.getElementById("s_4"+i).style.display="";
	}
	
	for(i=no_of_ben+1;i<=5;i++)
	{
		document.getElementById("s_1"+i).style.display="none";
		document.getElementById("s_2"+i).style.display="none";
		document.getElementById("s_3"+i).style.display="none";
		document.getElementById("s_4"+i).style.display="none";
	}
	for(i=1;i<=5;i++)
	{
		if(!isEmpty(form["txt_benefit_"+i]) && (form["txt_benefit_"+i].value != ("Factor "+i)))
		{
			document.getElementById("txt_benefit_2"+count).value = form["txt_benefit_"+i].value;
			count=parseInt(count)+1;
		}
	}
	gostep(4,7);
	document.getElementById("txt_weight_1").focus();
return true; 
}

//------------------step 3 ------------------------

//------------------step 4 ------------------------

function weight_change(form)
{
	var i=1;
	
	
	for(i=1;i<=no_of_ben;i++)
	{
		if((form["txt_weight_"+i].value == "0"))
		{ 
			alert("Please enter valid weight"+i);
			form["txt_weight_"+i].focus();
			form["txt_weight_"+i].value="";
			return false; 
		}
		if((form["txt_weight_"+i].value < 0) || (form["txt_weight_"+i].value > 100))
		{ 
			alert("Please enter the value between 1 and 100");
			form["txt_weight_"+i].focus();
			form["txt_weight_"+i].select();
			return false; 
		}
	}
	var total=0;
	for(i=1;i<=no_of_ben;i++)
	{
		total=total+parseFloat(form["txt_weight_"+i].value);
	}
	form["txt_weight_tot"].value = total;
	if(total != 100)
	{
		alert("The weight total must equal to 100");
		form["txt_weight_"+1].focus();
		return false; 
	}
	for(i=1;i<=no_of_ben;i++)
	{
		document.getElementById("txt_benefit_3"+i).value = form["txt_benefit_2"+i].value;
		document.getElementById("txt_weight_3"+i).value = parseFloat(form["txt_weight_"+i].value);
		document.getElementById("txt_benchmark_3"+i).value = parseFloat(form["txt_weight_"+i].value)/2;
	}
	document.getElementById("txt_weight_tot_1").value = form["txt_weight_tot"].value;
	document.getElementById("txt_benchmark_tot_1").value = form["txt_weight_tot"].value/2;
	for(i=3;i<=5;i++)
	{
		for(j=0;j<=6;j++)
		{
			document.getElementById("c_"+i+""+j).style.display="";
			document.getElementById("fc_"+i+""+j).style.display="";
		}
	}
	
	for(i=no_of_des+1;i<=5;i++)
	{
		for(j=0;j<=6;j++)
		{
			document.getElementById("c_"+i+""+j).style.display="none";
			document.getElementById("fc_"+i+""+j).style.display="none";
		}
	}
	gostep(5,7);
	return true; 
}
function caltotal(form,id)
{
	var total=0,x;
	for(i=1;i<=no_of_ben;i++)
	{
		x=document.getElementById("txt_weight_"+i).value;
		if(x ==""){x=0;}
		total = parseFloat(total) + parseFloat(x);
	}
	document.getElementById("txt_weight_tot").value = parseInt(total);
	if(total > 100)
	{
		alert("The weight total must equal to 100");
		aField = document.getElementById('txt_weight_'+id); 
		setTimeout("aField.focus()", 50);
		return false;
	}
}

//------------------step 4 ------------------------
//------------------step 5 ------------------------
function calcfun(form)
{
	var count=1;
	for(var i=1;i<=no_of_ben;i++)
	{
		document.getElementById("f_txt_benefit_"+i).value = form["txt_benefit_3"+i].value;
		document.getElementById("f_txt_weight_"+i).value = form["txt_weight_3"+i].value;
		document.getElementById("txt_decision_"+i+"1").value = form["txt_benchmark_3"+i].value;
		
	}
	for(var i=1;i<=5;i++)
	{
		if(!isEmpty(document.getElementById("txt_decision"+i)) && (document.getElementById("txt_decision"+i).value != ("Option "+i)))
		{ 
			document.getElementById("lbl_decision"+count).innerHTML = document.getElementById("txt_decision"+i).value;
			document.getElementById("lbl_decision_"+count).innerHTML = document.getElementById("txt_decision"+i).value;
			document.getElementById("h_decision_"+count).value = document.getElementById("txt_decision"+i).value;
			count=parseInt(count)+1;
		}
	}
	document.getElementById("lbl_decision").innerHTML = document.getElementById("txt_des").value;
	document.getElementById("f_decision").innerHTML = document.getElementById("txt_des").value;
	document.getElementById("h_decision").value = document.getElementById("txt_des").value;
	document.getElementById("f_weight_tot").value = document.getElementById("txt_weight_tot").value;
	document.getElementById("f_decision_tot1").value = document.getElementById("txt_weight_tot").value/2;
	gostep(6,7);
	//document.getElementById("txt_decision_21").focus();
	return true; 
}

//------------------step 6 ------------------------

function weight_cal(form, item)
{
	var i=item;
	var fieldName	=	'txt_decision_'+i;
	aField = document.getElementById('txt_decision_'+i); 
	var j=parseInt(i/10);
	if((parseFloat(form["txt_decision_"+i].value) < 0) || (parseFloat(form["txt_decision_"+i].value) > parseFloat(form["f_txt_weight_"+j].value)))
	{ 
		alert("Please enter the value between 1 and the weight value");
		setTimeout("aField.focus()", 50);
		return false; 
	}
}
function weight_cal_final(form)
{
	//var i=item
	var s=10,m=10,x;
	var fieldName;
	var aField;


	for(var i=1;i<=no_of_des;i++)
	{
		for(j=1;j<=no_of_ben;j++)
		{
			k=j+""+i;
			if((parseFloat(document.getElementById("txt_decision_"+k).value) <= 0) || (parseFloat(document.getElementById("txt_decision_"+k).value) > parseFloat(document.getElementById("f_txt_weight_"+j).value)))
			{ 
				alert("Please enter the value between 1 and the weighting value");
				document.getElementById("txt_decision_"+k).focus();
				return false; 
			}
		}
	}
	var d_total=0;
	var i;var k;
	for(i=1;i<=no_of_des;i++)
	{
		d_total=0;
		for(var j=1;j<=no_of_ben;j++)
		{
			
			x=document.getElementById("txt_decision_"+j+""+i).value;
			if(x ==""){x=0;}
			d_total=parseFloat(d_total)+parseFloat(x);
		}
		document.getElementById("f_decision_tot"+i).value=d_total;
		document.getElementById("rf_decision_tot"+i).value=d_total;
	}
	for(i=1;i<=no_of_des;i++)
	{
		d_total=0;
		for(var j=1;j<=no_of_ben;j++)
		{
			
			k=j+""+i;
			document.getElementById("rf_decision_"+k).value = document.getElementById("txt_decision_"+k).value;
		}
	}
	var count=1;
	for(var i=1;i<=no_of_ben;i++)
	{
		document.getElementById("rf_benefit_"+i).value = document.getElementById("txt_benefit_3"+i).value;
		document.getElementById("rf_weight"+i).value = document.getElementById("f_txt_weight_"+i).value;
	}
	document.getElementById("rf_weight_tot").value = document.getElementById("txt_weight_tot").value;
	gostep(7,7);
	document.getElementById("c_name").value = document.getElementById("company_name").value;
	document.getElementById("c_email").value = document.getElementById("email").value;
	if(!isEmpty(document.getElementById("email_1")) && (document.getElementById("email_1").value != "Enter email one here"))
	{ 
		if(isEmailAddress(document.getElementById("email_1")))
		{
			document.getElementById("c_email1").value = document.getElementById("email_1").value;
		}	
		else{
			document.getElementById("c_email1").value ="";
		}
	}
	if(!isEmpty(document.getElementById("email_2")) && (document.getElementById("email_2").value != "Enter email two here"))
	{ 
		if(isEmailAddress(document.getElementById("email_2")))
		{
			document.getElementById("c_email2").value = document.getElementById("email_2").value;
		}	
		else{
			document.getElementById("c_email2").value ="";
		}
	}
	if(!isEmpty(document.getElementById("email_3")) && (document.getElementById("email_3").value != "Enter email three here"))
	{ 
		if(isEmailAddress(document.getElementById("email_3")))
		{
			document.getElementById("c_email3").value = document.getElementById("email_3").value;
		}	
		else{
			document.getElementById("c_email3").value ="";
		}
	}
	if(!isEmpty(document.getElementById("email_4")) && (document.getElementById("email_4").value != "Enter email four here"))
	{ 
		if(isEmailAddress(document.getElementById("email_4")))
		{
			document.getElementById("c_email4").value = document.getElementById("email_4").value;
		}	
		else{
			document.getElementById("c_email4").value ="";
		}
	}
	max=0;
	for(i=1;i<=no_of_des;i++)
	{
		f_decision_tot =  document.getElementById("f_decision_tot"+i).value;

		if( max < f_decision_tot)
		{
			max=f_decision_tot;
			opt=document.getElementById("h_decision_"+i).value;
		}
		else if( max == f_decision_tot)
		{
			opt=opt+", "+document.getElementById("h_decision_"+i).value;
		}
	}
	
	

	document.getElementById("ovalue").innerHTML=max;
	document.getElementById("option").innerHTML=opt;
	document.getElementById("max_option").value=opt;
	document.getElementById("max_val").value=max;
	document.getElementById("r_counts").value = no_of_ben;
	document.getElementById("counts").value = no_of_des;
	document.getElementById("action_plan_1").focus();
	return true; 
}
function fcaltotal(form)
{
	var d_total=0;
	var i,x;
	for(i=1;i<=no_of_des;i++)
	{
		d_total=0;
		for(var j=1;j<=no_of_ben;j++)
		{
			
			x=document.getElementById("txt_decision_"+j+""+i).value;
			if(x ==""){x=0;}
			d_total=parseFloat(d_total)+parseFloat(x);
		}
		document.getElementById("f_decision_tot"+i).value=d_total;
		document.getElementById("rf_decision_tot"+i).value=d_total;
	}
}
//------------------------------Step 7----------------------------
function validate_action_info(form)
{
	if((!isEmpty(form.date_1)) || (!isEmpty(form.person_1) && (form.person_1.value != "Enter here")))
	{ 
		if(isEmpty(form.action_plan_1) || (form.action_plan_1.value == "Enter here"))
		{ 
			alert("Please enter action one");
			form.action_plan_1.focus();
			return false; 
		}
	}
	
	if((!isEmpty(form.date_2)) || (!isEmpty(form.person_2) && (form.person_2.value != "Enter here")))
	{ 
		if(isEmpty(form.action_plan_2) || (form.action_plan_2.value == "Enter here"))
		{ 
			alert("Please enter action two");
			form.action_plan_2.focus();
			return false; 
		}
	}

	if((!isEmpty(form.date_3)) || (!isEmpty(form.person_3) && (form.person_3.value != "Enter here")))
	{ 
		if(isEmpty(form.action_plan_3) || (form.action_plan_3.value == "Enter here"))
		{ 
			alert("Please enter action three");
			form.action_plan_3.focus();
			return false; 
		}
	}
	captchaValidation(form);
  return false; 
}


//-----------------------------------------------------------
function blockNonNumbers(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
	allowNegative=false;

	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	if (isNaN(key)) return true;
	keychar = String.fromCharCode(key);
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || key == 13 || isCtrl)
	{
		return true;
	}
	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
	return isFirstN || reg.test(keychar);
}

function getURL()
{
	url		=	document.location.href;
	x		=	url.indexOf(".");
	x		=	url.indexOf("/",x);
	return url.substring(0,x);
}

function processCaptchaResult(responseTxt)
{	
	if(responseTxt == "true")
	{	
		document.getElementById("error").display="none";
		document.frm8.submit();
		
	}else
	{
		document.getElementById("error").style.display="block";
		document.getElementById("captcha").value="";
		document.getElementById("captcha").focus();
		//document.getElementById("captcha_img").src="captcha.php";
		document.getElementById("captIMG").innerHTML=responseTxt;
		return false;
	}
	
	
}

//------------------------------------------------------

function captchaValidation(frm)
{

	if(isEmpty(frm.captcha))
	{
		alert('Please enter the security code');
		frm.captcha.focus();
		return false;
	}

	process		=	'validateCaptcha';
	output		=	'text';
	url		=	getURL()+ "/inc/validate_captcha.php";
	
	responseHandler	=	"processCaptchaResult";

	captchaCode	=	encodeURI(frm.captcha.value);

	qs			=	"code="+captchaCode;
	
	ajaxPostRequest(url,qs);
}

