//onload fucntion //
function ShowFocus(form){
	window.document.form.user.focus();
}

// form validation function //
function validate(form) {
  var user = form.user.value;
  var password1 = form.password1.value;
  var passwordverify = form.passwordverify.value;
  var firstname = form.firstname.value; 
  var address1 = form.address1.value;
  var city = form.city.value;
  var zip = form.zip.value;
  var phone = form.phone.value;
  var email = form.email.value;
  var emailverify = form.emailverify.value;
  
//Symbols checking  
  var userRegex = /^[0-9A-Za-z]{4,20}$/;
  var passwordRegex = /^[0-9A-Za-z]{6,20}$/;
//  var firstnameRegex = /^[A-Za-z]{4,20}$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
//  var zipCodePattern=/^[0-9A-Za-z]{6,20}$/;
 // var zipCodePattern = /^\d{5}$|^\d{5}-\d{4}$/;
  
//username checking
  if(user == "") {
    inlineMsg('user','<strong>Alert:</strong> Please enter your Username<br/>4 - 20 characters (A-Z, a-z, 0-9 only)',2);
    return false;
  }
  if(!user.match(userRegex)) {
    inlineMsg('user','<strong>Alert:</strong> Invalid Username<br/>4 - 20 characters (A-Z, a-z, 0-9 only)',2);
    return false;
  }
//password checking  
  if(password1 == "") {
    inlineMsg('password1','<strong>Alert:</strong> Please enter your password<br/>6 - 20 characters (A-Z, a-z, 0-9 only)',2);
    return false;
  }
  if(!password1.match(passwordRegex)) {
    inlineMsg('password1','<strong>Alert:</strong> Invalid Password<br/>6 - 20 characters (A-Z, a-z, 0-9 only)',2);
    return false;
  }
//confirm password checking  
  if(passwordverify == "") {
    inlineMsg('passwordverify','<strong>Alert:</strong> Please enter your Confirm Password',2);
    return false;
  }
  if(!passwordverify.match(password1)) {
    inlineMsg('passwordverify','<strong>Alert:</strong> Password &amp; Confirm Password did not match',2);
    return false;
  }
//firstname checking  
  if(firstname == "") {
    inlineMsg('firstname','<strong>Alert:</strong>Please enter your First Name',2);
    return false;
  }
//  if(!firstname.match(firstnameRegex)) {
//    inlineMsg('firstname','<strong>Alert:</strong> You have entered an invalid First Name',2);
//    return false;
//  }
//address checking  
  if(address1 == "") {
    inlineMsg('address1','<strong>Alert:</strong> Please enter your Address',2);
    return false;
  }
//city checking  
  if(city == "") {
    inlineMsg('city','<strong>Alert:</strong> Please enter your City',2);
    return false;
  }
//   if(!city.match(firstnameRegex)) {
//    inlineMsg('city','<strong>Alert:</strong> You have entered an invalid City',2);
//    return false;
//  }
  //Zipcode checking  
  if(zip == "") {
    inlineMsg('zip','<strong>Alert:</strong> Please enter your Zipcode',2);
    return false;
  }
//   if(!zip.match(zipCodePattern)) {
//    inlineMsg('zip','<strong>Alert:</strong> You have entered an invalid Zipcode',2);
//    return false;
//  }
//phone checking  
  if(phone == "") {
    inlineMsg('phone','<strong>Alert:</strong> Please enter your Phone Number',2);
    return false;
  } 
//email checking  
  if(email == "") {
    inlineMsg('email','Please enter your Email',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Alert:</strong>You have entered an invalid Email',2);
    return false;
  }
//confirm email checking  
  if(emailverify == "") {
    inlineMsg('emailverify','Please enter your Confirm Email',2);
    return false;
  }
  if(!emailverify.match(email)) {
    inlineMsg('emailverify','<strong>Alert:</strong> Your Email and Confirm Email did not match',2);
    return false;
  } 
  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 * 3000));
}

// 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 = 8;
  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.image) {
  arrow = new Image(7,80); 
  arrow.src = "../image/msg_arrow.gif"; 
}





