// form validation function //
function validate(form) {
  var name = form.name.value;
 // var fname = form.fname.value;
 // var lname = form.lname.value;
  var email = form.email.value;
  var country = form.country.value;
  var speciality = form.speciality.value;
  var weburl = form.weburl.value;
  var rate = form.rate.value;
  //var myfile1 = form.myfile1.value.lastIndexOf('.jpg');
  //var myfile2 = form.myfile2.value.lastIndexOf('.jpg');
  var unameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var rateRegex = /^\d+$/;
  //var urlRegex = /[^ ]+([A-Za-z]{2,3})+/;
  /*var myFSO = new ActiveXObject("Scripting.FileSystemObject");
  var filepath_1 = myfile1;
  var file_1 = myFSO.getFile(filepath_1);
  var size_1 = file_1.size;
  alert(file_1);
  var filepath_2 = myfile2;
  var file_2 = myFSO.getFile(filepath_2);
  var size_2 = file_2.size;
  var full_size = size_1 + size_2;
  */
 if(name == "") {
    inlineMsg('name','You must enter name.',2);
    return false;
  } 
  
  /*
  if(!uname.match(unameRegex)) {
    inlineMsg('uname','You have entered an invalid username.',2);
    return false;
  }
  
  if(fname == "") {
    inlineMsg('fname','You must enter your first name.',2);
    return false;
  } 
  if(lname == "") {
    inlineMsg('lname','You must enter your last name.',2);
    return false;
  } 
  */
  
  if(email == "") {
    inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
  if(country == "") {
    inlineMsg('country','<strong>Error</strong><br />You must choose your country.',2);
    return false;
  }
  /*if(speciality == "") {
    inlineMsg('speciality','<strong>Error</strong><br />You must choose your speciality field.',2);
    return false;
  }
 	
  if(weburl == "") {
    inlineMsg('weburl','You must enter your website url.',2);
    return false;
  }*/ 
  if(rate == "") {
    inlineMsg('rate','<strong>Error</strong><br />You must choose rate(USD) per hour.',2);
    return false;
  }
  /* if(!weburl.match(urlRegex)) {
    inlineMsg('weburl','<strong>Error</strong><br />You have entered an invalid web url.<br /> examples: www.domain.com, domain.',2);
    return false;
  }
	function isInteger(s)
	{
		var i;
		s = s.toString();
		for (i = 0; i < s.length; i++)
		{
			var c = s.charAt(i);
			if (isNaN(c)) 
			{
				inlineMsg('rate','You are allowed to enter only numbers.',2);
   				return false;
			}
		}
		
		return true;
	}

  if(rate == "") {
	  isInteger(rate);   
  }
  
  if(!rate.match(rateRegex)) {
    inlineMsg('rate','<strong>Error</strong><br />Only numbers allowed.',2);
	
    return false;
  }
  
	if(rate >= 80 && rate <= 3){
		inlineMsg('rate','You have entered invalid rate.<br/> Value should be in between 3 and 80.',2);
    	return false;
	}
  */
  
 /* if(myfile1 == -1) {
    inlineMsg('myfile1','Please upload .jpg file only.',2);
    return false;
  }
  
   if(myfile2 == -1) {
    inlineMsg('myfile2','Please upload .jpg file only.',2);
    return false;
  }*/
  
  /*if(full_size > 25600)
  {
	 inlineMsg('myfile2','<strong>Error</strong><br />You can not upload more than 200 KB files.',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 * 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"; 
}


// JavaScript Document
function SendMail(name, company, domain) {
    link = 'mai' + 'lto:' + name + '@' + company + '.' + domain;
    window.location.replace(link);
}
