var syncProxy = new test();
var asyncProxy = new test (new callback());

function ProcessAjaxComponent (userRequest) {
	var loading = document.getElementById('HTML_AJAX_LOADING');
	if (loading) {
		loading.style.display = 'block';
	}
	asyncProxy.process_component(userRequest);
}

function LinkTo (url, friendlyname) {
	ProcessAjaxComponent (url); dhtmlHistory.add(url, friendlyname);
}

function doLogin () {
	var objTemp = new Object();
	objTemp['Username'] = document.getElementById('Username').value;
	objTemp['Password'] = document.getElementById('Password').value;
	asyncProxy.mainlogin(objTemp);
}

function initPage() {
	var arrLabels=document.getElementsByTagName("label");
	for (var i=0; i < arrLabels.length; i++) {
		var objTemp=arrLabels[i];
		var strFor=objTemp.getAttribute('for');
		if (strFor!='') {
			objTemp.setAttribute('id','l'+strFor);
			objTemp.setAttribute('classOrig',objTemp.getAttribute('class'));
		}
	}
	document.getElementById('username').focus();
}

function setElement (strElement,blnValidated) {
	var objElem = document.getElementById ('l'+strElement);
	if (objElem) {
		if (blnValidated==1) {
			strClass=objElem.getAttribute('classOrig');
		}else{
			strClass='error';}objElem.setAttribute('class',''+strClass);
		}return false;
	}

function toggleElement(strElement,blnVisible) {var objStyle=document.getElementById(strElement).style;if(objStyle){objStyle.display=(blnVisible==1)?'block':'none';}}

function showHideDiv(vis){
		if (vis) {
			document.getElementById('globalDiv').style.display='block';
			document.getElementById('shadow').style.width=document.body.offsetWidth;
			document.getElementById('shadow').style.height='1600px';
			document.getElementById('shadow').style.display='block';
			document.getElementById('globalDiv').style.top = '500px';
			document.getElementById('globalDiv').style.left = '200px';
		} else {
			document.getElementById('globalDiv').style.display='none';
			document.getElementById('shadow').style.display='none';
		}
		return false;
}

function UpdateHiddenDateField(Element, DatePart) {
// Updates the hidden date field using results from the day, month and year dropdowns
// Triggered by on onchange on the day, month and year dropdowns
	Form		= Element.form;
	Value		= Element.value;
	ElementName	= Element.name;
	ElementName	= ElementName.replace('_'+DatePart,'');
	Element		= Form.elements[ElementName];
	DateSTR		= Element.value; // Date is a reserved word
	DateARY		= DateSTR.split('-');
	ErrorText	= '';
	// Ensure not selecting 31st for say April
	if ( Form.elements[ElementName+'_Year'].value != '0000' && Form.elements[ElementName+'_Month'].value != '00' && Form.elements[ElementName+'_Day'].value != '00' ) {
		switch ( Form.elements[ElementName+'_Month'].value ) {
			// Feb
			case '02':
				/*
					leap years occur in years exactly divisible by four,
     				except that years ending in 00 are leap years
      				only if they are divisible by 400.
	  			*/
				if ( Form.elements[ElementName+'_Year'].value.search('00$') > -1 ) {
					if ( Form.elements[ElementName+'_Year'].value % 400 == 0 ) {
						NumDays = 29;
					} else {
						NumDays = 28;
					}
				} else {
					if ( Form.elements[ElementName+'_Year'].value % 4 == 0 ) {
						NumDays = 29;
					} else {
						NumDays = 28;
					}
				}
				if ( Form.elements[ElementName+'_Day'].value > NumDays ) {
					alert('There are a maximum of '+NumDays+' days in February '+Form.elements[ElementName+'_Year'].value);
					Form.elements[ElementName+'_Day'].selectedIndex = NumDays;
					if ( DatePart == 'Day' ) {
						Value = NumDays;
					} else {
						DateARY[2] = NumDays;
					}
				}
			break;
			// Apr
			case '04':
				if ( Form.elements[ElementName+'_Day'].value > 30 ) {
					alert('There are only 30 days in April');
					Form.elements[ElementName+'_Day'].selectedIndex = 30;
					if ( DatePart == 'Day' ) {
						Value = 30;
					} else {
						DateARY[2] = 30;
					}
				}
			break;
			// Jun
			case '06':
				if ( Form.elements[ElementName+'_Day'].value > 30 ) {
					alert('There are only 30 days in June');
					Form.elements[ElementName+'_Day'].selectedIndex = 30;
					if ( DatePart == 'Day' ) {
						Value = 30;
					} else {
						DateARY[2] = 30;
					}
				}
			break;
			// Sep
			case '09':
				if ( Form.elements[ElementName+'_Day'].value > 30 ) {
					alert('There are only 30 days in September');
					Form.elements[ElementName+'_Day'].selectedIndex = 30;
					if ( DatePart == 'Day' ) {
						Value = 30;
					} else {
						DateARY[2] = 30;
					}
				}
			break;
			// Nov
			case '11':
				if ( Form.elements[ElementName+'_Day'].value > 30 ) {
					alert('There are only 30 days in November');
					Form.elements[ElementName+'_Day'].selectedIndex = 30;
					if ( DatePart == 'Day' ) {
						Value = 30;
					} else {
						DateARY[2] = 30;
					}
				}
			break;
		}
	}

	switch (DatePart) {
		case 'Year':
			DateARY[0] = Value;
		break;
		case 'Month':
			DateARY[1] = Value;
		break;
		case 'Day':
			DateARY[2] = Value;
		break;
	}
	Element.value = DateARY[0] + '-' + DateARY[1] + '-' + DateARY[2];
}

