var hints = new Array('hh:mm','dd/mm/yy');

function defaultHints() {
	var toset = new Array(
			new Array('access_time','0'),
			new Array('access_date','1'),
			new Array('report_time','0'),
			new Array('report_date','1')
		);

	for(var count=0; count < toset.length; count++) {
		var obj = document.getElementById(toset[count][0]);
		var hint = hints[toset[count][1]];
		setHint(obj,hint);
	}
}

function setHint(obj,hint) {
	if((obj.value == "") || (obj.value == hint)) {
		obj.style.color = "#666666";
		obj.value = hint;
	}
}

function clearHint(obj) {
	var hintflag = false;

	for(var count=0; count < hints.length; count++) {
		if(obj.value == hints[count]) {
			hintflag = true;	
		}
	}

	var ckval = obj.value;
	if(hintflag) {
		obj.value = "";
		obj.style.color = "#000000";
	}
}

function sendApplication() {
	obj = document.getElementById('applyonline');
	mes = document.getElementById('apply_message');

	if(ckfields(obj)) {
		var query = buildQuery(obj);
		obj.style.display = "none";		
		
		mes.innerHTML = "<p>The your application has been mailed. We will get back to you shortly.</p>";
		mes.className = "message_success";

		attachFile('../js/sendapp.php',query);
	} else {
		mes.innerHTML = "<p>Please make sure that you have filled in all the required fields.</p>";
		mes.className = "message_failure";
	}

	window.location = "#";
}

function ckfields(obj) {	
	var tock = obj.getElementsByTagName('input');
	var ckflag = true;

	for(var count=0; count < tock.length; count++) {
		if(tock[count].type == "text") {
			if(tock[count].value == "") {
				ckflag = false;
				tock[count].className = "missing";
			} else {
				for(var hintcount=0; hintcount < hints.length; hintcount++) {
					if(tock[count].value == hints[hintcount]) {
						ckflag = false;	
						tock[count].className = "missing";
					}
				}
			}
		}
	}
	
	return ckflag;
}

function buildQuery(obj) {
	var querystr = "?do=send";

	var inputfields = obj.getElementsByTagName('input');
	for(var count=0; count < inputfields.length; count++) {
		var curfield = inputfields[count];
		if(curfield.type == "checkbox") {
			if(curfield.checked) {
				querystr += "&" + curfield.name + "=checked";	
			}
		} else if(curfield.type == "radio") {
			if(curfield.checked) {
				querystr += "&" + curfield.name + "=" + curfield.value;	
			}
		} else {
			querystr += "&" + curfield.name + "=" + curfield.value;			
		}
	}
	
	var selfields = obj.getElementsByTagName('select');
	for(var count=0; count < selfields.length; count++) {
		querystr += "&" + selfields[count].name + "=" + getSelected(selfields[count].id);
	}
	
	return querystr;
}

function getSelected(id) {
	var obj = document.getElementById(id);
	return obj.options[obj.selectedIndex].value;
}

function getQuote() {
	var jobtype = getSelected('jobtype');
	var furnishing = getSelected('furnishing');
	var rooms = getSelected('bedrooms') - 1;
	var price = 0;
	
	if(rooms > 0) {
		rooms--;	
	}
	
	if(jobtype == 4) {
		price = calcPrice(1,furnishing,rooms) + calcPrice(2,furnishing,rooms);
	} else if(jobtype == 3) {
		price = calcPrice(2,furnishing,rooms) / 2;
	} else {
		price = calcPrice(jobtype,furnishing,rooms);
	}

	price += 10 * calcExtras();

	//switchForm('onlinequote',true);
	//switchOpacity('onlinequote',0.5);

	document.getElementById('quotebox').style.display = "block";
	document.getElementById('quoteprice').innerHTML = price.toFixed(2);
}

function closeQuote() {
	document.getElementById('quotebox').style.display = "none";	
	//switchForm('onlinequote',false);
	//switchOpacity('onlinequote',1.0);
}

function calcPrice(jobtype,furnishing,rooms) {
	//Set prices for Inventory
	var prices_1 = new Array(
				new Array(75,85,110,124,145,185,220),
				new Array(70,80,95,110,130,155,200),
				new Array(65,75,90,100,120,145,190)
			);
	
	//Set prices for Interim, Check-Out, Check-In and Update (double)
	var prices_2 = new Array(
				new Array(55,66,85,91,103,109,121),
				new Array(45,55,65,75,85,95,105),
				new Array(42.5,50,57.5,65,72.5,80,87.5)
			);

	//Generate price
	return eval("prices_" + jobtype + "[" + furnishing + "][" + rooms + "]");
}

function calcExtras() {
	var opts = document.getElementById('extrarooms').getElementsByTagName('input');
	var roomcount = 0;
	
	for(var count=0; count < opts.length; count++) {
		if(opts[count].checked) {
			roomcount += eval(opts[count].value);
		}
	}
	
	return roomcount;
}

function switchForm(formid,flag) {
	objElems = document.getElementById(formid).elements;

	for(count=0; count < objElems.length; count++){
		objElems[count].disabled = flag;
	}
}

function switchOpacity(objid,opacity) {
	obj = document.getElementById(objid);

	obj.style.filter = "alpha(opacity=" + opacity * 100 +")";
	obj.style.opacity = opacity;
}

function attachFile(file,query) {
	var scriptObj = document.createElement('script');
	
	document.getElementsByTagName("head")[0].appendChild(scriptObj);
	scriptObj.language = 'javascript';
	scriptObj.src = file + query;
}