﻿


var Contact = new Object();
var IsContantFormValid = false;
var IsContactNameValid = false;
var IsContactPhoneValid = false;
var IsContactMailValid = false;
Contact.Name = "";
Contact.Sender = null;

Contact.SubmitForm = function ()
{
	var name = document.getElementById("contactMeName");
	var phoneC = document.getElementById("contactMePhoneC");
	var phoneR = document.getElementById("contactMePhoneR");
	var phone = document.getElementById("contactMePhone");
	var mail = document.getElementById("contactMeMail");
	var button = document.getElementById("contactMeSubmit");
	var labels = document.getElementById("contactMeDialog").getElementsByTagName("label");
	var status = labels[labels.length - 1];
	var data = new NameValueCollection();

	status.innerHTML = "Skickar förfrågan...";
	button.disabled = true;

	data.Add(new NameValueItem("n", name.value));
	data.Add(new NameValueItem("p", "00" + phoneC.value + " " + phoneR.value + "-" + phone.value));
	if (mail.value.length > 0)
		data.Add(new NameValueItem("m", mail.value));
	if (Contact.Sender != null)
		Contact.Sender.Send(data);

	return IsContantFormValid;
}

Contact.FormSubmitted = function (status)
{
	if (status == ConnectionStatus.Ready)
	{
		var button = document.getElementById("contactMeSubmit");
		var labels = document.getElementById("contactMeDialog").getElementsByTagName("label");
		var status = labels[labels.length - 1];
		if (parseInt(Contact.Sender.Response) == 0)
			status.innerHTML = "Förfrågan Skickad";
		else if (parseInt(Contact.Sender.Response) == 2)
			status.innerHTML = "Felaktig Förfrågan";
		else
			status.innerHTML = "Sändningsfel";
		button.disabled = false;
	}
}

Contact.FormFailed = function (reason)
{
	var button = document.getElementById("contactMeSubmit");
	var labels = document.getElementById("contactMeDialog").getElementsByTagName("label");
	var status = labels[labels.length - 1];
	status.innerHTML = "Anslutningsfel";
	button.disabled = false;
}

Contact.LoadLastInput = function ()
{
	var name = System.Load("ContactMeName");
	var phoneC = System.Load("ContactMePhoneC");
	var phoneR = System.Load("ContactMePhoneR");
	var phone = System.Load("ContactMePhone");
	var mail = System.Load("ContactMeMail");

	if (name != null)
	{
		document.getElementById("contactMeName").value = name;
		Contact.ValidateName();
	}
	if (phoneC != null)
	{
		document.getElementById("contactMePhoneC").value = phoneC;
		Contact.ValidatePhone();
	}
	if (phoneR != null)
	{
		document.getElementById("contactMePhoneR").value = phoneR;
		Contact.ValidatePhone();
	}
	if (phone != null)
	{
		document.getElementById("contactMePhone").value = phone;
		Contact.ValidatePhone();
	}
	if (mail != null)
	{
		document.getElementById("contactMeMail").value = mail;
		Contact.ValidateMail();
	}
}

Contact.ValidateForm = function ()
{
	var name = document.getElementById("contactMeName");
	var phoneC = document.getElementById("contactMePhoneC");
	var phoneR = document.getElementById("contactMePhoneR");
	var phone = document.getElementById("contactMePhone");
	var mail = document.getElementById("contactMeMail");
	var button = document.getElementById("contactMeSubmit");

	var hasErrors = false;

	if (name.value.length < 2 || phoneC.value.length < 2 || phoneR.value.length < 1 || phone.value.length < 4)
	{
		button.disabled = true;
		hasErrors = true;
	}
	else
		button.disabled = false;
}

Contact.ValidateName = function (name)
{
	var name = document.getElementById("contactMeName");
	var value = name.value;

	/*if (event)
	{
	if (event.keyCode == 8 || event.keyCode == 189 || event.keyCode == 192 || event.keyCode == 221 || event.keyCode == 222 || event.keyCode == 32 || (event.keyCode >= 65 && event.keyCode <= 90))
	{
	Contact.Name = value;
	}
	else
	{
	if (name.value != Contact.Name && (event.keyCode >= 37 && event.keyCode <= 40))
	name.value = Contact.Name;

	this.ValidateForm();
	return;
	}
	}*/

	while (value.charAt(0) == " ")
		value = value.substring(1);
	while (value.length > 1 && (value.substring(value.length - 2) == "  " || value.substring(value.length - 2) == "--" || value.substring(value.length - 2) == ".."))
		value = value.substring(0, value.length - 1);

	for (var i = 0; i < value.length; i++)
	{
		if (i == 0)
		{
			if (String.IsChar(value.charAt(i)))
			{
				value = String.ToUpper(value, i);
			}
			else
				value = value.substring(1);
		}
		else
		{
			if (String.IsChar(value.charAt(i)))
			{
				if (String.IsChar(value.charAt(i - 1)))
					value = String.ToLower(value, i);
				else
					value = String.ToUpper(value, i);
			}
			//else if (value.charAt(i) == "." && value.charAt(i + 1) != " ")
			//	value = value.substring(0, i + 1) + " " + value.substring(i + 1);
			else if (value.charAt(i) != " " && value.charAt(i) != "-" && value.charAt(i) != ".")
				value = value.substring(0, i) + value.substring(i + 1);
		}
		//		if (value.charAt(i - 1).toLowerCase().charCodeAt(0) != value.charAt(i - 1).toUpperCase().charCodeAt(0))
		//			value = value.substring(0, i) + value.substring(i, 1).toUpperCase() + value.substring(i + 1);
	}

	name.value = value;
	System.Save("ContactMeName", value);
	this.ValidateForm();
}

Contact.ValidatePhone = function (number)
{
	var phoneC = document.getElementById("contactMePhoneC");
	var phoneR = document.getElementById("contactMePhoneR");
	var phone = document.getElementById("contactMePhone");
	var valueC = phoneC.value;
	var valueR = phoneR.value;
	var value = phone.value;

	for (var i = 0; i < value.length; i++)
	{
		if (i == 0)
		{
			if (value.charAt(i) != "+" && !String.IsNumber(value.charAt(i)))
			{
				value = value.substring(1);
			}
		}
		else
		{
			if (!String.IsNumber(value.charAt(i)))
			{
				value = value.substring(0, i) + value.substring(i + 1);
			}
		}
	}

	phoneC.value = valueC;
	phoneR.value = valueR;
	phone.value = value;
	System.Save("ContactMePhoneC", valueC);
	System.Save("ContactMePhoneR", valueR);
	System.Save("ContactMePhone", value);
	this.ValidateForm();
}

Contact.ValidateMail = function ()
{
	var mail = document.getElementById("contactMeMail");
	var value = mail.value;
	var hasAtSign = false;
	var hasDomain = false;
	var hasTopDomain = false;
	//mail.Valid = false;

	for (var i = 0; i < value.length; i++)
	{
		if (i == 0)
		{
			if (value.charAt(i) == "@")
				value = value.substring(1);
		}
		if (value.charAt(i) == " ")
		{
			value = value.substring(0, i) + value.substring(i + 1);
		}
		if (i > 0 && value.charAt(i) == "@" && !hasAtSign)
			hasAtSign = true;
		else if (value.charAt(i) == "@" && hasAtSign)
			value = value.substring(0, i) + value.substring(i + 1);
		if (hasAtSign)
		{
			dot = value.lastIndexOf(".");
			if (dot > value.indexOf("@") + 1 && dot < value.length - 1)
			{
			}
		}
	}

	mail.value = value;
	System.Save("ContactMeMail", value);
	this.ValidateForm();
}