function OnlyDecimals (which) {
//--- make sure that there are only two decimals
	var x = 0;
	x = which.value;
	var oldvalue = 0;
	oldvalue = which.value;
	var newvalue = '';
	var testing = 0;
	var i =0;
	for (i = 0; i < x.length; i++) {
	//--- go thru all of the characters
		switch (x.charAt (i)) {
			case "0" :
			case "1" :
			case "2" :
			case "3" :
			case "4" :
			case "5" :
			case "6" :
			case "7" :
			case "8" :
			case "9" :
			case "." :	newvalue = newvalue + x.charAt (i);
						if ((i < x.length) && (x.charAt (i) == '.')) {
							testing = x.substr (i, 100);
						}
						break;
				break;
			default : //--- here do nothing
				break;
		}; //--- end switch
	}
	which.value = newvalue;
	x = which.value;
	if (x.charAt (x.length - 1) == ".") {
		return true;
	} else {
		which.value = (Math.round (x * 100)) / 100;
		//--- add in here for the zero after decimal problem
		if ((testing == 0) && (testing != '') && (testing != '.')) {
			if (testing.length < 3) {
				which.value = which.value + testing;
			}
		}
		if (testing.length >= 2) {
		//--- if there are two or more numbers after the decimal
			if ((testing.charAt (2) == '0') && ((testing == 0.00) || (testing < 0.005)))  {
				which.value = which.value + '.00';
			}
		}
	}

	if (which.value == '0') {
		which.value = '';
	}
	if (oldvalue.length > 0) {
		if (oldvalue.charAt (0) == '0') {
			which.value = '0' + which.value;
		}
	}
	return true;
}

