var is_unique;
var server_url = "/index.php/";
function validate(id)
{
	required = $("v_"+id).down("div.required");
	if(required == null)
	{
		required = false;
	}
	else
	{
		required = true;
	}
	max = $("v_"+id).down("div.maxLength");
	if(max == null)
	{
		max = -1;
	}
	else
	{
		max = max.innerHTML;
	}
	min = $("v_"+id).down("div.minLength");
	if(min == null)
	{
		min = -1;
	}
	else
	{
		min = min.innerHTML;
	}
	
	text_valid = isTextValid(id, required, max, min);
	
	// input password is changed and there may be other input that is confirmationt to it
	child = $("v_"+id).down('div.confirmable');
	if(child != null && (($(id+"ConfirmationValid").visible()== true) || ($(id+"ConfirmationInvalid").visible()== true)))
	{
		isEqual(child.innerHTML, id);
	}
	
	if(text_valid)
	{
		var validation = $("v_" + id);
		children = validation.childNodes;
		clen = children.length;
		for (j = 0; j < clen; j++)
		{
			child = children[j];
			class_name = child.className;
			if((class_name == "required") || (class_name == "maxLength") || (class_name == "minLength") || (class_name == "password"))
				continue;
			
			if(class_name == "mail")
			{
				if(isMail(id) == false)
				break;
			}
			
			if(class_name == "regularExp")
			{
				if(isRegularExp(id, child) == false)
				break;
			}
			
			if(class_name == "confirmation")
			{
				if(isEqual(id, child.innerHTML) == false)
				break;
			}
			
			if(class_name == "unique")
			{
				isUniqueReqeust(child.innerHTML, id);
				if(is_unique == false)
					break;
			}
		}
	} 
	validate_all();
}
function isRegularExp(input, exp)
{
	var str = $(input).value;
	var valid = input+"Valid";
	var invalid = input+"Invalid";

	validRegExp = exp.innerHTML;

	if (str.search(validRegExp) != -1) 
	{
		$(invalid).hide();
		$(valid).show();
		toReturn = true;
	}
	else
	{
		var validateText = input + "ValidateText";
		message = exp.nextSiblings()[0].innerHTML;
		$(validateText).innerHTML = "Must be " + message;

		$(valid).hide();
		$(invalid).show();
		toReturn = false;
	}

	return toReturn;
}

function isMail(input)
{
	var str = $(input).value;
	var valid = input+"Valid";
	var invalid = input+"Invalid";

	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

	if (str.search(validRegExp) != -1) 
	{
		$(invalid).hide();
		$(valid).show();
		toReturn = true;
	}
	else
	{
		var validateText = input + "ValidateText";
		$(validateText).innerHTML = "Invalid mail address";

		$(valid).hide();
		$(invalid).show();
		toReturn = false;
	}

	return toReturn;
}


function isTextValid(input, required, maxLength, minLength)
{
	var str = $(input).value;
	var valid = input+"Valid";
	var invalid = input+"Invalid";
	var validateText = input + "ValidateText";

	if(required == true && str=="")
	{
		$(validateText).innerHTML = input+" is required";

		$(valid).hide();
		$(invalid).show();

		toReturn = false;
	}
	else if(minLength > 0 && str.length < minLength)
	{
		$(validateText).innerHTML = "Min length is " + minLength + " characters";

		$(valid).hide();
		$(invalid).show();

		toReturn = false;
	}
	else if(maxLength > 0 && str.length > maxLength)
	{
		$(validateText).innerHTML = "Max length is " + maxLength + " characters";

		$(valid).hide();
		$(invalid).show();

		toReturn = false;
	}
	else 
	{
		$(invalid).hide();
		$(valid).show();

		toReturn =  true;
	}
	
	return toReturn;
}

function isEqual(input, comparee)
{
	var str1 = $(input).value;
	var str2 = $(comparee).value;
	var valid = input+"Valid";
	var invalid = input+"Invalid";
	var validateText = input + "ValidateText";

	if (str1 == str2)
	{
		$(invalid).hide();
		$(valid).show();	
		toReturn = true;	
	}
	else
	{
		$(validateText).innerHTML = "Both " + comparee + "s are not the same";

		$(valid).hide();
		$(invalid).show();	
		toReturn = false;	
	}
	return toReturn;
}

