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 ValidNum(itemID, vName)  
 {
  if(itemID)
  {
	  if (itemID.value.length>0)
	   {
			for (var i=0;i<itemID.value.length;i++)
		 	{
			  var ch=itemID.value.substring(i,i+1);
			  if ("0">ch || ch>"9") 
			  {
				  alert("Invalid " + vName ) ;
				  return false ;
			  }
			}
		}
  }
	return true ;
  }
 
 function ValidNumD(itemID, vName)  
 {
  if (itemID.value.length>0)
   {
    for (var i=0;i<itemID.value.length;i++)
     {
      var ch=itemID.value.substring(i,i+1);
      if (("0">ch || ch>"9")&& ch!="."&&ch!=" "&&ch!=",") 
	  {
		  alert("Invalid " + vName) ;
		  return false ;
	  }
     }
    }
	return true ;
  }
  
function IsMail(mailID)
{
	if( mailID.value.length > 0 )
	{
		var patrn = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
		if (!patrn.test(mailID.value)) 
		{
			alert("Invalid Email address") ;
			return false;
		}
	}
	return true;
}

function CheckConfirmPloicy()
{
	var chk = document.getElementById("confirmPloicy") ;
		
  	if(chk.checked)
	{
		//chk.className.replace('required', '') ;
		var obj = document.getElementById("policyDiv") ;
		obj.className = '';
		return true ;
	}
	else 
	{
		//chk.className = 'required';
		var obj = document.getElementById("policyDiv") ;
		obj.className = 'valueRequired1';
		alert('To complete the application form, you must read and agree to our Privacy Policy and Declaration. \n\nTo indicate your acceptance, click on the checkbox at the bottom of the page before hitting "Send Application".');
		

		return false ;
	}
}
function checkAppForm2(formObj) 
{
  var FormOK = true;
  var HomeOwnerOK = true;
  
  initWarningList();
  document.getElementById('warning').innerHTML = '';
  document.getElementById('warning').style.display = 'none';
  var CheckStatus = '';
  var Msg = '';
  var labels = formObj.getElementsByTagName('label');
  /*if (checkAgreement2('declareAuth1') == false) { FormOK = false; }
  if (checkAgreement2('declareAuth2') == false) { FormOK = false; }
  if (checkAgreement2('declareAuth3') == false) { FormOK = false; }
  if (FormOK == false) 
  {
      alert('To complete the application form, you must read and agree to our privacy policy and declaration. \n\nPlease read the privacy policy on this page and indicate your acceptance by clicking the check-boxes at the bottom of the page before hitting "Send Application".');
  }
  
  if(!CheckConfirmPloicy()) FormOK = false ;   
  if (!checkHomeOwner()) { HomeOwnerOK = false;}
  if (HomeOwnerOK==false) 
  {
   		FormOK = false; 
      	alert('Please specify if you own real estate property or you are paying off a mortgage.');
  }
  */
  if( ! ValidNumD( document.getElementById('LoanAmountInput'),'Loan Amount')|| ! ValidNumD( document.getElementById('RealEstate'),'Real Estate ')||! ValidNumD( document.getElementById('BalanceOwing'),'Balance Owing ')||!ValidNum( document.getElementById('HomeNumber'),'Home Number') || ! ValidNum( document.getElementById('WorkNumber'),'Work Number')|| ! ValidNum( document.getElementById('MobileNumber'),'Mobile Number') ||! ValidNum( document.getElementById('PostCode'),'Post Code')||!IsMail(document.getElementById('EmailAddress'))||!CheckRadioSelected("HaveProperty","HavePropertyID")) FormOK = false; 
	
  if (checkHomeOwner()) 
  { 
	/*var OwnPropertyOrNot = getCheckedValue(document.forms['applicationForm'].elements['ownProperty']);
  	if (OwnPropertyOrNot == "y") 
	{
	  	if (document.getElementById('RealEstate').value.length == 0) 
		{
			FormOK = false; 
	      	alert('Please enter the value of real estate property you own.');
			document.getElementById('RealEstate').className += ' required';
	    } 
		
		if (document.getElementById('BalanceOwing').value.length == 0) 
		{
			FormOK = false; 
	      	alert('Please enter the balance owing.');
			document.getElementById('BalanceOwing').className += ' required';
	    } 	
	}
	else
	{*/
		//alert(document.getElementById('RealEstate').className);
		document.getElementById('RealEstate').className.replace(' required', '');
		//alert(document.getElementById('RealEstate').className);
		document.getElementById('BalanceOwing').className.replace(' requiredd', '');
	/*}*/
  }	
  
  var labelName ;
  for (var i = 0; i < labels.length; i++) 
  {
  	if (labels[i].className) 
	{
      if (labels[i].className.indexOf('required') > -1) 
	  {
        if(labels[i].htmlFor) 
		{
		 if(document.getElementById(labels[i].htmlFor) )
		 {
			  labelName = document.getElementById(labels[i].htmlFor) ;
			  
			  CheckStatus = checkFormElement2(labelName);
			  //if(labelName.name =="State" ) alert(CheckStatus);
			  if (CheckStatus != 'OK') 
			  {
					FormOK = false;
					addWarning(getInnerText2(labels[i]).replace(':', ''));
					
					labelName.className += ' required';
			  } 
			  else 
			  {
					labelName.className = labelName.className.replace(' required', '');
			  }
		  }
		 }
        }
      }
    }
  
  //if (checkEmployment2('EmploymentType') == false) { FormOK = false; }
  if (FormOK == false) 
  {
    document.getElementById('warning').style.display = 'block';
    var warnMsg = 'Some required fields have not been filled in. They have been highlighted.'
    var warnP = document.createElement('p');
    var warnText = document.createTextNode(warnMsg);
    warnP.appendChild(warnText);
    warnP.id = 'warningText';
    document.getElementById('warning').appendChild(warnP);
//    document.getElementById('warning').appendChild(warningList);
    //alert(warnMsg);
    return false;
  } 
  else 
  {
    return true;
  }
}