function OnlyIntegers (which) {
//--- make sure that there are only two decimals
	var x = 0;
	x = which.value;
	var oldvalue = 0;
	oldvalue = which.value;
	var newvalue = '';
	var testing = 0;
	var i =0;
	for (i = 0; i < x.length; i++) {
	//--- go thru all of the characters
		switch (x.charAt (i)) {
			case "0" :
			case "1" :
			case "2" :
			case "3" :
			case "4" :
			case "5" :
			case "6" :
			case "7" :
			case "8" :
			case "9" :	newvalue = newvalue + x.charAt (i);
						if ((i < x.length) && (x.charAt (i) == '.')) {
							testing = x.substr (i, 100);
						}
						break;
				break;
			default : //--- here do nothing
				break;
		}; //--- end switch
	}
	which.value = newvalue;
	x = which.value;
	if (x.charAt (x.length - 1) == ".") {
		return true;
	} else {
		which.value = (Math.round (x * 100)) / 100;
		//--- add in here for the zero after decimal problem
		if ((testing == 0) && (testing != '') && (testing != '.')) {
			if (testing.length < 3) {
				which.value = which.value + testing;
			}
		}
		if (testing.length >= 2) {
		//--- if there are two or more numbers after the decimal
			if ((testing.charAt (2) == '0') && ((testing == 0.00) || (testing < 0.005)))  {
				which.value = which.value + '.00';
			}
		}
	}

	if (which.value == '0') {
		which.value = '';
	}
	if (oldvalue.length > 0) {
		if (oldvalue.charAt (0) == '0') {
			which.value = '0' + which.value;
		}
	}
	return true;
}

function showHideDiv(vis){
	if (vis) {
		var height = 0;
		var width = 0;
		height = windowTotalHeight ();
		width = windowWidth ();

		if (height  > windowHeight ()) {
			document.getElementById('dummyTD').style.height = (height - windowHeight ()) + 'px';
		} else {
			document.getElementById('dummyTD').style.height = '0px';
		}
		document.getElementById('dummyTD').style.width = width + 'px';

		document.getElementById('globalDivTD').style.height = windowHeight () + 'px';
		document.getElementById('globalDivTD').style.width = width + 'px';

		document.getElementById('globalDiv').style.height = height + 'px';
		document.getElementById('globalDiv').style.width = width + 'px';
		document.getElementById('globalDiv').style.align = 'center';
		document.getElementById('globalDiv').style.valign = 'middle';
		document.getElementById('globalDiv').style.display='block';

		document.getElementById('shadow').style.width= width + 'px';
		document.getElementById('shadow').style.height= height + 'px';
		document.getElementById('shadow').style.display='block';
	} else {
		document.getElementById('globalDiv').style.display='none';
		document.getElementById('shadow').style.display='none';
	}
	return false;
}

function windowWidth() {
    var width = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		width = window.innerWidth;
		return parseInt(width);
    } else if( document.documentElement && document.documentElement.clientWidth ) {
		//IE 6+ in 'standards compliant mode'
		width = document.documentElement.clientWidth;
		return parseInt(width);
    } else if( document.body && document.body.clientWidth ) {
		//IE 4 compatible
		width = document.body.clientWidth;
    }
    return parseInt(width);
}

function windowTotalHeight () {
	var x,y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;
	var test3 = document.getElementById ('globalDivTD').clientHeight;

	if (test1 > test2) {
	//--- all but Explorer Mac
		y = document.body.scrollHeight;
	} else {
	// Explorer Mac and would also work in Explorer 6 Strict, Mozilla and Safari
		y = document.body.offsetHeight;
	}

	if (test3 > y) {
		y = test3;
	}

	return y;
}

function windowHeight() {
    var height = 0;
	if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		height = window.innerHeight;
		return parseInt(height);
    } else if( document.documentElement && document.documentElement.clientHeight ) {
		//IE 6+ in 'standards compliant mode'
		height = document.documentElement.clientHeight;
		return parseInt(height);
    } else if( document.body && document.body.clientHeight ) {
		//IE 4 compatible
		height = document.body.clientHeight;
    }
    return parseInt(height);
}

function setAlertText (alerttext) {
	document.getElementById ('layerdivtext').innerHTML = alerttext;
	showHideDiv (true);
}

