﻿function isEmail(email) 
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) 
	{
		return false;
	}
	
	return true;
}

function Verify()
{
	GetObjectByIdValue("Nume").value = GetObjectByIdValue("Nume").value.replace(/[^a-z\- ]/i, "");
	GetObjectByIdValue("Prenume").value = GetObjectByIdValue("Prenume").value.replace(/[^a-z\- ]/i, "");
	GetObjectByIdValue("NrTel").value = GetObjectByIdValue("NrTel").value.replace(/[^0-9]/, "");
	GetObjectByIdValue("Suma").value = GetObjectByIdValue("Suma").value.replace(/[^0-9]/, "");

	if (GetObjectByIdValue("Prenume").value == "")	
	{
		alert ("Nu aţi completat prenumele!");
		GetObjectByIdValue("Prenume").focus();
		return false;
	}
	if (GetObjectByIdValue("Nume").value == "")	
	{
		alert ("Nu aţi completat numele!");
		GetObjectByIdValue("Nume").focus();
		return false;
	}
	if (GetObjectByIdValue("Email").value!="" && !isEmail (GetObjectByIdValue("Email").value))
	{
		alert ("Adresa de email nu este corectă!");
		GetObjectByIdValue("Email").focus();
		return false;
	}
	if (GetObjectByIdValue("NrTel").value == "" || GetObjectByIdValue("NrTel").value.length != 10 || !GetObjectByIdValue("NrTel").value.match(/^(\d+)$/) )
	{
		alert ("Nu aţi introdus un număr corect de telefon, din 10 cifre!");
		GetObjectByIdValue("NrTel").focus ();
		return false;
	}
	if (GetObjectByIdValue("Suma").value == "" || !GetObjectByIdValue("Suma").value.match(/^(\d+)$/))	
	{
		alert ("Nu aţi completat suma pe care doriţi să o solicitaţi!");
		GetObjectByIdValue("Suma").focus();
		return false;
	}	
	if ((GetObjectByIdValue("DistrictID").value == "") || (GetObjectByIdValue("DistrictID").value <= 0))
	{
		alert ("Trebuie sa alegeti un judeţ!");
		GetObjectByIdValue("DistrictID").focus();
		return false;
	}
	if ((GetObjectByIdValue("DistrictCityID").value == "") || (GetObjectByIdValue("DistrictCityID").value <= 0))
	{
		alert ("Trebuie sa alegeţi un oras!");
		GetObjectByIdValue("DistrictCityID").focus();
		return false;
	}
	if ((GetObjectByIdValue("MonedaID").value == "") || (GetObjectByIdValue("MonedaID").value <= 0))
	{
		alert ("Trebuie sa alegeţi moneda pentru suma solicitată!");
		GetObjectByIdValue("MonedaID").focus();
		return false;
	}
	if ((GetObjectByIdValue("ProductID").value == "") || (GetObjectByIdValue("ProductID").value <= 0))
	{
		alert ("Trebuie sa alegeţi tipul de finanţare dorit!");
		GetObjectByIdValue("ProductID").focus();
		return false;
	}

	return true;
}

function Form_Act()
{
	if (Verify ())
	{
		var objForm = GetObjectByIdValue("frm_consult");
		objForm.act.value = "save_data";
		objForm.submit();
		return true;
	}
	
	return false;
}


function changeDistrict ()
{
	var intDistrictID = new Number(GetObjectByIdValue("DistrictID").value);
	GetObjectByIdValue("DistrictCityID").disabled = (this.value == 0 ? true : false);
	this.changeDistrictResponse = changeDistrictResponse;
	getUrlXMLResponse('/common/user_controls/get_cities.aspx?judet_id=' + intDistrictID, null, self.changeDistrictResponse);
}
function changeDistrictResponse(xmlResponseXML)
{
	var root_node = getRootNode(xmlResponseXML);
	var cities = root_node.getElementsByTagName ("city");
	
	var objList = GetObjectByIdValue("DistrictCityID");
	for (i = objList.options.length; i >= 0; i--) objList.options[i] = null;
	for (i = 0; i < cities.length; i++)
	{
		objList.options[objList.options.length] = new Option(cities[i].attributes.getNamedItem("DistrictCity").value, cities[i].attributes.getNamedItem("DistrictCityID").value);
		if(cities[i].attributes.getNamedItem("DistrictCityID").value == intDistrictCityID + "")
		{
			objList.options[i].selected = true;
		}
	}
}

function ShowThanks ()
{
	GetObjectByIdValue ('show_credit_information').className = 'hide';
	GetObjectByIdValue ('show_thanks').className = '';
}

function HideThanks ()
{
	GetObjectByIdValue ('show_credit_information').className = '';
	GetObjectByIdValue ('show_thanks').className = 'hide';
}

function externalLinks()
{
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
	}
}