// form validation function //
function validate(form) {
  var name = form.name.value;
  var age = form.age.value;
  var sal = form.sal.value;
  var doj = form.doj.value;
  var email = form.email.value;
  var selDate=form.selDate.value;
  var gender = form.gender.value;
  var inDateTime=form.inDateTime.value;
  var outDateTime=form.outDateTime.value;
  var message = form.message.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var ageRegex = /^([2-9][0-9])?$/;
  var salRegex = /^([1][0-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/;
  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 your name.',2);
    return false;
  }
  if(!name.match(nameRegex)) {
    inlineMsg('name','You have entered an invalid name.',2);
    return false;
  }
   if(age == "") {
    inlineMsg('age','You must enter your age.',2);
    return false;
  }
   if(!age.match(ageRegex)) {
    inlineMsg('age','You have entered an invalid age.',2);
    return false;
  }
   if(sal == "") {
    inlineMsg('sal','You must enter your salary.',2);
    return false;
  }
   if(!sal.match(salRegex)) {
    inlineMsg('sal','You have entered an invalid salary.',2);
    return false;
  }
   if(doj == "") {
    inlineMsg('doj','You must enter your date of joining.',2);
    return false;
  }
   if(!isValidDate(doj, 'M/D/Y')) {
    inlineMsg('doj','You have entered an invalid date of joining.',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(selDate == "") {
    inlineMsg('selDate','You must select the date.',2);
    return false;
  }
  if(gender == "") {
    inlineMsg('gender','You must select your gender.',2);
    return false;
  }
  if(inDateTime == "") {
    inlineMsg('inDateTime','You must select inDateTime.',2);
    return false;
  }
  if(outDateTime == "") {
    inlineMsg('outDateTime','You must select outDateTime.',2);
    return false;
  }
  if(!calculate(form)) {
    inlineMsg('outDateTime','The second date ocurred earlier than the first one.',2);
    return false;
  }
	
  if(message == "") {
    inlineMsg('message','You must enter a message.');
    return false;
  }
  if(message.match(messageRegex)) {
    inlineMsg('message','You have entered an invalid message.');
    return false;
  }
  return true;
}


//Check Booking form validation.

function checkBookForm(form) {
	//alert(form.therapy.value);
  	var contactName = form.contactName.value;
  	var contactAge = form.contactAge.value;
	var emailId = form.emailId.value;
	var contact = form.contact.value;
  	var therapy = form.therapy.value;
  	var date = form.date.value;
	var time = form.time.value;
  	var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  	var ageRegex = /^([1-9][0-9])?$/;
  	var salRegex = /^([1][0-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/;
  	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  	var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  	var phoneRegex =  /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
	var i = 0;
	for (i=1;i<=27;i++)
	{
		var hourcntrl = "hourControl"+i;
		if(document.getElementById(hourcntrl).style.display == 'inline'){
			 var citydur = "city"+i;
			 var j = 4+i;
			var duratn = form.elements[j].value;
		}
		
	}
	if(reason == "0") {
    	inlineMsg('reason','You must select the reason.',2);
   		return false;
  	}
	if(contactName == "") {
    	inlineMsg('contactName','You must enter your name.',2);
		return false;
  	}
 /* 	if(!contactName.match(nameRegex)) {
    	inlineMsg('contactName','You have entered an invalid name.',2);
    	return false;
  	}
   	if(contactAge == "") {
	    inlineMsg('contactAge','You must enter your age.',2);
    	return false;
  	}
   	if(!contactAge.match(ageRegex)) {
    	inlineMsg('contactAge','You have entered an invalid age.',2);
    	return false;
  	}*/
	
	if(phoneNo == "") {
    	inlineMsg('phoneNo','You must enter your phone number.',2);
   		return false;
  	}
  	if(!phoneNo.match(phoneRegex)) {
    	inlineMsg('phoneNo','You have entered an invalid phone number.',2);
    	return false;
  	}
	if(emailId == "") {
    	inlineMsg('emailId','You must enter your email.',2);
    	return false;
	}
	if(!emailId.match(emailRegex)) {
		inlineMsg('emailId','You have entered an invalid email.',2);
		return false;
	}
	if(subject == "") {
    	inlineMsg('subject','You must enter your name.',2);
		return false;
  	}
	if(query == "") {
    	inlineMsg('query','You must enter your name.',2);
		return false;
  	}
	/*if(duratn == "0") {
    	inlineMsg(citydur,'You must select the duration.',2);
   		return false;
  	}
	if(date == "") {
    	inlineMsg('date','You must select the date.',2);
   		return false;
  	}
	if(time == "") {
    	inlineMsg('time','You must enter the time.',2);
   		return false;
  	}
	return true;*/

}

//Check Booking form validation.

function checkSubsForm(form) {
	//alert(form.therapy.value);
	var emailId = form.emailId.value;
  	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

	if(emailId == "") {
    	inlineMsg('emailId','You must enter your email.',2);
    	return false;
	}
	if(!emailId.match(emailRegex)) {
		inlineMsg('emailId','You have entered an invalid email.',2);
		return false;
	}
	return true;

}

// feedback form validation
// form validation function //
function checkFeedForm(form) {
  var emailId = form.emailId.value;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  if(emailId == "") {
    inlineMsg('emailId','You must enter your email.',2);
    return false;
  }
  if(!emailId.match(emailRegex)) {
    inlineMsg('emailId','You have entered an invalid email.',2);
    return false;
  }
 return true;
}

// form validation function //
function checkTherpForm(form) {
  var contactName = form.contactName.value;
  var emailId = form.emailId.value;
  var phoneNo = form.phoneNo.value;
  var query = form.query.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var ageRegex = /^([2-9][0-9])?$/;
  var salRegex = /^([1][0-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  var phoneRegex =  /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
  if(contactName == "") {
    inlineMsg('contactName','You must enter your name.',2);
    return false;
  }
  if(!contactName.match(nameRegex)) {
    inlineMsg('contactName','You have entered an invalid name.',2);
    return false;
  }
  if(emailId == "") {
    inlineMsg('emailId','You must enter your email.',2);
    return false;
  }
  if(!emailId.match(emailRegex)) {
    inlineMsg('emailId','You have entered an invalid email.',2);
    return false;
  }
  if(phoneNo == "") {
    inlineMsg('phoneNo','You must enter your phone number.',2);
    return false;
  }
  if(!phoneNo.match(phoneRegex)) {
    inlineMsg('phoneNo','You have entered an invalid phone number.',2);
    return false;
  }
  if(query == "") {
    inlineMsg('query','You must enter a message.');
    return false;
  }
  if(query.match(messageRegex)) {
    inlineMsg('query','You have entered an invalid message.');
    return false;
  }
  return true;
}

//Validate career form

function checkCarForm(form) {
	//alert(form.therapy.value);
	var contactName = form.contactName.value;
	var emailId = form.emailId.value;
	var phoneNo = form.phoneNo.value;
	var resume = form.resume.value;
	var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var phoneRegex =  /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
	var fileRegex = /^.*\.(docx|DOCX|txt|TXT|xls|XLS|rtf|RTF|doc|DOC|pdf|PDF)$/;
	if(contactName == "") {
    	inlineMsg('contactName','You must enter your name.',2);
    	return false;
	}
  	if(!contactName.match(nameRegex)) {
    	inlineMsg('contactName','You have entered an invalid name.',2);
    	return false;
  	}
	if(emailId == "") {
    	inlineMsg('emailId','You must enter your email.',2);
    	return false;
	}
	if(!emailId.match(emailRegex)) {
		inlineMsg('emailId','You have entered an invalid email.',2);
		return false;
	}
	if(phoneNo == "") {
    	inlineMsg('phoneNo','You must enter your phone number.',2);
    	return false;
    }
  	if(!phoneNo.match(phoneRegex)) {
    	inlineMsg('phoneNo','You have entered an invalid phone number.',2);
		return false;
  	}
	if(resume == "") {
    	inlineMsg('resume','You must browse your resume.',2);
    	return false;
    }
  	if(!resume.match(fileRegex)) {
    	inlineMsg('resume','You have selected an invalid file type.',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;
}

//date validation
function isValidDate(dateStr, format)
{

	//check if 2nd parameter contains valid value or not
	//if not valid then set default format = 'MDY'
	if (format == null) { format = 'MDY' }
	format = format.toUpperCase();
	if (format.length != 3) { format = 'MDY' }
	if ( (format.indexOf('M') == -1) ||
	(format.indexOf('D') == -1) ||
	(format.indexOf('Y') == -1)
	)
	{ format = 'MDY' }
	
	if (format.substring(0, 1) == 'Y')
	{ // If the year is first
	var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
	var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
	}
	else if (format.substring(1, 2) == 'Y')
	{ // If the year is second
	var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
	var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
	}
	else
	{ // The year must be third
	var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
	var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	}
	
	// If it doesn’t conform to the right format
	//(with either a 2 digit year or 4 digit year), fail
	if ( (reg1.test(dateStr) == false) &&
	(reg2.test(dateStr) == false)
	)
	{ return false; }
	
	// Split into 3 parts based on what the divider was
	var parts = dateStr.split(RegExp.$1);
	
	// Check to see if the 3 parts end up making a valid date
	//extract month part
	if (format.substring(0, 1) == 'M') { var mm = parts[0]; }
	else if (format.substring(1, 2) == 'M') { var mm = parts[1]; }
	else { var mm = parts[2]; }
	
	//extract day part
	if (format.substring(0, 1) == 'D') { var dd = parts[0]; }
	else if (format.substring(1, 2) == 'D') { var dd = parts[1]; }
	else { var dd = parts[2]; }
	
	//extract Year part
	if (format.substring(0, 1) == 'Y') { var yy = parts[0]; }
	else if (format.substring(1, 2) == 'Y') { var yy = parts[1]; }
	else { var yy = parts[2]; }
	
	//if year is in 2 digit
	//00-49 are assumed to be 21st century and 50-99 are assumed to be 20th century
	if (parseFloat(yy) <= 50)
	{ yy = (parseFloat(yy) + 2000).toString(); }
	if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
	
	var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
	
	if (parseFloat(dd) != dt.getDate()) { return false; }
	
	if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
	
	return true;
	
	}
	// preload the arrow //
	if(document.images) {
	  arrow = new Image(7,80); 
	  arrow.src = "images/msg_arrow.gif"; 
}
function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else
	cntfield.value = maxlimit - field.value.length;
}
function calculate (form)
{
	 var d1= new Date();
	 var d2= new Date();
	 d1=getdate(form.inDateTime.value);
	 d2=getdate(form.outDateTime.value);
	var sec = d2.getTime() - d1.getTime();
	if (sec < 0)
		return false;
	else
		return true;

}
function getdate(MyDate)
{
	MD_i1=MyDate.indexOf(' ');
	MD_Date=MyDate.substring(0,MD_i1);
	MD_Time=MyDate.substring(MD_i1+1);
	MD_i2=MD_Date.indexOf('-');
	MD_i3=MD_Date.indexOf('-',MD_i2+1);
	MD_M=MD_Date.substring(0,MD_i2);
	MD_D=MD_Date.substring(MD_i2+1,MD_i3);
	MD_Y=MD_Date.substring(MD_i3+1);
	MD_i4=MD_Time.indexOf(':');
	MD_H=MD_Time.substring(0,MD_i4);
	//MD_N=parseInt(MD_Time.substring(MD_i4+1));
	MD_N=MD_Time.substring(MD_i4+1);
	MD_i5=MD_N.indexOf(' ');
	MD_MI=parseFloat(MD_N.substring(0,MD_i5));
	//alert(MD_MI);
	MD_S=0;
	if ((isNaN(MD_Y)) || (isNaN(MD_M)) || (isNaN(MD_D))|| (isNaN(MD_H)) || (isNaN(MD_MI)) || (isNaN(MD_S))) {
	//alert('Not numeric.');
	DObj='';
	} else {
	MD_M=MD_M-1; // Jan-Dec=00-11
	if (MD_Time.toLowerCase().indexOf('pm') > -1) {
	 if (MD_H != 12) {
	 MD_H=MD_H*1+12;
	 }
	 } else {
	 if (MD_H == 12) {
	 MD_H=0;
	 }
	}
	DObj=new Date(MD_Y, MD_M, MD_D, MD_H, MD_MI, MD_S);
	}
	return DObj;
}

