var quesno = 0;
var manArray = new Array() ;
var manStr = new String("");

//called to check Mandatory fields
function CheckMandatoryFields()
{	
	if (!CheckFields())
	{
		alert ("Please answer all the mandatory questions.");
		return false;
	}
	else
	{
		return true ;
	}
}
function CheckFields()
{
	var lenOfName;
	var ctrlName ;
	var aContent ;
	var ctrlNameHead ;
	var bChar ;
	var bChar2 ;
	var bThereIsValue ;
	var editCtrlName ;
	
	var strTemp ; //save as the control name
	var quesID ;
	var quesID2 ;
	var posX ;
	//var numberCtrl ;
	//numberCtrl = document.all.length ;
	//java array: 1--n
	//java string:0--n
	//alert (manArray.length);
	//alert (manArray[1]);
	//alert (manArray[2]);
	//alert (manStr);

	var anObj ;
	
	//The first five or 11 controls are not for questions
	var index = 0 ;//starting from 0 
		
	anObj = document.forms["theForm"];
	var count = document.forms["theForm"].elements.length ;
	
	for (i=0; i< quesno;i++)
	{
		if (manArray[i+1] == 0) //if no choice for the question, just continue
		{
			continue ;
		}		
		
		//whether it is Mandatory or not
		bChar = manStr.charAt(i) ;
		
//		alert (bChar) ;
//		alert ("index=" + index) ;
//		alert (ctrlName) ;

		//get control name
		strTemp = anObj.elements[index].name ;
		
		//for controls that are not for questions
		while (strTemp.substring(0,1) != "Q" && index < count) 
		{
			index++ ;
			strTemp = anObj.elements[index].name ;
			continue ;
		}
		//get question id
		quesID = getQuestionID(strTemp) ;
		quesID2 = quesID ;
		
//		alert (strTemp + " : " + quesID + " -- " + quesID2) ;
//		alert (bChar + " -- " + i) ;
		
		if (bChar == '0') //skip the first controls for this question
		{			
			while (quesID2 == quesID)
			{
				index++;
				if (anObj.elements[index] != null)
				{
					strTemp = anObj.elements[index].name ;
					quesID2 = getQuestionID(strTemp)
				}
				else
					break;
			}
		}
		else //Mandatory
		{			
			answer = 0;
			ctrlName = anObj.elements[index].type;		
			
			//This is the control type. 
			//alert (ctrlName);
			
			//check whether the current choice is selected or not
			while (quesID == quesID2)
			{
				switch (ctrlName)
				{
					case "textarea":
					{				
						aContent = anObj.elements[index].value;
														
						if (aContent != "")
						{
							answer = 1 ;
						}
						break;
					}
					case "checkbox":
					{
						if (anObj.elements[index].checked)
						{
							answer = 1;
						}
						break;
					}
					case "radio":
					{				
						if (anObj.elements[index].checked)
						{
							answer = 1;
						}
						break;
					}
					case "text":
					{
						aContent = anObj.elements[index].value;
						if (aContent != "")
						{
							answer = 1 ;
						}
						break;
					}
					case "select-one":
					{
						aContent = anObj.elements[index].selectedIndex;
						if (aContent != 0)
						{
							answer = 1 ;
						}
						break;
					}
					case "select-multiple":
					{
						for (j=0;j<anObj.elements[index].options.length;j++)
						{
							aContent = anObj.elements[index].options[j].selected;
							if (aContent)
							{
								answer = 1;
								break;
							}
						}
						break;
					}
					default:
						break;
				}
				
				if (answer == 1)
				{
					//skip other controls for this question
					while (quesID == quesID2)
					{
						index++;
						ctrlName = anObj.elements[index].type;
						strTemp = anObj.elements[index].name ;
						quesID2 = getQuestionID(strTemp)
					}
					break;
				}
				else
				{
					//get next control name and id
					index++;
					ctrlName = anObj.elements[index].type;
					strTemp = anObj.elements[index].name ;
					quesID2 = getQuestionID(strTemp)
				}
			}
			if (answer == 0) //not answered yet
				return false ;
		}
	}
	return true ;
}


function getQuestionID(strTemp)
{
	//The first parameter to the function is the character index at which to start, while the second parameter is the character index at which to end (non-inclusive): 	
	//checkbox, radio button, combox, list
	
	var quesID = 0;
	if (strTemp.substring(0,2) == "Q1") 
	{
		quesID = strTemp.substr(5) ;		
	}
	else //edit and memo fields
	{  
		pos = strTemp.indexOf("x", 5) ;
		quesID = strTemp.substring(5,pos) ; //0-based		
	}
	return quesID ;
}

function mppChkvalue(customerFlag)
{	
//	var v =	document.theForm.totalsales.value ;

//	var val = parseFloat(v)  ;

//	if (isNaN(val) || val == 0.0)
//	{
//		alert ("Please select any products first!") ;
//	
//		return false;
//	}

	return chkvalue(customerFlag) ;
}

function chkvalue(customerFlag)
{	
	
	//special case for bot checking forms
	if (document.theForm.bot_check) { 
		if (document.theForm.bot_check.value.toLowerCase() != "orange") {
			alert ("Please correctly fill out the Humanity Check") ;
			return false;
		}
	}
	
	if (customerFlag == 3)
	{
		if (document.theForm.firstname.value == "")
		{
			alert ("Please input the First Name.") ;
			return false ;
		}
		if (document.theForm.lastname.value == "")
		{
			alert ("Please input the Last Name.") ;
			return false ;
		}
		if (document.theForm.phone.value == "")
		{
			alert ("Please input the Phone Number.") ;
			return false ;
		}
		if (document.theForm.email.value == "")
		{
			alert ("Please input the Email Address.") ;
			return false ;
		}
		if (document.theForm.company.value == "")
		{
			alert ("Please input the Company Name.") ;
			return false ;
		}
	}
	else if (customerFlag > 0)
	{
		if (document.theForm.firstname.value == "")
		{
			alert ("Please input the First Name.") ;
			return false ;
		}
		if (document.theForm.lastname.value == "")
		{
			alert ("Please input the Last Name.") ;
			return false ;
		}
		if (document.theForm.phone.value == "")
		{
			alert ("Please input the Phone Number.") ;
			return false ;
		}
		if (document.theForm.email.value == "")
		{
			alert ("Please input the Email Address.") ;
			return false ;
		}
		if (document.theForm.company.value == "")
		{
			alert ("Please input the Company Name.") ;
			return false ;
		}
	}

	return true ;
}

function mppCheckall(customerFlag) 
{
	//alert ("here")
	if (mppChkvalue(customerFlag))
		return checkall(customerFlag) ;
	else
		return false ;
}  

function checkall(customerFlag) 
{
	//alert ("here")
	if (!chkvalue(customerFlag))
		return false ;

	return CheckMandatoryFields() ;
}  