function UpdateHiddenTimeField(Element, TimePart) {
// UpTimes the hidden Time field using results from the hour and minute dropdowns
// Triggered by on onchange on the hour and minute dropdowns
	Form		= Element.form;
	ElementName	= Element.name;
	ElementName	= ElementName.replace('_'+TimePart, '');
	Form.elements[ElementName].value = Form.elements[ElementName+'_Hour'].value+''+Form.elements[ElementName+'_Minute'].value;
}

function GoToLink (url, friendlyname) {
	ProcessAjaxComponent (url); dhtmlHistory.add(url, friendlyname);
}


function ProcessRebateForm () {
	var x;
	var objTemp=new Object();
	var myElements = document.getElementById ('rebateFRM').elements;
	for (x = 0; x < myElements.length; x++) {
		if (myElements[x].name == '' || myElements[x].id == '') {
			continue;
		}
		if (myElements[x].type == 'radio') {
			if (myElements[x].checked) {
				objTemp[''+myElements[x].name+''] = myElements[x].value;
			}
		} else {
			objTemp[''+myElements[x].name+''] = document.getElementById(myElements[x].id).value;
		}
	}
	asyncProxy.process_rebate_form (objTemp);
	return false;
}

function ProcessReferaFriend () {
	var x;
	var objTemp=new Object();
	var myElements = document.getElementById ('referralFRM').elements;
	for (x = 0; x < myElements.length; x++) {
		if (myElements[x].name == '' || myElements[x].id == '') {
			continue;
		}
		if (myElements[x].type == 'radio') {
			if (myElements[x].checked) {
				objTemp[''+myElements[x].name+''] = myElements[x].value;
			}
		} else {
			objTemp[''+myElements[x].name+''] = document.getElementById(myElements[x].id).value;
		}
	}
	asyncProxy.process_referafriend_form (objTemp);
	return false;
}

function doContact () {
	var x;
	var objTemp=new Object();
	var myElements = document.getElementById ('contactform').elements;
	for (x = 0; x < myElements.length; x++) {
		if (myElements[x].name == '' || myElements[x].id == '') {
			continue;
		}
		if (myElements[x].type == 'radio') {
			if (myElements[x].checked) {
				objTemp[''+myElements[x].name+''] = myElements[x].value;
			}
		} else {
			objTemp[''+myElements[x].name+''] = document.getElementById(myElements[x].id).value;
		}
	}
	asyncProxy.process_contact_form (objTemp);
	return false;
}

var lastclicked = 0;

function ClearNav () {
	var x = 0;
	var elemname = '';
	for (x = 0; x <= 100; x++) {
		elemname = 'nav_' + x;
		if (lastclicked == x) {
			document.getElementById (elemname).className = 'navlinkhover';
			continue;
		}
		if (document.getElementById (elemname)) {
			document.getElementById (elemname).className = 'navlink';			
		} else {
			break;
		}
	}
}