function isUnique(input, unique)
{
	var valid = input+"Valid";
	var invalid = input+"Invalid";
	var validateText = input + "ValidateText";

	if(unique=="unique")
	{
		$(invalid).hide();
		$(valid).show();
		toReturn  = true;
	}
	else
	{
		$(validateText).innerHTML = input + " exists!";

		$(valid).hide();
		$(invalid).show();
		toReturn  = false;
	}
	
	is_unique = toReturn;
	return toReturn;
}

function isUniqueReqeust(url, name)
{
	//url = url + "?" + name + "=" + $(name).value;
	new Ajax.Request(url,{method:'post',
		onSuccess: function(transport){
			isUnique(name, transport.responseText);},
			parameters: {param: $(name).value},
			asynchronous: false
			}); 
}

function validate_all()
{
	submit = $('btn_submit');
	valid_divs = $$('.valid');
	len = valid_divs.length;
	for(i = 0; i < len; i++)
	{
		input  = valid_divs[i].up(1).down();
		if((valid_divs[i].visible() == false) || 
			(input.hasClassName('required') && input.value==""))
		{
			submit.disabled = true;
			return;
		}
	}
	
	submit.disabled = false;
}

function onEnterSubmit(evt){
	submit = $('btn_submit');
	if(submit.disabled == true){
		return true;
	}
	
	var keyCode = null;

	if( evt.which ) {
		keyCode = evt.which;
	} else if( evt.keyCode ) {
		keyCode = evt.keyCode;
	}
	
	if(keyCode==13){
		submit.click();
		return false;
	}
	return true;
}

/***************************************************************************/
/***************************************************************************/
/*****************************Sign Up**************************************/
/***************************************************************************/
/***************************************************************************/
var xmlHttp;
function getXmlHttp()
{
	
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  return xmlHttp;
}
function signupWindow() 
{
	//document.getElementById('overlay').style.display = "block";
	//document.getElementById('signup').style.display = "block";
	inputs = Form.getInputs('signupForm', 'text');
	for(f=0; f < inputs.length; f++)
	{
		inputs[f].value = "";
	}
	passwords = Form.getInputs('signupForm', 'password');
	for(f=0; f < passwords.length; f++)
	{
		passwords[f].value = "";
	}
	validate_all();
}
function closeSignUp()
{
	document.getElementById('overlay').style.display = "none";
	document.getElementById('signup').style.display = "none";
}
function signupStateChanged()
{
	if (xmlHttp.readyState==4)
	{ 
		var response = xmlHttp.responseText;
		if(response == "success")
		{
//			document.getElementById("voicecloudusername").value = document.getElementById("username").value;
//			document.getElementById("voicecloudpassword").value = document.getElementById("password").value;
//			login();
			//closeSignUp();
			//window.location = server_url + "transactions/main";
			window.location = "http://www.voicecloud.com/index.php/customers/setup"; //server_url; // + "transactions/main";
			
			
		}
		else
		{
			alert(response);
		}
	}
}
function signUp()
{
	xmlHttp=getXmlHttp();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	promocode = document.getElementById("promocode").value;
	username = document.getElementById("username").value;
	password = document.getElementById("password").value;
	firstname = document.getElementById("firstname").value;
	lastname = document.getElementById("lastname").value;
	email = document.getElementById("email").value;
	mobile = document.getElementById("mobile").value;
	timezone = document.getElementById("timezone").value;
	pinnum = document.getElementById("pinnum").value;
	
	var url= server_url+"customers/signup";
	
	var params = "promocode="+promocode+"&username="+username+"&password="+password+"&firstname="+firstname+"&lastname="+lastname+"&email="+email+"&mobile="+mobile+"&timezone="+timezone+"&pinnum="+pinnum;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange=signupStateChanged;
	xmlHttp.send(params);	
}