/*Hack for IE to fire radio click event.*/
$(function () { if ($.browser.msie) { $('input:radio').click(function () {  this.blur(); this.focus(); });} });

$.validator.addMethod("australianDate", function(value, element) {
		
        return this.optional(element) || validDateFormat(value);  			        
    },"Please enter a date in the format dd/mm/yyyy and must be between year 1910 to current year"
);

$.validator.addMethod("otherSaluteCheck", function(value, element) {
	if($("#fcompanyDirectorsalute").val()=='Other') {						
		return this.optional(element) || /^[a-zA-Z]+$/i.test($("#fcompanyDirectorsalute").val());  			
	} else {
		$("#fcompanyDirectorsaluteOther").val("");
		return true;			
	}
}, "Please specify the salutataion (maximum 4 characters, only alphabetical values allowed)");

/*This function will uncheck the both raio buttons, like yes and no both will be unchecked*/
function unCheckRadioButton(groupName)
{
	var radios = document.getElementsByName(groupName);
	for(var i = 0; i < radios.length; i++)
	{
		if(radios[i].name==groupName)
			radios[i].checked = false;
	}
}

function removeFormField(obj, element){	
	var idToRemove=  $(obj).attr('id');
	//console.error("Remove id="+idToRemove);
	var count= document.getElementById(element+"Counter").value;		
	$("#div"+element+idToRemove).remove();		
	document.getElementById(element+"Counter").value = count-1;
}

function deleteRow(tableID, rowId, counterField) {
	try {
		$('#'+rowId).remove();
		var newTable = document.getElementById(tableID);		
		var id= newTable.rows.length;
		document.getElementById(counterField).value = id;						
	}catch(e) {
		alert(e);
	}
}

function checkIfOtherIsChecked(){
	return $("#aboutCuscalOthers").is(':checked');
}

function checkIfOtherSaluteIsselected(elementName){

	var otherSelect = $('#'+elementName).val();
	if(otherSelect=='Other')
		return true;
	else
		return false;
}


function checkIfShareholderIsChecked(){
	return ($("input[name=fcompShareHolders]:checked").val() == 'yes')
}

function checkIfTrusteeShareholderIsChecked(){
	return ($("input[name=ftcompShareHolders]:checked").val() == 'yes')
}

function checkIfBeneficiaryIsNo(){
	var val= $("input[name=ftrustBeneficiary]:checked").val();
	if(val=="undefined")
		return false;
	else
		return $("input[name=ftrustBeneficiary]:checked").val() == 'NO' ;
}
function checkIfBeneficiaryIsYes(){
	var val= $("input[name=ftrustBeneficiary]:checked").val();
	if(val=="undefined")
		return false;
	else
		return $("input[name=ftrustBeneficiary]:checked").val() == 'YES' ;
}

function checkIfParentIsChecked(parentWho){
	return $("#"+parentWho).is(':checked');
}

function abnAcnCheck(valType){				
	return ($("input[name=ABNyesNo]:checked").val() == valType)
}

function trusteeABNACNCheck(valType){				
	return ($("input[name=ftrusteeABNyesNo]:checked").val() == valType)
}

function organisationCheck(orgName){				
	return ($("input[name=forgtype]:checked").val() == orgName)
}

function fcompanyTypeCheck(name){				
	return ($('input[name=fcomType]:checked').val() == name)
}

function checkIfOrganisationIsCompany(){
	return organisationCheck('company');
}

function checkIfOrganisationIsTrust(){
	return organisationCheck('trust');
}

function checkIfOrganisationIsSoleTrader(){
	return organisationCheck('soletrader');
}

function checkIfFullBusinessAddressChecked(){
	return ($("input[name=fsolFullBusiness]:checked").val() == 'yes')	
}



function checkIfAddressIsSameChecked(){				
	if(checkIfOrganisationIsCompany())
	{
		return !$("#fpostaldiff").is(':checked');
	}else
		return false;				
}

function checkIfAddressIsSameCheckedTrust(){				
	if(checkIfTrustIsCompany())
	{
		return !$("#ftpostaldiff").is(':checked');
	}else
		return false;				
}


function checkIfAddressIsSameCheckedSoleTrader(){				
	if(checkIfOrganisationIsSoleTrader())
	{
		return !$("#fsolpostaldiff").is(':checked');
	}else
		return false;				
}

function checkIfTrustTypeIsOther(){
	return ($('input[name=ftrustType]:checked').val() == 'OtherTrust')
}
function checkIfTrustIsCompany(){
	return ($('input[name=fcompInd]:checked').val() == 'company')
}
function checkIfTrusteeIsCompany(){
	return ($('input[name=ftrusteesTypeDetail]:checked').val() == 'Companies')
}

function checkIfTrustIsProprietary (){
	return ($('input[name=ftcomType]:checked').val() == 'proprietary')
}

function checkTrustType(trustValue){	
	return $('input[name=ftrustType]:checked').val()== trustValue;	
}

function checkIfTrusteeType(value){

	return $('input[name=ftrusteesType]:checked').val() == value;
}

function checkIfDetailedTrusteeType(value){

	return $('input[name=ftrusteesTypeDetail]:checked').val() == value;
}
function companyPostalDiffUnchecked(){
	if(checkIfOrganisationIsTrust() && checkIfDetailedTrusteeType("Companies"))
	{
		return !$("#fComppostaldiff").is(':checked');
	}else
		return false;				
}

function updateBusinessInfoLabels(orgType){

	var $businessNameLabel = $("label[id='fregisteredname']");
	if(orgType=="COMPANY"){
		$businessNameLabel.html("Full Registered name of company (as registered by ASIC)<span class='mandatory'>*</span>");
	}else if(orgType=="TRUST"){
		$businessNameLabel.html("Full name of trust <span class='mandatory'>*</span>");
	}else if(orgType=="SOLETRADER"){
		$businessNameLabel.html("Full business name <span class='mandatory'>*</span>");
	}	
	
}

function validDateFormat(value){

	var dateVal= value.split("/");	
	var validDD= false;
	var validMM= false;
	var validYY= false;
	var currDate = new Date();	
	var currYear = currDate.getFullYear();
		
	if(/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(value)){
		
		if(parseInt(dateVal[0])<=31)
			validDD= true;
		
		if(parseInt(dateVal[1])<=12)
			validMM= true;
		
		if(parseInt(dateVal[2])>=1910 && parseInt(dateVal[2])<=currYear)
			validYY= true;			
		
		if(validDD && validMM && validYY)
			return true;
		else
			return false;			
	}else
		return false;		
}