function CalculateRight () {
	var income = 0;
	var age = 0;
	var risk = 0;
	var place = 0;
	var control = 0;
	var totalscore = 0;
	
	income = document.getElementById ('income').value;
	age = document.getElementById ('age').value;
	risk = document.getElementById ('risk').value;
	place = document.getElementById ('place').value;
	control = document.getElementById ('control').value;
	
	if (income < 15000) {
		totalscore = totalscore + 0;
	} else
	if (income < 20000) {
		totalscore = totalscore + 1.5;
	} else
	if (income < 25000) {
		totalscore = totalscore + 1.75;
	} else
	if (income < 30000) {
		totalscore = totalscore + 2.00;
	} else
	if (income < 35000) {
		totalscore = totalscore + 2.25;
	} else
	if (income < 40000) {
		totalscore = totalscore + 2.5;
	} else
	if (income < 45000) {
		totalscore = totalscore + 2.75;
	} else
	if (income >= 45000) {
		totalscore = totalscore + 3;
	}
	
	if (age == '') {
		totalscore = totalscore + 0;
	} else 
	if (age < 20) {
		totalscore = totalscore + 5.5;
	} else
	if (age < 25) {
		totalscore = totalscore + 5.5;
	} else
	if (age < 27.5) {
		totalscore = totalscore + 5.5;
	} else
	if (age < 30) {
		totalscore = totalscore + 5.25;
	} else
	if (age < 32.5) {
		totalscore = totalscore + 5;
	} else
	if (age < 35) {
		totalscore = totalscore + 4.75;
	} else
	if (age < 37.5) {
		totalscore = totalscore + 4.5;
	} else
	if (age < 40) {
		totalscore = totalscore + 4.25;		
	} else
	if (age < 41) {
		totalscore = totalscore + 4;
	} else
	if (age < 43) {
		totalscore = totalscore + 3;	
	} else
	if (age < 44) {
		totalscore = totalscore + 2;
	} else
	if (age < 45) {
		totalscore = totalscore + 1.5;
	} else
	if (age < 50) {
		totalscore = totalscore + 1;
	} else
	if (age < 55) {
		totalscore = totalscore + 0.5;
	}
	
	switch (risk) {
		case 'low' : totalscore = totalscore -1; break;
		case 'medium' : totalscore = totalscore + 0; break;
		case 'high' : totalscore = totalscore + 1; break;
		case 'vhigh' : totalscore = totalscore + 1; break;
	};
	
	switch (place) {
		case 'Inside' : totalscore = totalscore - 1.5; break;
		case 'Outside' : totalscore = totalscore + 0; break;
	};
	
	switch (control) {
		case 'Me' : totalscore = totalscore + 0.5; break;
		case 'Govt' : totalscore = totalscore - 0.5; break;
	};
	
	if (totalscore < 0) {
		totalscore = 0;
	}
	
	if (income == '' || age == '' || risk == '' || place == '' || control == '') {
	//--- give a message to complete all the fields
		document.getElementById ('right_results').innerHTML = 'Please complete all the fields above to get a result displayed';	
		document.getElementById ('right_results').style['border'] = 'none';	
		document.getElementById ('right_results').style['color'] = '#000000';
		document.getElementById ('arrow_td').style['left'] = '2px';	
		document.getElementById ('arrow_td').style['left'] = (totalscore * 32) + 'px';
		//--- hide the whole table
		document.getElementById ('spectrum_table').style['display'] = 'none';
	} else {
	//--- allocate the score to a results div
		if (totalscore < 3.5) {
			document.getElementById ('right_results').innerHTML = '<b>Your Score: ' + totalscore + '</b><br><br><b>You will very likely be worse off by Contracting Out. People in your position usually don\'t claim an NI Rebate.</b>';	
			document.getElementById ('right_results').style['border'] = '2px #CC0000 solid';
			document.getElementById ('right_results').style['color'] = '#CC0000';
		} else
		if (totalscore < 5.75) {
			document.getElementById ('right_results').innerHTML = '<b>Your Score: ' + totalscore + '</b><br><br><b>Your decision to Contract Out is in the balance. You may be better or worse off. Play with some of the various questions to see how changing them may influence your decision. If you are still uncertain get advice.</b>';
			document.getElementById ('right_results').style['border'] = '2px #EE8833 solid';
			document.getElementById ('right_results').style['color'] = '#EE8833';
		} else
		if (totalscore >= 5.75) {
			document.getElementById ('right_results').innerHTML = '<b>Your Score: ' + totalscore + '</b><br><br><b>Most people in your position are better off by claiming the Rebate and Contracting Out.</b>';
			document.getElementById ('right_results').style['border'] = '2px #00CC00 solid';
			document.getElementById ('right_results').style['color'] = '#00CC00';
		}
		document.getElementById ('arrow_td').style['left'] = (totalscore * 32) + 'px';
		//--- show the whole table
		document.getElementById ('spectrum_table').style['display'] = '';
	}
	document.getElementById ('totalscore').innerHTML = totalscore;
}

