// form validation function //

var openInSameWindow = 0;

function validateforgotpassword(form)
{
	var email = form.email.value;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	
	if (email == "")
	{
		inlineMsg('email','You must enter your email.',2);
		return false;
	}
	if (!email.match(emailRegex))
	{
		inlineMsg('email','You have entered an invalid email.',2);
		return false;
	}
	setCookie("em",email,6,"/",location.hostname.substring(location.hostname.indexOf('.')),"");
	document.getElementById("submitbutton").innerHTML = 'Please wait ... <img src="/images/site/ajaxloader.gif" />';
	return true;
}

function validateinfo(form)
{
	var name = form.name.value;
	var email = form.email.value;
	var phone = form.phone.value;
	var question = form.question.value;
	var nameRegex = /^[a-zA-Z\s]+(([\'\,\.\-\s ][a-zA-Z\s ])?[a-zA-Z\s]*)*$/;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var phoneRegex = /^[0-9_\s\-\.]+$/;
	
	if (name == "")
	{
		inlineMsg('name','You must enter your name.',2);
		return false;
	}
	if (!name.match(nameRegex))
	{
		inlineMsg('name','You have entered an invalid name.',2);
		return false;
	}
	if (email == "")
	{
		inlineMsg('email','You must enter your email.',2);
		return false;
	}
	if (!email.match(emailRegex))
	{
		inlineMsg('email','You have entered an invalid email.',2);
		return false;
	}
	if (phone == "")
	{
		inlineMsg('phone','Please enter your phone number',2);
		return false;
	}
	if (!phone.match(phoneRegex))
	{
		inlineMsg('phone','You have entered an invalid phone number.',2);
		return false;
	}
	if (question == "")
	{
		inlineMsg('question','Please enter your message',2);
		return false;
	}
	document.getElementById("infosubmitbutton").innerHTML = 'Please wait ... <img src="/images/site/ajaxloader.gif" />';
	return true;
}
function validatecontact(form)
{
	var name = form.name.value;
	var email = form.email.value;
	var subject = form.subject.value;
	var message = form.message.value;
	var nameRegex = /^[a-zA-Z\s]+(([\'\,\.\-\s ][a-zA-Z\s ])?[a-zA-Z\s]*)*$/;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	
	if (name == "")
	{
		inlineMsg('name','You must enter your name.',2);
		return false;
	}
	if (!name.match(nameRegex))
	{
		inlineMsg('name','You have entered an invalid name.',2);
		return false;
	}
	if (email == "")
	{
		inlineMsg('email','You must enter your email.',2);
		return false;
	}
	if (!email.match(emailRegex))
	{
		inlineMsg('email','You have entered an invalid email.',2);
		return false;
	}
	if (subject == "")
	{
		inlineMsg('subject','Please enter a subject.',2);
		return false;
	}
	if (message == "")
	{
		inlineMsg('message','Please enter a message.',2);
		return false;
	}
	document.getElementById("infosubmitbutton").innerHTML = 'Please wait ... <img src="/images/site/ajaxloader.gif" />';
	return true;
}

function validatedemo(form)
{
	if (!validate1(form))
	{
		return false;
	}

	// if(openInSameWindow != 1) 
	// {
		// var isIE = (navigator.appVersion.indexOf("MSIE")==-1)? false : true;

		// var scrollbars = (isIE ? "no" : "yes");
		// try {
			// wnd = window.open("", "demowindow", "height=600, width=800, toolbar=no, menubar=no, scrollbars=" + scrollbars + ", location=no, directories=no, status=yes, resizable=1");
		// } catch (e) { }
		// openWindowTimer = setTimeout("checkOpenWindow();", 10000);
	// }
	
	document.getElementById("demosubmitbutton").innerHTML = 'Please wait ... <img src="/images/site/ajaxloader.gif" />';
	return true;
}

function validateconverttrialPro(form)
{
	var nbseats = form.nbseats.value;
	if (nbseats < 5)
	{
		inlineMsg('nbseats','You must have 5 or more seats.',2);
		return false;
	}
	else if (!validateconverttrial(form))
	{
		return false;
	}
	return true;
}

function validateconverttrial(form)
{
	var companyName = form.companyName.value;
	var shortUrl = form.shortUrl.value;
	var supportEmail = form.supportEmail.value;
	var buff_capacity = form.buff_capacity.value;
	var shortUrlRegex = /^[a-zA-Z0-9]+(([a-zA-Z0-9])?[a-zA-Z0-9]*)*$/;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	if (companyName == "")
	{
		inlineMsg('companyName','Please enter your company name.',2);
		return false;
	}
	if (shortUrl == "")
	{
		inlineMsg('shortUrl','Please select a shortUrl.',2);
		return false;
	}
	if (shortUrl.length < 3)
	{
		inlineMsg('shortUrl','3 letter minimum.',2);
		return false;
	}
	if (!shortUrl.match(shortUrlRegex))
	{
		inlineMsg('shortUrl','Letters and numbers only. No spaces.',2);
		return false;
	}
	if (supportEmail == "")
	{
		inlineMsg('supportEmail','Please enter a support email contact.',2);
		return false;
	}
	if (!supportEmail.match(emailRegex))
	{
		inlineMsg('supportEmail','You have entered an invalid email.',2);
		return false;
	}
	if (buff_capacity == "")
	{
		inlineMsg('buff_capacity','Please enter a buffer capacity.',2);
		return false;
	}
	document.getElementById("convertButtonDiv").innerHTML = 'Please wait ... <img src="/images/site/ajaxloader.gif" />';
	return true;
}

function validatereseller(form)
{
	var email = form.email.value;
	var password = form.password.value;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	if (email == "")
	{
		inlineMsg('email','You must enter your email.',2);
		return false;
	}
	if (!email.match(emailRegex))
	{
		inlineMsg('email','You have entered an invalid email.',2);
		return false;
	}
	if (password == "")
	{
		inlineMsg('password','You must enter your password.',2);
		return false;
	}
	return true;
}

function checkOpenWindow() {
	
	try {
		var ln = wnd.name;
		if(ln == undefined || wnd.name == undefined)
			throw "Undefined Window";
	} catch(e) {
		openInSameWindow = 1;
		document.demoform.target = "_self";
		
		document.demoform.submit();
	}
}

function changeLocation() 
{
	clearTimeout(openWindowTimer);
	window.location = '/trial-after-demo';
}

function validate(form)
{
	if (!validate1(form))
	{
		return false;
	}
	document.getElementById("trialsubmitbutton").innerHTML = 'Please wait ... <img src="/images/site/ajaxloader.gif" />';
	return true;
}

function demoredir()
{
	var firstname = document.getElementById("fn").value;
	var lastname = document.getElementById("ln").value;
	var email = document.getElementById("email").value;
	if ((firstname != "") && (lastname != "") && (email != ""))
	{
		setCookie("fn",firstname,6,"/",location.hostname.substring(location.hostname.indexOf('.')),"");
		setCookie("ln",lastname,6,"/",location.hostname.substring(location.hostname.indexOf('.')),"");
		setCookie("email",email,6,"/",location.hostname.substring(location.hostname.indexOf('.')),"");
		window.location='/trial-after-demo';
		return true;
	}
	else
		return false;
}
function validate1(form)
{
	var firstname = form.fn.value;
	var lastname = form.ln.value;
	var email = form.email.value;
	var nameRegex = /^[a-zA-Z\s]+(([\'\,\.\-\s ][a-zA-Z\s ])?[a-zA-Z\s]*)*$/;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
	if (firstname == "")
	{
		inlineMsg('fn','You must enter your name.',2);
		return false;
	}
	if (!firstname.match(nameRegex))
	{
		inlineMsg('fn','You have entered an invalid name.',2);
		return false;
	}
	if (lastname == "")
	{
		inlineMsg('ln','You must enter your name.',2);
		return false;
	}
	if (!lastname.match(nameRegex))
	{
		inlineMsg('ln','You have entered an invalid name.',2);
		return false;
	}
	if (email == "")
	{
		inlineMsg('email','You must enter your email.',2);
		return false;
	}
	if (!email.match(emailRegex))
	{
		inlineMsg('email','You have entered an invalid email.',2);
		return false;
	}
	setCookie("em",email,6,"/",location.hostname.substring(location.hostname.indexOf('.')),"");
	return true;
}

function validate2(form)
{
	var email = form.email.value;
	var organization = form.orgname.value;
	var companyRegex = /^[a-zA-Z0-9\s]+(([\'\,\.\-\s ][a-zA-Z\s ])?[a-zA-Z\s]*)*$/;
	if (!validate1(form))
	{
		return false;
	}
	else
	{
		if (organization == "")
		{
			inlineMsg('orgname','Please enter your organization name.',2);
			return false;
		}
		if (!organization.match(companyRegex))
		{
			inlineMsg('orgname','You have entered an invalid name.',2);
			return false;
		}
	}
	document.getElementById("trialsubmitbutton2").innerHTML = 'Please wait ... <img src="/images/site/ajaxloader.gif" />';
	setCookie("em",email,6,"/",location.hostname.substring(location.hostname.indexOf('.')),"");
	return true;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}

function setCookie(name, value, expires, path, domain, secure) {
	var date = new Date();
	date.setMonth(date.getMonth() + ((expires) ? expires : 6));

	var curCookie = name + "=" + escape(value) + "; expires=" + date.toGMTString() +
		((path) ? "; path=" + path : "; path=/") +
		((domain) ? "; domain=" + domain : "; domain="+location.hostname.substring(location.hostname.indexOf('.')) ) +
		((secure) ? "; secure" : "");

	try {
		document.cookie = curCookie;
	} catch(e) {
		alert("Cookie Error");
	}
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);

	if(begin == -1) {
		begin = dc.indexOf(prefix);
		if(begin != 0)
			return null;
	} else
		begin += 2;

	var end = document.cookie.indexOf(";", begin);

	if(end == -1)
		end = dc.length;

	var theValue = unescape(dc.substring(begin + prefix.length, end));

	return (theValue == undefined || theValue == "" ? null : theValue);
}

//Use this function to validate Email is in proper format
function validateEmail(emailValue, fieldName)
{
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	
	if (emailValue == "") 
	{
		inlineMsg(fieldName,'You must enter your email.',2);
		return false;
	}
	
	if(!emailValue.match(emailRegex))
	{
		inlineMsg(fieldName,'You must enter a valid email address.',2);
		return false;
	}
	
	return true;
}


//Use this function to validate Phone Number Field.
function validatePhone(phoneValue, fieldName)
{
	var phoneRegex = /^[0-9_\s\-\.\(\)]+$/;
	
	if (phoneValue == "")
	{
		inlineMsg(fieldName,'You must enter your phone number.',2);
		return false;
	}
	
	if (!phoneValue.match(phoneRegex))
	{
		inlineMsg(fieldName,'You must enter a valid phone number.',2);
		return false;
	}
	return true;
}

function validateTextField(fieldValue, fieldName)
{
	if (fieldValue == "")
	{
		inlineMsg(fieldName,'This field cannot be left blank',2);
		return false;
	}
	
	return true;
	
}