﻿function writeHTML(html)
{
	document.write(html);
}

function showSubMenu(parent, id)
{

	menuItemHide = false;
	if (menuItemCurrent && menuItemCurrent.id == "menuitem" + id)
		return;
	if (menuItemCurrent)
	{
		if (menuItemCurrent.filters && menuItemCurrent.filters.length)
			menuItemCurrent.filters[0].apply();
		menuItemCurrent.style.visibility = "hidden";
		if (menuItemCurrent.filters && menuItemCurrent.filters.length)
			menuItemCurrent.filters[0].play();
	}
	var item = document.getElementById("menuitem" + id);
	if (!item) return;
	var isRtl = document.body.style.direction == "rtl";
	item.style.top = _offsetTop(parent) + 36;
	

	//item.style.left =(document.body.offsetWidth-555) ;
	//item.style.left =(document.body.offsetWidth)/2-10 ;
	item.style.left = document.body.offsetWidth - item.offsetWidth - (document.body.offsetWidth)/2+350;

	
	if (item.filters && item.filters.length)
		item.filters[0].apply();
	item.style.visibility = "visible";
	if (item.filters && item.filters.length)
		item.filters[0].play();
	menuItemCurrent = item;
}
function hideSubMenu()
{
	if (!menuItemHide || !menuItemCurrent)
		return;
	if (menuItemCurrent.filters && menuItemCurrent.filters.length)
		menuItemCurrent.filters[0].apply();
	menuItemCurrent.style.visibility = "hidden";
	if (menuItemCurrent.filters && menuItemCurrent.filters.length)
		menuItemCurrent.filters[0].play();
	menuItemCurrent = null;
	menuItemHide = false;
}
var menuItemCurrent = null;
var menuItemHide = false;
var globalEvent;

function _offsetTop(obj)
{
	var top = 0;
	for (; obj; obj=obj.offsetParent)
		top += obj.offsetTop;
	return top;
}

function _offsetLeft(obj)
{
	var left = 0;
	for (; obj && obj != document.body; obj=obj.offsetParent)
		left += obj.offsetLeft;
	return left;
}

// returns true if node a contains event.toElement/relatedTarget.
function containsToElement(a)
{
	var b = globalEvent.toElement ? globalEvent.toElement : globalEvent.relatedTarget;
	if (!b) return false;
	while (b.parentNode)
		if ((b = b.parentNode) == a)
			return true;
	return false;
}

/*
	Validate a form's elements according to varius attributes
*/
function validateForm(form, elements, title)
{
	var message = "";
	for (var i = 0; i < elements.length; i++)
	{
		input = form.elements[elements[i]];
		// skip input when it's not rendered (ie. parent display:none)
		if (input.offsetHeight == 0) continue;
		valid = true;
		// Validate value according to element type and validation pattern
		switch (input.type)
		{
			case "text":
			case "password":
			case "textarea":
			case "file":
				if (input.getAttribute("mandatory"))
					if (input.value.length == 0)
						valid = false;
				if (input.value.length && input.getAttribute("pattern"))
				{
					var regExp = new RegExp();
					regExp.pattern = input.getAttribute("pattern");
					valid = regExp.test(input.value);
				}
				break;
			case "select-one":
			case "select-multiple":
				if (input.getAttribute("mandatory"))
					if (input.selectedIndex == 0)
						valid = false;
				break;
			case undefined:
				switch (input[0].type)
				{
					case "radio":
						if (input[0].getAttribute("mandatory"))
						{
							valid = false;
							for (var j = 0; j < input.length; j++)
								if (input[j].checked)
								{
									valid = true;
									break;
								}
						}
						input = input[0];
						break;
				}
				break;
		}
		if (!valid)
		{
			if (input.getAttribute("validationError"))
				message += input.getAttribute("validationError") + "\n";
			else
				message += "Error in field: " + input.name + "\n";
		}
	}
	if (message.length)
	{
		alert((title ? title + "\n" : "") + message);
		return false;
	}
	return false;
}
function submitSearchForm()
{
	document.location.href="73.html?q="+document.all["q"].value;
}

