// form validation function //
function funCheck(e)
{

	if(navigator.appName == 'Microsoft Internet Explorer')
	{
		if(e.keyCode>=48 && e.keyCode<=57)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		if(e.which>=48 && e.which<=57)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

}
function funCancel()
{
	if(confirm('do u want to cancel?'))
	{
		window.location = 'index.php';
	}

}


function validate(form) 
{
  var name = form.txtUserid.value;
  var password=form.password.value;
  var cPassword=form.cPassword.value;
  var firstname=form.txtfirstname.value;
  var lastname =form.txtlastname.value;
  var address1= form.txtaddress1.value;
  var city= form.txtcity.value;
  var pin=  form.txtZip.value;
  var country=form.selectcountry.value;
  var emailid=form.txtemailid.value;
  var altemail=form.txtaemail.value;
  var cemail=form.txtContactemail.value;
  var theurl=form.txtweburl.value;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var tomatch= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
  var chk=form.check.checked;
  
  if(name == "") 
  {
    inlineMsg('txtUserid','You must enter your name.',2);
     //name.style.color='#FF0000';
    return false;
  }
  if(password == "")
  {
	  inlineMsg('password','You must enter the password.',2);
	  return false;
  }
  if(cPassword == "")
  {
	  inlineMsg('cPassword','You must Retype the password',2);
	  return false;
  }
  if(password != cPassword)
  {
      inlineMsg('cPassword','<strong>ERROR !!</strong><br/>Two passwords must be same',2);
      return false;	  
  }
  if(firstname == "")
  {
      inlineMsg('txtfirstname','You must enter Your Firstname');
      return false;	  
  }
  if(lastname == "")
  {
      inlineMsg('txtlastname','You must enter Your Lastname');
      return false;	  
  }
  if(address1 == "")
  {
      inlineMsg('txtaddress1','You must enter Your Address',2);
      return false;	  
  }
  if(city == "")
  {
      inlineMsg('txtcity','You must enter your City',2);	
      return false;  
  }
  if(pin == "")
  {
      inlineMsg('txtZip','You must enter your Postal Code',2);	  
      return false;
  }
  if(country == "")
  {
      inlineMsg('selectcountry','You must select Your Country',2);
      return false;	  
  }
  if(emailid == "")
  {
      inlineMsg('txtemailid','You must enter your Emailid',2);
      return false;	  
  }
  if(!emailid.match(emailRegex)) 
  {
    inlineMsg('txtemailid','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
  if(altemail!="" && !altemail.match(emailRegex))
  {
	  inlineMsg('txtaemail','<strong>Error !!</strong><br/>You have entered an invalid email.',2);
	  return false;
  }
  if(cemail!="" && !cemail.match(emailRegex))
  {
	  inlineMsg('txtContactemail','<strong>Error !!</strong><br/>You have entered an invalid email.');
	  return false;
  }
  if(theurl!="" && !theurl.match(tomatch))
  {
	 inlineMsg('txtweburl','<strong>Error !!</strong><br/>You must Enter a Invalid WEBURL.',2);
     return false; 
  }
  if(chk==false)
  {
	  inlineMsg('check','Check the Terms and Conditions',2);
	  return false;
  }
  
  return true;
}



function validateedit(form) 
{
  var name = form.txtuser_name.value;
  var password=form.password.value;
  var cPassword=form.cpassword.value;
  var firstname=form.txtfirstname.value;
  var lastname =form.txtlastname.value;
  var address1= form.txtaddress1.value;
  var city= form.txtcity.value;
  var pin=  form.txtpin.value;
  var country=form.txtcountry.value;
  var emailid=form.txtpemail.value;
  var altemail=form.txtaemail.value;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/; 
  var chk=form.check.checked;
  
  if(name == "") 
  {
    inlineMsg('txtuser_name','You must enter your name.',2);
    //document.getElementById('txtUserid').style.color='#FF0000';
    return false;
  }
  if(password == "")
  {
	  inlineMsg('password','You must enter the password.',2);
	  return false;
  }
  if(cPassword == "")
  {
	  inlineMsg('cpassword','You must Retype the password',2);
	  return false;
  }
  if(password != cPassword)
  {
      inlineMsg('cPassword','<strong>ERROR !!</strong><br/>Two passwords must be same',2);
      return false;	  
  }
  if(firstname == "")
  {
      inlineMsg('txtfirstname','You must enter Your Firstname');
      return false;	  
  }
  if(lastname == "")
  {
      inlineMsg('txtlastname','You must enter Your Lastname');
      return false;	  
  }
  if(address1 == "")
  {
      inlineMsg('txtaddress1','You must enter Your Address',2);
      return false;	  
  }
  if(city == "")
  {
      inlineMsg('txtcity','You must enter your City',2);	
      return false;  
  }
  if(pin == "")
  {
      inlineMsg('txtpin','You must enter your Postal Code',2);	  
      return false;
  }
  if(country == "")
  {
      inlineMsg('txtcountry','You must select Your Country',2);
      return false;	  
  }
  if(emailid == "")
  {
      inlineMsg('txtpemail','You must enter your Emailid',2);
      return false;	  
  }
  if(!emailid.match(emailRegex))
  {
    inlineMsg('txtpemail','<strong>Error !!</strong><br />You have entered an invalid email.',2);
    return false;
  }
  if(altemail != "" && !altemail.match(emailRegex))
  {
	 inlineMsg('txtaemail','<strong>Error !!</strong><br/>You have entered an invalid email.',2);
	 return false;  
  }
  if(chk==false)
  {
	  inlineMsg('check','Check the Terms and Conditions',2);
	  return false;
  }
  
   return true;
}


function validatecontact(form) 
{
  var name1 = form.txtFirstName.value;
  var emailid=form.txtEmail.value;
  var Title = form.txtTitle.value;
  var JobTitle = form.txtJobTitle.value;
  var CompanyName = form.txtCompanyName.value;
  var Address = form.txtAddress.value;
  var Country = form.selCountry.value;
  var Subject = form.txtSubject.value;
  var Message = form.txtMsg.value;

  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/; 
  if(name1 == "") 
  {
    inlineMsg('txtFirstName','You must enter your First name.',2);
    //document.getElementById('txtUserid').style.color='#FF0000';
    return false;
  }
  if(emailid == "")
  {
      inlineMsg('txtEmail','You must enter your Emailid',2);
      return false;	  
  }
  if(!emailid.match(emailRegex))
  {
    inlineMsg('txtEmail','<strong>Error !!</strong><br />You have entered an invalid email.',2);
    return false;
  }
     if(JobTitle == "")
  {
      inlineMsg('txtJobTitle','You must enter your Job Title',2);
      return false;	  
  }
  if(CompanyName == "")
  {
      inlineMsg('txtCompanyName','You must enter your Organisation Name',2);
      return false;	  
  }
   if(Address == "")
  {
      inlineMsg('txtAddress','You must enter your Address',2);
      return false;	  
  }
  if(Country == "")
  {
      inlineMsg('selCountry','You must enter your Country',2);
      return false;	  
  }
   if(Title == "")
  {
      inlineMsg('txtTitle','You must enter your Document Title',2);
      return false;	  
  }
  if(Subject == "")
  {
      inlineMsg('txtSubject','You must enter your Subjects',2);
      return false;	  
  }
  if(Message == "")
  {
      inlineMsg('txtMsg','You must enter your Document Content',2);
      return false;	  
  }
  
  
 
  return true;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 30;
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"; 
}