//******************join us form validation*********************//
function validate_joinform(joinform) 
{
  var newid = joinform.newid.value;
  var newpass = joinform.newpass.value;
  var confpass = joinform.confpass.value;
  var name = joinform.name.value;
  var email = joinform.email.value;  
  var message = joinform.message.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);

  if(newid == "") {
    inlineMsg('newid','You must enter a Login  name.',5);
    return false;
  }
  if(!newid.match(nameRegex)) {
    inlineMsg('newid','You have entered an invalid Login name.',5);
    return false;
  }
  if(newpass == "") {
    inlineMsg('newpass','You must enter Password.',5);
    return false;
  }
  if(confpass == "") {
    inlineMsg('confpass','You must enter Confirm Password.',5);
    return false;
  }
  if(newpass != confpass) {
    inlineMsg('confpass','Your must enter same Password.',5);
    return false;
  } 
  if(name == "") {
    inlineMsg('name','You must enter your name.',5);
    return false;
  }
  if(!name.match(nameRegex)) {
    inlineMsg('name','You have entered an invalid name.',5);
    return false;
  }
  if(email == "") {
    inlineMsg('email','You must enter your email.',5);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','You have entered an invalid email.',5);
    return false;
  }  
 
  return true;
}

//******************contact us form validation*********************//
function validate_contactform(contactform) 
{
  var name = contactform.name.value;
  var email = contactform.email.value;  
  var message = contactform.message.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);

if(name == "") {
    inlineMsg('name','You must enter a your name.',5);
    return false;
  }
  if(email == "") {
    inlineMsg('email','You must enter a your name.',5);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','You have entered an invalid email.',5);
    return false;
  }  
  if(message == "") {
    inlineMsg('message','You must enter a your message.',5);
    return false;
  }
  return true;
}


//******************ask to editor input validation*********************//
function validate_editorform(editorform) 
{
  var qtitle = editorform.qtitle.value; 
  var question = editorform.question.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/; 
  
if(qtitle == "") {
    inlineMsg('qtitle','You must enter a Question title.',5);
    return false;
  }
  if(editorform.tag.selectedIndex == 0) {
    inlineMsg('tag','You must Tag your question.',5);
    return false;
  }
  if(question == "") {
    inlineMsg('question','You must enter a your Question.',5);
    return false;
  }
  return true;
}

//******************post new blog input validation*********************//
function validate_blogform(blogform) 
{
  var btitle = blogform.btitle.value; 
  var blog = blogform.blog.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/; 
  
if(btitle == "") {
    inlineMsg('btitle','You must enter a Blog title.',5);
    return false;
  }
  if(blogform.tag.selectedIndex == 0) {
    inlineMsg('tag','You must Tag your blog.',5);
    return false;
  }
  if(blog == "") {
    inlineMsg('blog','You must enter a your Blog.',5);
    return false;
  }
  return true;
}

//******************login form validation*********************//
function validate_loginform(loginform) 
{
	var uid = loginform.uid.value;
	var pass = loginform.pass.value;
	if(uid == "") 
	  {		
		//alert("You must enter User ID.");		
		loginform.uid.style.borderColor='red';
		loginform.uid.focus();
		return false;
	  }
  if(pass == 0) 
	  {
		//alert("You must enter Password.");
		loginform.pass.style.borderColor='red';
		loginform.pass.focus();
		return false;
	  }

	return true;
}

//******************search bar validation*********************//
function validate_tagform(tagform) 
{
	var tag = tagform.tag.value;
	if(tag == "") 
	  {		
		//alert("You must enter Tag string.");		
		tagform.tag.style.borderColor='red';
		tagform.tag.focus();
		return false;
	  }
	return true;
}

//******************answer form*********************//
function validate_ansform(ansform) 
{
	var name = ansform.name.value;
	var ans = ansform.ans.value;
	if(name == "") 
	  {		
		inlineMsg('name','You must enter a your Name.',5);		
		ansform.name.style.borderColor='red';
		ansform.name.focus();
		return false;
	  }
	if(ans == "") 
	  {		
		inlineMsg('ans','You must enter a your Answer.',5);		
		ansform.ans.style.borderColor='red';
		ansform.ans.focus();
		return false;
	  }
	return true;
}

//******************blog comment validation*********************//
function validate_blogform(blogform) 
{
	var name = blogform.name.value;
	var post = blogform.post.value;
	if(name == "") 
	  {		
		inlineMsg('name','You must enter a your Name.',5);		
		blogform.name.style.borderColor='red';
		blogform.name.focus();
		return false;
	  }
	if(post == "") 
	  {		
		inlineMsg('post','You must enter a your Comment.',5);		
		blogform.post.style.borderColor='red';
		blogform.post.focus();
		return false;
	  }
	return true;
}

//******************article comment validation*********************//
function validate_artform(artform) 
{
	var name = artform.name.value;
	var post = artform.post.value;
	if(name == "") 
	  {		
		inlineMsg('name','You must enter a your Name.',5);		
		artform.name.style.borderColor='red';
		artform.name.focus();
		return false;
	  }
	if(post == "") 
	  {		
		inlineMsg('post','You must enter a your Comment.',5);		
		artform.post.style.borderColor='red';
		artform.post.focus();
		return false;
	  }
	return true;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 10;
var MSGSPEED = 4;
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.png"; 
}