function submitSearchAgentForm()
{
	document.location.href="200.html?p="+document.all["AgentBox"].value;
}

	// data holder object

	var arrChild = new Array();
	var MAX_CHILD_NUM = 10;
	var arrParams = new Array();
	var arrMsg = new Array();
	var data = new objData();
	var doc = document.all;
	var round = true;
	var ERR_TARIFF_READ_CODE = "שגיאה בקריאת קוד תעריף";
	var ERR_TARIFF_FILE_LOAD = "";
	var strParams = null;
	
	

	function objData()
	{
		// input parameters
		this.clName = null;
		this.clIncome = null;
		this.clAge = null;
		this.clGender = null;
		this.clSmoke = null;
		this.clRetiermentAge = null;
		this.clAllowance = null;
		this.clAllowancePeriod = null;
		this.rate = null;
		
		this.spsName = null;
		this.spsIncome = null;
		this.spsAge = null;
		this.spsAllowance = null;
		this.spsAllowancePeriod = null;

		this.childName = new Array();
		this.childGender = new Array();
		this.childAllowance = new Array();
		this.childAllowancePeriod = new Array();

		this.totalPrime = null;
		this.totalCapital = null;

		// output prarameters
		this.tariff = null;
		this.clPv = null;
		this.clPrime = null;
		this.spsPv = null;
		this.spsPrime = null;
		this.childPv = new Array();
		this.childPrime = new Array();
		
		// functions
		this.getPv = pv;
		this.getPrime = prime;
		this.getPrimeRisk = primeRisk;
		this.getTariff = tariff;
		
		// external file
		this.tariffFile = "include\\tariff.xml";
	}


	function getParameters(arrParams)
	{	
		strParams = unescape(document.location.search);
		
		if ( strParams.length == 0 ) { return; }

		strParams = strParams.substring ( 1, strParams.length );
		arrParams = strParams.split ("&");

		var c = 0;
		for (i = 0; i < arrParams.length; i++)
		{
			var paramName = arrParams[i].split("=")[0];
			var paramValue = arrParams[i].split("=")[1];
			if ( paramValue == "" ) { paramValue = null; }
			switch ( paramName.toLowerCase() )
			{
				case 'clname': { data.clName = paramValue; break; }
				case 'clincome': { data.clIncome = paramValue; break; }
				case 'clage': { data.clAge = paramValue; break; }
				case 'clgender': { data.clGender = paramValue; break; }
				case 'clsmoke': { data.clSmoke = paramValue; break; }
				case 'clretage': { data.clRetiermentAge = paramValue; break; }
				case 'clallowance': { data.clAllowance = paramValue; break; }
				case 'clperiod': { data.clAllowancePeriod = paramValue; break; }
				case 'rate': { data.rate = paramValue; break; }
					
				case 'spname': { data.spsName = paramValue; break; }
				case 'spincome': { data.spsIncome = paramValue; break; }
				case 'spage': { data.spsAge = paramValue; break; }
				case 'spallowance': { data.spsAllowance = paramValue; break; }
				case 'spperiod': { data.spsAllowancePeriod = paramValue; break; }

				case 'childn' + parseInt(data.childName.length + 1):{
					data.childName[parseInt(data.childName.length)] = paramValue;
					break;
				}
				case 'childg' + parseInt(data.childGender.length + 1):{
					data.childGender[parseInt(data.childGender.length)] = paramValue;
					break;
				}
				case 'childa' + parseInt(data.childAllowance.length + 1):{
					data.childAllowance[parseInt(data.childAllowance.length)] = paramValue;
					break;
				}
				case 'childp' + parseInt(data.childAllowancePeriod.length + 1):{
					data.childAllowancePeriod[parseInt(data.childAllowancePeriod.length)] = paramValue;
					break;
				}

			}
		}
	}


	function pv(allowanceSum, periodYears, rateYearly)
	{	
		if ( isNaN ( allowanceSum ) || isNaN ( periodYears ) || isNaN ( rateYearly ) )
			return 0;
					
		periodYears = parseInt ( periodYears ) * 12;
		rateYearly = parseFloat ( rateYearly ) / 1200;
		presentValue = parseFloat ( allowanceSum );
		
		var result = 0;
		

		if(parseFloat(rateYearly) == 0)
			result = allowanceSum * periodYears;
		else
			result = allowanceSum * (  ( 1 - 1 / Math.pow ( ( 1 + rateYearly ), periodYears ) )  / rateYearly );
		

		result = parseFloat ( "0" + result );
		if ( round )
			result = Math.round ( result );
	
		return result;
	}
	
	function prime(presentValue, periodYears, rateYearly)
	{
		periodYears = parseInt ( periodYears ) * 12;
		rateYearly = parseFloat ( rateYearly ) / 1200;
		presentValue = parseFloat ( presentValue );
				

		var result = 0;
		
		if(parseFloat(rateYearly) == 0)
			result = presentValue / periodYears;
		else
			result = presentValue / ( ( Math.pow ( ( 1 + rateYearly ), periodYears ) - 1 ) / rateYearly );


		result = parseFloat ( "0" + result );
		if ( round )
			result = Math.round ( result );
		
		return result;
	
	}
	
	function primeRisk(presentValue, tariff)
	{
		var result = ( ( parseFloat ( presentValue ) / 1000 ) * parseFloat ( tariff ) ) / 12;

		result = parseFloat ( "0" + result );
		if ( round )
			result = Math.round ( result );
		
		return result;
	}
	
	function tariff(xmlFile, age, gender, smoke)
	{
		var tariffByCode = "3_9_5_" + gender + "_" + smoke;
		var tariffByAge = "fldAge_" + age;
		
		var xmlDoc = new ActiveXObject ("microsoft.XMLDOM");
		xmlDoc.async = false;
		if ( xmlDoc.load ( xmlFile ) )
		{
			var tariffCodeNode = xmlDoc.selectSingleNode("//tariff/code[@name='" + tariffByCode + "']");	
			
			if ( tariffCodeNode == null )
				arrMsg[arrMsg.length+1] = ERR_TARIFF_READ_CODE;
			else
			{
				if ( typeof ( tariffCodeNode ) != "object" )
					arrMsg[arrMsg.length+1] = ERR_TARIFF_READ_CODE;
				else
				{
					var tariffAgeNode = tariffCodeNode.selectSingleNode (tariffByAge);
					
					if ( tariffAgeNode == null )
						arrMsg[arrMsg.length+1] = ERR_TARIFF_READ_CODE;
					else
					{
						if ( typeof ( tariffAgeNode ) != "object" )
							arrMsg[arrMsg.length+1] = ERR_TARIFF_READ_CODE;
						else
							return(tariffAgeNode.text);
					}
				}
			}
		}
		else
		{
			arrMsg[arrMsg.length+1] = ERR_TARIFF_FILE_LOAD;
			return null;
		}
	}



	function init()
	{
		document.getElementById ("trClient").style.display = "block" ;
		document.getElementById ("trSpouse").style.display = "block" ;
		document.getElementById ("trChildren").style.display = "block" ;

		data.clName == null ? doc["clName"].value = "" : doc["clName"].value = data.clName;
		data.clIncome == null ? doc["clIncome"].value = "" : doc["clIncome"].value = data.clIncome;
		data.clAge == null ? doc["clAge"].value = "" : doc["clAge"].value = data.clAge;

		data.clGender == "M" ? doc["clGender"][0].checked = true : doc["clGender"][1].checked = true
		data.clSmoke == "S" ? doc["clSmoke"][0].checked = true : doc["clSmoke"][1].checked = true

		data.clAllowance == null ? doc["clAllowance"].value = "" : doc["clAllowance"].value = data.clAllowance;
		data.clAllowancePeriod == null ? doc["clPeriod"].value = "" : doc["clPeriod"].value = data.clAllowancePeriod;
		data.rate == null ? doc["Rate"].value = "3" : doc["Rate"].value = data.rate;

		data.spsName == null ? doc["spName"].value = "" : doc["spName"].value = data.spsName;
		data.spsIncome == null ? doc["spIncome"].value = "" : doc["spIncome"].value = data.spsIncome;
		data.spsAge == null ? doc["spAge"].value = "" : doc["spAge"].value = data.spsAge;

		data.spsAllowance == null ? doc["spAllowance"].value = "" : doc["spAllowance"].value = data.spsAllowance;
		data.spsAllowancePeriod == null ? doc["spPeriod"].value = "" : doc["spPeriod"].value = data.spsAllowancePeriod;

		if ( data.childName.length > 0 )
		{
			//aa
			document.all.lstNumChild.selectedIndex = data.childName.length ;
			for ( i = 1; i <= data.childName.length; i++ )
				arrChild[i] = new child( i , data.childName[i-1] , data.childGender[i-1] , data.childAllowance[i-1], data.childAllowancePeriod[i-1] );
		}


		for ( i = arrChild.length ; i <= MAX_CHILD_NUM; ++i )
			arrChild[i] = new child(i,"ילד" + i,"M","1000","10");

		setChildrenInputTable();

	}

	function initChildArray()
	{
		arrChild = new Array();
		for ( i = 0; i <= MAX_CHILD_NUM; ++i )
		{
			arrChild[i] = new child(i,"ילד" + i,"M","1000","10");
		}
	}

	function child(index, name, gender,allowance, allowancePeriod)
	{
		this.index = index;
		this.name = name;
		this.gender = gender;
		this.allowance = allowance;
		this.allowancePeriod = allowancePeriod;
	}


	function toggleDisplay(item)
	{
		var row = document.getElementById (item);
		if ( row.state == "open" )
		{
			row.style.display = "none";
			row.state = "close";
		}
		else
		{
			row.style.display = "block";
			row.state = "open";
		}
	}

	function setChildrenInputTable()
	{

		var curCount = document.all.tblChildrenInput.rows.length;

		// add requeired number of rows
		// to children input table
		for ( i = 0; i <= parseInt(document.all.lstNumChild.selectedIndex); ++i )
		{

			if ( curCount <= i )
			{
				var oRow = document.all.tblChildrenInput.insertRow();

				var oCell = oRow.insertCell();
				oCell.className = "childInputTable";
				oCell.innerHTML = getChildInputObject("N", i);
				var oCell = oRow.insertCell();
				oCell.className = "childInputTable";
				oCell.innerHTML = getChildInputObject("G", i);
				var oCell = oRow.insertCell();
				oCell.className = "childInputTable";
				oCell.innerHTML = getChildInputObject("A", i);
				var oCell = oRow.insertCell();
				oCell.className = "childInputTable";
				oCell.innerHTML = getChildInputObject("P", i);
			
			}
		}

		// delete overflow rows off children
		// input table
		while ( document.all.tblChildrenInput.rows.length > i )
		{
			document.all.tblChildrenInput.deleteRow (i);
		}

	}

	function getChildInputObject(inputType, index)
	{
		var strInputHTML = "";
		var strData = "";
		var strName = "child" + inputType + index;

		switch ( inputType.toLowerCase() )
		{
			case "n": // child name
			{

				strInputHTML = "<input type='text' id='" + strName + "' name='" + strName + "' value='" + arrChild[index].name + "' class='inputText' style='width:100%'>";
				break;
			}
			case "a": // child allowance sum
			{
				strInputHTML = "<input type='text' id='" + strName + "' name='" + strName + "' value='" + arrChild[index].allowance + "' class='inputSum' style='width:100%'>";
				break;
			}
			case "p": // child allowance period
			{
				strInputHTML = "<input type='text' id='" + strName + "' name='" + strName + "' value='" + arrChild[index].allowancePeriod + "' class='inputSum' style='width:100%'>";
				break;
			}
			case "g": // child gender
			{
				if ( arrChild[index].gender == "M" )
				{
					strInputHTML = "<span style='padding:0;width:70'><input type='radio' id='" + strName + "' name='" + strName + "' value='M' checked > זכר</span>";
					strInputHTML = strInputHTML +
								   "<span style='padding:0;width:70'><input type='radio' id='" + strName + "' name='" + strName + "' value='F' > נקבה</span>";
				}
				else
				{
					strInputHTML = "<span style='padding:0;width:70'><input type='radio' id='" + strName + "' name='" + strName + "' value='M'  > זכר</span>";
					strInputHTML = strInputHTML +
								   "<span style='padding:0;width:70'><input type='radio' id='" + strName + "' name='" + strName + "' value='F' checked> נקבה</span>";
				}
			}
		}


		return strInputHTML;
	}

	function validateInput()
	{
		if ( !validateClient() ) { return false; }
		if ( !validateSpouse() ) { return false; }
		if ( !validateChildren() ) { return false; }

		var l = setAction().toString();

		self.location = l;
		return true;
	}

	function setAction()
	{
		var strAction = "82.html?";

		for (i=0; i < document.all.length; i++)
		{
			strElement = "";

			var element = document.all[i];

			if ( typeof(element) != "undefined" )
			{
				switch ( element.type )
				{
					case "text":
					{
						strElement = element.name.toString() + "=" + element.value.toString() + "&";
						break;
					}
					case "radio":
					{
						rdoGroup = document.all[element.id];
						for (r=0; r<rdoGroup.length; r++)
						{
							if ( rdoGroup[r].checked == true )
							{
								element = rdoGroup[r];
								break;
							}
						}
						//alert("strAction.indexOf(" + element.id + ") == " + strAction.indexOf(element.id));
						if (strAction.indexOf(element.id) < 0 )
							strElement = element.name.toString() + "=" + element.value.toString() + "&";
						break;
					}
					case "hidden":
					{
						strElement = element.name.toString() + "=" + element.value.toString() + "&";
						break;
					}
					case "select-one":
					{
						strElement = element.name.toString() + "=" + element.value.toString() + "&";
						break;
					}
				}

				strAction = strAction.toString() + strElement.toString();
			}
		}
		strAction = strAction.substring(0,strAction.length-1);

		return strAction;
	}


	var ClientMinAge = 1;
	var ClientMaxAge = 64;

	function validateClient()
	{
		// validate client input data
		if ( document.all.clIncome.value.length == 0 )
		{
			alert("נא לעדכן ערך בשדה הכנסה ללקוח");
			document.all.clIncome.select();
			return false;
		}
		if ( isNaN ( document.all.clIncome.value ) == true )
		{
			alert("נא לעדכן ערך בשדה הכנסה ללקוח");
			document.all.clIncome.select();
			return false;
		}

		if ( document.all.clAge.value.length == 0 )
		{
			alert("נא לעדכן ערך בשדה גיל לקוח");
			document.all.clAge.select();
			return false;
		}
		if ( isNaN ( document.all.clAge.value ) == true )
		{
			alert("נא לעדכן ערך בשדה גיל לקוח");
			document.all.clAge.select();
			return false;
		}


		if (  document.all.clAge.value < ClientMinAge || document.all.clAge.value > ClientMaxAge)
		{
			alert("גיל הלקוח חייב להיות בין "+ClientMinAge+" לבין "+ClientMaxAge);
			document.all.clAge.select();
			return false;
		}


		if ( document.all.clAllowance.value.length == 0 )
		{
			alert("נא לעדכן ערך בשדה קצבה ללקוח");
			document.all.clAllowance.select();
			return false;
		}
		if ( isNaN ( document.all.clAllowance.value ) == true )
		{
			alert("נא לעדכן ערך בשדה קצבה ללקוח");
			document.all.clAllowance.select();
			return false;
		}


		if ( document.all.clPeriod.value.length == 0 )
		{
			alert("נא לעדכן ערך בשדה תקופת קצבה ללקוח");
			document.all.clPeriod.select();
			return false;
		}
		if ( isNaN ( document.all.clPeriod.value ) == true )
		{
			alert("נא לעדכן ערך בשדה תקופת קצבה ללקוח");
			document.all.clPeriod.select();
			return false;
		}


		if ( document.all.Rate.value.length == 0 )
		{
			alert("נא לעדכן ערך בשדה ריבית קצבה ללקוח");
			document.all.Rate.select();
			return false;
		}
		if ( isNaN ( document.all.Rate.value ) == true )
		{
			alert("נא לעדכן ערך בשדה ריבית קצבה ללקוח");
			document.all.Rate.select();
			return false;
		}

		return true;
	}

	function validateSpouse()
	{
		// validate spouse input data
		if ( document.all.spAllowance.value.length > 0 || document.all.spPeriod.value.length > 0 )
		{
			if ( isNaN ( document.all.spAge.value ) == true )
			{
				alert("נא לעדכן ערך בשדה גיל בן / ת זוג");
				document.all.spAge.select();
				return false;
			}


			if (  document.all.spAge.value < ClientMinAge || document.all.spAge.value > ClientMaxAge)
			{
				alert("גיל בן הזוג חייב להיות בין "+ClientMinAge+" לבין "+ClientMaxAge);
				document.all.spAge.select();
				return false;
			}

			if ( document.all.spAllowance.value.length == 0 )
			{
				alert("נא לעדכן ערך בשדה קצבה לבן / ת זוג");
				document.all.spAllowance.select();
				return false;
			}

			if ( isNaN ( document.all.spAllowance.value ) == true )
			{
				alert("נא לעדכן ערך בשדה קצבה לבן / ת זוג");
				document.all.spAllowance.select();
				return false;
			}

			if ( document.all.spPeriod.value.length == 0 )
			{
				alert("נא לעדכן ערך בשדה תקופת קצבה לבן / ת זוג");
				document.all.spPeriod.select();
				return false;
			}
			if ( isNaN ( document.all.spPeriod.value ) == true )
			{
				alert("נא לעדכן ערך בשדה תקופת קצבה לבן / ת זוג");
				document.all.spPeriod.select();
				return false;
			}


			if ( document.all.spIncome.value == "" ) { document.all.spIncome.value = 0 }
		}



		return true;
	}


	function validateChildren()
	{
		// validate children input data
		for ( i = 1; i < tblChildrenInput.rows.length; ++i )
		{
			var cName = document.getElementById ("childN" + i);
			var cGender = document.getElementById ("childG" + i);
			var cAllowance = document.getElementById ("childA" + i);
			var cAllowancePeriod = document.getElementById ("childP" + i);

			if ( cName.value.length == 0 ) { cName.value = "ילד / ה " + i; }

			if ( cAllowance.value.length == 0 )
			{
				alert("נא לעדכן קצבה ל" + cName.value);
				cAllowance.select();
				return false;
			}
			if ( isNaN ( cAllowance.value ) == true )
			{
				alert("נא לעדכן קצבה ל" + cName.value);
				cAllowance.select();
				return false;
			}

			if ( cAllowancePeriod.value.length == 0 )
			{
				alert("נא לעדכן קצבה תקופת קצבה ל" + cName.value);
				cAllowancePeriod.select();
				return false;
			}
			if ( isNaN ( cAllowancePeriod.value ) == true )
			{
				alert("נא לעדכן קצבה תקופת קצבה ל" + cName.value);
				cAllowancePeriod.select();
				return false;
			}

			/*
			if ( cAllowancePeriod.value < 10 )
			{
				alert("תקופת קצבה מינימלית של " + cName.value + " היא 10 שנים");
				cAllowancePeriod.value = 10;
				cAllowancePeriod.select();
				return false;
			}

			if ( cAllowancePeriod.value > 30 )
			{
				alert("תקופת קצבה מקסימלית של " + cName.value + " היא 30 שנים");
				cAllowancePeriod.value = 30;
				cAllowancePeriod.select();
				return false;
			}
			*/

		}//for
		return true;
	}//function

	function resetForm()
	{

		for ( i = 0; i < doc.length; ++i )
		{
			var element = doc[i];
			if ( element.tagName == "INPUT" || element.tagName == "SELECT" )
			{
				switch ( element.type )
				{
					case "text":
					{
						element.value = "";
						break;
					}
					case "select-one":
					{
						element.selectedIndex = 0;
						break;
					}
					case "radio":
					{
						//element.options[1].checked = true;
						break
					}
				}
			}
		}
		initChildArray();
		setChildrenInputTable();
	}


function displayContent(index)
{
if(document.all['c'+index].style.display=="none")
	document.all['c'+index].style.display="block";
else
	document.all['c'+index].style.display="none";
}











function setHome()
{
   document.body.style.behavior='url(#default#homepage)';
   document.body.setHomePage("http://www.agamin.co.il/");
}



function checkform(form)
{

return true;

}