function extractTrim(frm, name){
	return frm[name].value = trimStr(frm[name].value);
}

function valInsForm(frm){
	var hasErr = false;
	var fullName = extractTrim(frm, "fullName");
	var dob = extractTrim(frm, "dob");
	var occupation = extractTrim(frm, "occupation");
	var address = extractTrim(frm, "address");
	var phoneHome = extractTrim(frm, "phoneHome")
	var phoneWork = extractTrim(frm, "phoneWork")
	var phoneMobile = extractTrim(frm, "phoneMobile")
	var email = extractTrim(frm, "email");
	var propertySize = extractTrim(frm, "propertySize");
	var value = extractTrim(frm, "value");
	var sumContents = extractTrim(frm, "sumContents");
	
	// if residential investment property form
	if (document.location.href.indexOf('/residential_investment_property_form/') != -1)
	{
		if(!markValid("reqQuotTenancy", frm["tenancy"].selectedIndex != 0))
			hasErr = true;
	}
	
	// if holiday home form
	if (document.location.href.indexOf('/holiday') != -1)
	{
		var bedrooms = extractTrim(frm, "bedrooms");
		if(!markValid("reqQuotBedrooms", bedrooms.length != 0))
			hasErr = true;
	}

	if(!markValid("reqQuotName", fullName.length != 0))
		hasErr = true;
	if(!markValid("reqQuotDOB", dob.length != 0))
		hasErr = true;
	if(!markValid("reqQuotAddress", address.length != 0))
		hasErr = true;
	
	if (!markValid("reqQuotPhone", phoneHome.length != 0) && !markValid("reqQuotPhone", phoneWork.length != 0) && !markValid("reqQuotPhone", phoneMobile.length != 0)) 
		hasErr = true;

	if (email.length != 0 && !markValid("reqQuotEmail", validMail(email)))
		hasErr = true;
	
	
	if(!markValid("reqQuotTypeOfProperty", frm["typeOfProperty"].selectedIndex != 0))
		hasErr = true;
	
	if(!markValid("reqQuotPropertySize", propertySize.length != 0))
		hasErr = true;	
	
	if(!markValid("reqQuotValue", value.length != 0))
		hasErr = true;	
	
	if(!markValid("reqQuotSumContents", sumContents.length != 0))
		hasErr = true;		
	
	if ((!markValid("declarationExceptions", frm.decl1.checked == true) || !markValid("declarationExceptions", frm.decl2.checked == true)) && !markValid("declarationExceptions", frm.qualDec.value.length != 0))
		hasErr = true;
		
	
	return !hasErr;
}

function	displayRisks(display) {
	
	if (display == "on")
		document.getElementById("risksCover").style.display = "block";
	else
		document.getElementById("risksCover").style.display = "none";
}

var item = 1;

function addItem()	{
	
	item++;
	document.getElementById("item" + item).style.display = "block";
	
	if (item == 100)
		document.getElementById("AddAnotherItem").style.display = "none";
	else if (item == 1)
		document.getElementById("RemoveLastItem").style.display = "inline";
		
	return false;
}

function removeItem()	{
	document.getElementById("item" + item).style.display = "none";
	item--;
	
	if (item == 0)
		document.getElementById("RemoveLastItem").style.display = "none";
	if (item == 99)
		document.getElementById("AddAnotherItem").style.display = "inline";
	
	return false;
}

function secondAddress(bool){
	document.getElementById("reqCorAddress").style.display = (bool)? "inline" : "none";
}

function adjustReqText(ta){
	var oldRows = parseInt(ta.getAttribute("rows"));
	var txt = ta.value;
	if (txt.length == 0)
		rows = 3;
	else{
		rows = 2;
		var lines = ta.value.replace("\r\n","\n").replace("\r","\n").split("\n");
		for (var iter = 0; iter != lines.length; ++iter)
			rows += (Math.floor(lines[iter].length/38) + 1);
	}

	ta.setAttribute("rows",rows);
}

