var sprytextfield3;
var sprytextfield4;
var sprytextfield5;

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 updateStatus() {
	if(confirm('Would you like to update the status?')){
		document.form1.submit();
	}
}

function doVat(total, state){
		var calcVAT = total/1.15;
		var calcVATEx = total-calcVAT;
		calcVATEx = calcVATEx.toFixed(2);
		calcVAT = calcVAT.toFixed(2);
		document.getElementById('vatShow').innerHTML = calcVATEx;
		document.getElementById('vatExShow').innerHTML = calcVAT;
}

function expenseFormat(){
	if(getCheckedValue(document.getElementById('recoverable'))=='0'){
		document.getElementById('vatShow').innerHTML = '';
		document.getElementById('vatExShow').innerHTML = '';
	}else{
		doVat(document.getElementById('value').value, 1);
	}
	
	if(getCheckedValue(document.getElementById('type2'))=='consumable'){
		document.getElementById('serial_number').disabled = true;
		document.getElementById('serial_number').className = "";
		if(sprytextfield3){
			sprytextfield3.reset();
			sprytextfield3.destroy();
		}
	}else{
		document.getElementById('serial_number').disabled = false;
		document.getElementById('serial_number').className = "imp_input";
		sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3");
	}
	
	if(document.form1.currency.options[document.form1.currency.selectedIndex].value=="£ - GBP"){
		document.getElementById('exchangeRate').disabled = true;
		document.getElementById('exchangeRate').className = "";
		if(sprytextfield4){
			sprytextfield4.reset();
			sprytextfield4.destroy();
		}
	}else{
		document.getElementById('exchangeRate').disabled = false;
		document.getElementById('exchangeRate').className = "imp_input";
		sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4");
	}
	
	if(document.form1.paidCom.options[document.form1.paidCom.selectedIndex].value=="Paid - BAC/CHAPS" || document.form1.paidCom.options[document.form1.paidCom.selectedIndex].value=="Paid - Paypal" || document.form1.paidCom.options[document.form1.paidCom.selectedIndex].value=="Paid - Cheque"){
		document.getElementById('chequeNum').disabled = false;
		document.getElementById('chequeNum').className = "imp_input";
		sprytextfield5 = new Spry.Widget.ValidationTextField("sprytextfield5");
	}else{
		document.getElementById('chequeNum').disabled = true;
		document.getElementById('chequeNum').className = "";
		if(sprytextfield5){
			sprytextfield5.reset();
			sprytextfield5.destroy();
		}
	}
	
	if(document.form1.paidCom.options[document.form1.paidCom.selectedIndex].value!="Paid - Cash" && document.form1.paidCom.options[document.form1.paidCom.selectedIndex].value!="Paid - Personal Card"){
		document.form1.expRec.selectedIndex = 1;
	}else{
		document.form1.expRec.selectedIndex = 0;
	}
}

function adminPopUp(URL, width, height) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=" + width +",height=" + height +",left = 440,top = 312');");
}
function updateNotes(type, targetForm) {
	if(type=='blur') {
		if(confirm('Would you like to update the notes?')){
			if(targetForm == "form2"){
				document.form2.submit();
			}else{
				document.form3.submit();
			}
		}
	}else{
		if(targetForm == "form2"){
			document.form2.submit();
		}else{
			document.form3.submit();
		}
	}
}

function printPopUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=10,height=10,left = 0,top = 0');");
}
function delete_check(delid, cat_1, cat_2, page){
	if (confirm("Are you sure you want to delete this record?")){
		location.href = page+"?delid="+delid+"&cat_1="+cat_1+"&cat_2="+cat_2;
		return true;
	}else{
		return false;
	}
}
function copyValue(source, target){
	target.selectedindex = source.selectedindex;
}
//Inits for design view...
function Init() { 
 	document.getElementById('iView').contentWindow.document.designMode = "on";
	//document.getElementById('iView').document.body.innerHTML="wertqwerqwer";
  	document.getElementById("iView").contentWindow.focus();
}
function content_init() {
	document.getElementById('iView').contentWindow.document.designMode = "on"; 
	//alert(document.getElementById('story_holder').innerHTML);
	setTimeout("document.getElementById('iView').contentWindow.document.body.innerHTML = document.getElementById('story_holder').innerHTML;",500);
	//alert(document.getElementById('iView').contentWindow.document.body.innerHTML);
	document.getElementById("iView").contentWindow.focus();
}
//-----------
function fixoldformating() {
	s = document.getElementById('story_holder').innerHTML;
	alert(s);
	//document.getElementById('iView').contentWindow.document.body.innerHTML = s.replace('*\n*', '<br />');
	out = "\n"; // replace this
	add = "<br />"; // with this
	temp = "" + s;

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	alert(temp);
	document.getElementById('iView').contentWindow.document.body.innerHTML = temp;
}
function preSend() {
	story_content = document.getElementById('iView').contentWindow.document.body.innerHTML;
	document.form1.story.value = story_content;
	//alert(document.form1.story.value);
	document.form1.submit();
}