function checkAgreement2(CheckBoxID) 
{
  var thisOK = true;
  if (document.getElementById(CheckBoxID)) 
  {
    thisOK = document.getElementById(CheckBoxID).checked;
    if(!thisOK) 
	{
      document.getElementById('declaration').className += ' required';
	  thisOK = false ;
    } 
	else 
	{
      document.getElementById('declaration').className = document.getElementById('declaration').className.replace(' required', '');
    }  
  }
  return thisOK;
}

function checkHomeOwner() 
{
	result = getCheckedValue(document.forms['applicationForm'].elements['ownProperty'])
	if (result == "")  return false;
	else {
		var obj = document.getElementById('ownProperty');
		if (obj) obj.className = obj.className.replace(' required', '');
		return true;
	}
 }

/*
function checkEmployment2(employmentTypeID) {

  return true;

  var thisOK = true;
  var CheckStatus = '';
  if (document.getElementById(employmentTypeID)) {
    var employmentType = document.getElementById(employmentTypeID).options[document.getElementById(employmentTypeID).selectedIndex].value
    if (employmentType == '') {
      return false;
    }
    if (employmentType != 'Unemployed') {
      var employmentFS = document.getElementById('EmploymentDetails');
      var labels = employmentFS.getElementsByTagName('label');
      for (var i = 0; i<labels.length; i++) {
        CheckStatus = checkFormElement(document.getElementById(labels[i].htmlFor));
        if (CheckStatus != 'OK') {
          thisOK = false;
          addWarning(getInnerText(labels[i]).replace(':', ''));
          document.getElementById(labels[i].htmlFor).className += ' required';
        } else {
          document.getElementById(labels[i].htmlFor).className = document.getElementById(labels[i].htmlFor).className.replace(' required', '');
        }
      }
    }
  }
  return thisOK;
}
*/

function checkFormElement2(FormElm) {
  if( FormElm.type )
  {
	  switch(FormElm.type)
	  {
		case "text":
		  if (FormElm.value.length == 0) return FormElm.id ;
		  break;
		//case "radio":
		//  if( getCheckedValue(FormElm).length == 0 ) return FormElm.name ;
		case "select-one":
		  if (FormElm.options[FormElm.selectedIndex].value.length == 0) return FormElm.id ;
		  break;
		case "textarea":
		  if (FormElm.value.length == 0) return FormElm.id ;
		  break;
	  }
	  return "OK";
  }
  else return "" ;
}

function getInnerText2(el) {
	if (el.innerText) return el.innerText;	//Not needed but it is faster

	var str = "";

	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				str += getInnerText2(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}

	}

	return str;
}

function CheckRadioSelected( radioName,tdName )
{
	var chkNum = 0 ;
	var obj = eval("document.applicationForm."+ radioName) ;
	if( obj )
	{
		for(var i = 0 ; i < obj.length ; i ++)
		{
			if( obj[i].checked ==true ) chkNum ++ ;
		}
		obj = document.getElementById(tdName) ;
		if( chkNum == 0&&obj)
		{
			obj.className ="valueRequired" ;
			return false ;
		}
		else
		{
			obj.className ="" ;
			return true ;
		}
	}
}

/*
function CheckALStatus()
{
	var ownPropertyChk = document.applicationForm.ownProperty ;
	var isChecked = true ;
	for( var i = 0 ; i < ownPropertyChk.length ; i ++ )
	{
		isChecked = ownPropertyChk[i].checked ;
	}
	return isChecked ;
}


function IsOwnProperty()
{
	var re = document.getElementById("RE_requiredSpan") ;
	var bo = document.getElementById("BO_requiredSpan") ;
	if(!CheckALStatus())
	{
		re.innerHTML ="<font color='#FF8700'>*</font>" ;
		bo.innerHTML ="<font color='#FF8700'>*</font>" ;
	}
	else
	{
		re.innerHTML ="" ;
		bo.innerHTML ="" ;
	}
}
*/