
function validate_category_form(form){
	
		var title = form.title.value; 	
		
		if( title == "") {
			inlineMsg('title','Title cannot be blank.',2);
			return false;
		}
		
		var isMainMenu = form.isMainMenu.value;
		
		if( isMainMenu == 1 ){
			
			var country = form.country.value;
			var type = form.type.value;
			
			if( country == 0) {
				inlineMsg('country','You must select a Country.',2);
				return false;
			}
			if( type == 0) {
				inlineMsg('type','You must select a Menu Type.',2);
				return false;
			}
			
		}
		
		if(!checkRadio(form.name, "status")) {
			inlineMsg('status','You must select status.',2);
			return false;
		} 
		return true;	
		
}


function validate_basicinfo_form(form){
	
		var title = form.title.value; 	
		
		if( title == "") {
			inlineMsg('title','Title cannot be blank.',2);
			return false;
		}
		
		if(!checkRadio(form.name, "status")) {
			inlineMsg('status','You must select status.',2);
			return false;
		} 
		return true;	
		
}


function validate_metainfo_form(form){
	
		var name = form.name.value; 
		var content = form.content.value;
		
		if( name == "") {
			inlineMsg('name','Tag name cannot be blank.',2);
			return false;
		}
		
		if( content == "") {
			inlineMsg('content','Tag content cannot be blank.',2);
			return false;
		}
		
		if(!checkRadio('form_metainfo', "status")) {			
			inlineMsg('status','You must select status.',2);
			return false;
		} 
	
		return true;	
}


function validate_member_form(form){
	
		var txtName 		= form.txtName.value; 		
		var txtDesignation 	= form.txtDesignation.value;
		var txtPostalAddress = form.txtPostalAddress.value;
		var txtEmail 		= form.txtEmail.value;
		
		var txtphoto 		= form.txtphoto.value;
		
		if( txtName == "") {
			inlineMsg('txtName','Name cannot be blank.',2);
			return false;
		}
		
		if( txtDesignation == "") {
			inlineMsg('txtDesignation','Designation cannot be blank.',2);
			return false;
		}
		
		if( txtPostalAddress == "") {
			inlineMsg('txtPostalAddress','Postal address cannot be blank.',2);
			return false;
		}
		
		if( txtEmail != "" && isValidEmail(txtEmail) == 0 ) {	  
			inlineMsg('txtEmail','Invalid email address.',2);
			return false;
		}
		
		if(txtphoto != '' && !isValidImage('txtphoto')){
			inlineMsg('txtphoto','Invalid image format[valid formats: jpg, gif & png].',2);
			return false;
		}
		
		if(!checkRadio(form.name, "status")) {			
			inlineMsg('status','You must select status.',2);
			return false;
		} 
	
		return true;	
}


function validate_feedback_form(form){
	
		var txtName 		= form.txtName.value; 		
		var txtEmail 		= form.txtEmail.value;
		var txtTitle 		= form.txtTitle.value;
		var txtDescription 	= form.txtDescription.value;
		var txtphoto 		= form.txtphoto.value;
		
		if( txtName == "") {
			inlineMsg('txtName','Name cannot be blank.',2);
			return false;
		}
		
		if( txtEmail == "") {
			inlineMsg('txtEmail','Email cannot be blank.',2);
			return false;
		}		
		if( isValidEmail(txtEmail) == 0 ) {	  
			inlineMsg('txtEmail','Invalid email address.',2);
			return false;
		}
		if( txtTitle == "") {
			inlineMsg('txtTitle','Feedback Title cannot be blank.',2);
			return false;
		}
		
		if( txtDescription == "") {
			inlineMsg('txtDescription','Description cannot be blank.',2);
			return false;
		}
		
		if(txtphoto != '' && !isValidImage('txtphoto')){
			inlineMsg('txtphoto','Invalid image format[valid formats: jpg, gif & png].',2);
			return false;
		}
		
		if(!checkRadio(form.name, "status")) {			
			inlineMsg('status','You must select status.',2);
			return false;
		} 
	
		return true;	
}


function validate_client_feedback_form(form){
	
		var txtName 		= form.txtName.value; 		
		var txtEmail 		= form.txtEmail.value;
		var txtTitle 		= form.txtTitle.value;
		var txtDescription 	= form.txtDescription.value;
		var txtphoto 		= form.txtphoto.value;
		
		if( txtName == "") {
			inlineMsg('txtName','Name cannot be blank.',2);
			return false;
		}
		
		if( txtEmail == "") {
			inlineMsg('txtEmail','Email cannot be blank.',2);
			return false;
		}		
		if( isValidEmail(txtEmail) == 0 ) {	  
			inlineMsg('txtEmail','Invalid email address.',2);
			return false;
		}
		if( txtTitle == "") {
			inlineMsg('txtTitle','Feedback Title cannot be blank.',2);
			return false;
		}
		
		if( txtDescription == "") {
			inlineMsg('txtDescription','Description cannot be blank.',2);
			return false;
		}
		
		if(txtphoto != '' && !isValidImage('txtphoto')){
			inlineMsg('txtphoto','Invalid image format[valid formats: jpg, gif & png].',2);
			return false;
		}
	
		return true;	
}




function validate_focus_form(form){
	
	
		var title 		= form.title.value; 
		
		var txtphoto 	= form.txtphoto.value;
		var isNewItem	= form.isNewItem.value;
		var prevImage	= form.prevImage.value;
		
		if( title == "") {
			inlineMsg('title','Title cannot be blank.',2);
			return false;
		}
		if(txtphoto == '' && isNewItem == 1){
			inlineMsg('txtphoto','You must upload trip header image.',2);
			return false;
		}
		
		if(txtphoto == '' && prevImage == ''){
			inlineMsg('txtphoto','You must upload trip header image.',2);
			return false;
		}
		
		if(txtphoto != '' && !isValidImage('txtphoto')){
			inlineMsg('txtphoto','Invalid image format[valid formats: jpg, gif & png].',2);
			return false;
		}
		
		if(!checkRadio(form.name, "status")) {			
			inlineMsg('status','You must select status.',2);
			return false;
		} 
	
		return true;	

}

function validate_stat_content_form(form){
	
	var title = trimAll(form.title.value);
	
	if (title == '') {
		inlineMsg('title','Title missing.',2);	
		return false;				
	}
	return true;
}

function validate_trip_form(form){
	
	var title	 	= trimAll(form.title.value);
	var menu_id		= form.menu_id.value;
	var duration	= trimAll(form.duration.value);
	
	var txtphoto 	= form.txtphoto.value;
	var isNewItem	= form.isNewItem.value;
	var prevImage	= form.prevImage.value;
	
	if (title == '') {
		inlineMsg('title','Trip Title missing.', 2);	
		return false;				
	}
	
	if(txtphoto == '' && isNewItem == 1){
		inlineMsg('txtphoto','You must upload trip header image.',2);
		return false;
	}
	
	if(txtphoto == '' && prevImage == ''){
		inlineMsg('txtphoto','You must upload trip header image.',2);
		return false;
	}
	
	if(txtphoto != '' && !isValidImage('txtphoto')){
		inlineMsg('txtphoto','Invalid image format[valid formats: jpg, gif & png].',2);
		return false;
	}
	
	if (menu_id == 0) {
		inlineMsg('menu_id','You must select Menu.', 2);	
		return false;				
	}
	if (duration=='') {
		inlineMsg('duration','Trek Days cannot be blank.', 2);	
		return false;				
	}	
	
	if( duration && !isValidInteger(duration) ){
		inlineMsg('duration','Invalid Format. Only Number allowed.', 2);	
		return false;
	}	
	
	if(!checkRadio(form.name, "status")) {			
		inlineMsg('status','You must select status.',2);
		return false;
	}
	return true;
}


function validate_departure_trip_form(form){
	
	var title	 	= trimAll(form.title.value);
	var country = form.country.value;
	var duration	= trimAll(form.duration.value);
	
	var txtphoto 	= form.txtphoto.value;
	var isNewItem	= form.isNewItem.value;
	var prevImage	= form.prevImage.value;
	
	var start_date	= form.start_date.value;
	var end_date	= form.end_date.value;
	
	
	if (title == '') {
		inlineMsg('title','Trip Title missing.', 2);	
		return false;				
	}
	
	if( country == 0) {
		inlineMsg('country','You must select a Country.',2);
		return false;
	}
	
	if(txtphoto == '' && isNewItem == 1){
		inlineMsg('txtphoto','You must upload trip header image.',2);
		return false;
	}
	
	if(txtphoto == '' && prevImage == ''){
		inlineMsg('txtphoto','You must upload trip header image.',2);
		return false;
	}
	
	if(txtphoto != '' && !isValidImage('txtphoto')){
		inlineMsg('txtphoto','Invalid image format[valid formats: jpg, gif & png].',2);
		return false;
	}
	if (duration=='') {
		inlineMsg('duration','Trek Days cannot be blank.', 2);	
		return false;				
	}	
	
	if (start_date=='') {
		inlineMsg('start_date','Start Date cannot be blank.', 2);	
		return false;				
	}	
	
	if (end_date=='') {
		inlineMsg('end_date','End Date cannot be blank.', 2);	
		return false;				
	}
	
	if( duration && !isValidInteger(duration) ){
		inlineMsg('duration','Invalid Format. Only Number allowed.', 2);	
		return false;
	}	
	
	if(!checkRadio(form.name, "status")) {			
		inlineMsg('status','You must select status.',2);
		return false;
	}
	return true;
}


function validate_hotel_form(form){
	
	var title	 	= trimAll(form.title.value);
	var menu_id		= form.menu_id.value;
	
	var delux_plan	= form.delux_plan.value;
	var std_plan	= form.std_plan.value;
	var bud_plan	= form.bud_plan.value;
	
	var delux_price	= trimAll(form.delux_price.value);
	var std_price	= trimAll(form.std_price.value);
	var bud_price	= trimAll(form.bud_price.value);
	
	
	var txtphoto 	= form.txtphoto.value;
	var isNewItem	= form.isNewItem.value;
	var prevImage	= form.prevImage.value;
	
	if (title == '') {
		inlineMsg('title','Trip Title missing.', 2);	
		return false;				
	}
	
	if(txtphoto == '' && isNewItem == 1){
		inlineMsg('txtphoto','You must upload trip header image.',2);
		return false;
	}
	
	if(txtphoto == '' && prevImage == ''){
		inlineMsg('txtphoto','You must upload trip header image.',2);
		return false;
	}
	
	if(txtphoto != '' && !isValidImage('txtphoto')){
		inlineMsg('txtphoto','Invalid image format[valid formats: jpg, gif & png].',2);
		return false;
	}
	
	if (menu_id == 0) {
		inlineMsg('menu_id','You must select Menu.', 2);	
		return false;				
	}
	
	if( delux_price && !isValidNumber(delux_price) ){
		inlineMsg('delux_price','Invalid Format. Only Number(supports decimal format) allowed.', 2);	
		return false;
	}	
	
	if( std_price && !isValidNumber(std_price) ){
		inlineMsg('std_price','Invalid Format. Only Number(supports decimal format) allowed.', 2);	
		return false;
	}
	if( bud_price && !isValidNumber(bud_price) ){
		inlineMsg('bud_price','Invalid Format. Only Number(supports decimal format) allowed.', 2);	
		return false;
	}
	
	if(!checkRadio(form.name, "status")) {			
		inlineMsg('status','You must select status.',2);
		return false;
	}
	
	return true;
}


function validate_country_form(form){
		
	var txtphoto 	= form.txtphoto.value;
	var isNewItem	= form.isNewItem.value;
	var prevImage	= form.prevImage.value;
		
	if(txtphoto == '' && isNewItem == 1){
		inlineMsg('txtphoto','You must upload trip header image.',2);
		return false;
	}
	
	if(txtphoto == '' && prevImage == ''){
		inlineMsg('txtphoto','You must upload trip header image.',2);
		return false;
	}
	
	if(txtphoto != '' && !isValidImage('txtphoto')){
		inlineMsg('txtphoto','Invalid image format[valid formats: jpg, gif & png].',2);
		return false;
	}

	if(!checkRadio(form.name, "status")) {			
		inlineMsg('status','You must select status.',2);
		return false;
	}
	
	return true;
}



function validate_trip_pkg_form(form){

	var trip_id		= form.trip_id.value;
	var type		= form.type.value;	
	//var cost		= trimAll(form.cost.value);
	

	if (trip_id == 0) {
		inlineMsg('trip_id','You must select Trip.', 2);	
		return false;				
	}	
	if (type == 0) {
		inlineMsg('type','You must select Package Type.', 2);	
		return false;				
	}
	if(isTxtboxArrayEmpty('inclusive[]') ){
		inlineMsg('inclusive','Inclusive Cannot be blank. Specify at least one value', 2);	
		return false;
	}
	if(isTxtboxArrayEmpty('exclusive[]') ){
		inlineMsg('exclusive','Inclusive Cannot be blank. Specify at least one value', 2);	
		return false;
	}
	/*
	if (cost=='') {
		inlineMsg('cost','Cost cannot be blank.', 2);	
		return false;				
	}	
	if( cost && !isValidNumber(cost) ){
		inlineMsg('cost','Invalid Format. Only Number(supports decimal format) allowed.', 2);	
		return false;
	}
	*/
	
	
	return true;
}


function validate_trip_itinerary_form(form){
	
	if( validateTxtboxArray('description[]') )
		return true;
	
	else
		return false;
}


function validate_trip_reservation_form(form){	
	
	var person_count	= form.person_count.value;
	var person_name		= form.person_name.value;
	var phone			= form.phone.value;
	var email			= form.email.value;
	var country			= form.country.value;
	var city			= form.city.value;
	
	if( person_count != '' && isNaN(person_count) ){
		inlineMsg('person_count','Invalid Format. Only Number allowed.', 2);	
		return false;
	}
	
	if( person_name == '' ){
		inlineMsg('person_name','Please specify your name.', 2);	
		return false;
	}
	
	if( phone == '' ){
		inlineMsg('phone','Please specify your phone.', 2);	
		return false;
	}
	if( email == "") {
		inlineMsg('email','Email cannot be blank.',2);
		return false;
	}		
	if( isValidEmail(email) == 0 ) {	  
		inlineMsg('email','Invalid email address.',2);
		return false;
	}

	if( country == '' ){
		inlineMsg('country','Please specify your country.', 2);	
		return false;
	}
	if( city == '' ){
		inlineMsg('city','Please specify your city.', 2);	
		return false;
	}
	return true;
}


function validate_fixedept_reservation_form(form){
	
	var fixeddept			= form.fixeddept.value;
	var person_name		= form.person_name.value;
	var phone			= form.phone.value;
	var email			= form.email.value;
	var address			= form.address.value;
	var country			= form.country.value;
	var city			= form.city.value;
	
	if( fixeddept == '' ){
		inlineMsg('fixeddept','Please select a program.', 2);	
		return false;
	}
	
	if( person_name == '' ){
		inlineMsg('person_name','Please specify your name.', 2);	
		return false;
	}
	
	if( phone == '' ){
		inlineMsg('phone','Please specify your phone.', 2);	
		return false;
	}
	if( email == "") {
		inlineMsg('email','Email cannot be blank.',2);
		return false;
	}		
	if( isValidEmail(email) == 0 ) {	  
		inlineMsg('email','Invalid email address.',2);
		return false;
	}
	if( address == '' ){
		inlineMsg('address','Please specify your address.', 2);	
		return false;
	}
	if( city == '' ){
		inlineMsg('city','Please specify your city.', 2);	
		return false;
	}
	if( country == '' ){
		inlineMsg('country','Please specify your country.', 2);	
		return false;
	}
	
	return true;
}

function validate_callback_form(form){
	
	var contact_name  = form.contact_name.value;
	var contact_phone = form.contact_phone.value;
	
	if( contact_name === '' || contact_name === 'Enter your name' ){
		inlineMsg('contact_name','Please specify your name.', 2);	
		return false;
	}
	
	if( contact_phone === '' || contact_phone === 'Enter your phone' ){
		inlineMsg('contact_phone','Please specify your phone number.', 2);	
		return false;
	}
	
	return true;
	
}






function trimAll( strValue ) {
	var objRegExp = /^(\s*)$/;
		if(objRegExp.test(strValue)) {
		   strValue = strValue.replace(objRegExp, '');
		   if( strValue.length == 0)
			  return strValue;
		}
	   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	   if(objRegExp.test(strValue)) {
		   strValue = strValue.replace(objRegExp, '$2');
		}
	  return strValue;
}


function validateTxtboxArray(txtfieldName) {
	
	var txt = document.getElementsByName(txtfieldName);
	
	for (var i = 0; i < txt.length; i++){		
		if (txt[i].value == ''){
			inlineMsg(txt[i].id,'Blank Field.',2);
			return false;
		}
	}
	return true;	
}


function isAnyItemOfTxtboxArrayEmpty(txtfieldName) {
	
	var txt = document.getElementsByName(txtfieldName);
	
	var isEmpty = false;
	
	for (var i = 0; i < txt.length; i++){		
		if (txt[i].value == ''){			
			isEmpty =  true;
		}
	}
	return isEmpty;	
}


function isTxtboxArrayEmpty(txtfieldName) {
	
	var txt = document.getElementsByName(txtfieldName);
	
	var isEmpty = true;
	
	for (var i = 0; i < txt.length; i++){		
		if (txt[i].value != ''){			
			isEmpty =  false;
		}
	}
	return isEmpty;	
}


function isValidCheckBoxArray(checkboxName) {
	
	var chks = document.getElementsByName(checkboxName);	
	var hasChecked = false;
	for (var i = 0; i < chks.length; i++){
		if (chks[i].checked){
			hasChecked =  true;
		}
	}
	if(!hasChecked){
		return false;
	}		
	return true;
}



function isValidInteger(integer){
  if(/^-?\d+$/.test(integer)) 
  	return true;

  return false;
}

function isValidNumber(number){  // for decimal number
  if(isNaN(number))
  	return false;
  else 
    return true;
		
}

function isValidImage(elementID)
{
	//alert('image validation');
	var imgpath = document.getElementById(elementID).value;
	if(imgpath != "")
	{
		// code to get File Extension..
		var arr1 = new Array;
		arr1 = imgpath.split("\\");
		var len = arr1.length;
		var img1 = arr1[len-1];
		var filext = img1.substring(img1.lastIndexOf(".")+1);
		
		filext = filext.toLowerCase();
		
		// Checking Extension
		if(filext == "jpg" || filext == "gif" || filext == "png")
			return true;
		
		else{
			return false;
		}
	}
	return false;
}

function isValidURL(url) {
 	var urlRegxp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
	
	if (urlRegxp.test(url) != true) {
		return false;
	} else {
		return true;
	}
}

function isValidEmail(strEmail){	
	var msg;
	var i;
	var len=strEmail.length;
	if(strEmail=="") {
			msg="0";
	} 
	else {
		if((strEmail.charAt(0)=="@") || (strEmail.charAt(0)==".")) {
			msg="0";
		} else {
	//	checking the number of the @ and dots in the email add	
			var countAt=0;
			var countDot=0;
			for (i=0; i<len; i++) {
				if(strEmail.charAt(i)=="@") {
					countAt=countAt+1;
				}	
				if(strEmail.charAt(i)==".") {
					countDot=countDot+1;
				}
			} //end of for (i=0; i<strEmail.length; i++) {...	
			if((countAt!=1) || (countDot<1)) {
				msg="0";
			} else {
		//	checking the position of the @ with respect to the dot 
				var posDot=0;
				var posAt=0;
				posDot=strEmail.lastIndexOf(".");
				posAt=strEmail.indexOf("@");
				if((posAt>posDot) || (posDot==(posAt+1))) {
					msg="0";
				} else {
			//	checking the dot position and the @ postion at the end
					if((strEmail.charAt(len)=="@") || (strEmail.charAt(len)==".")) {
						msg="0";
					} else {
						/* 	checking for the invalid characters
						 if(ereg('[^A-Za-z0-9_]', strEmail)) {
					msg="Only letters, numbers and underscores(_).";		*/
						msg = "1";
					} //end of if((strEmail.charAt(len)=="@") || (strEmail.charAt(len)==".")) {...
				} //end of if((posAt>posDot) || (posDot==(posDot+1))) {...
			} //end of if((countAt!=1) ||(countDot<1)) {...		
		} //end  if((strEmail.charAt(i)=="@") || (strEmail.charAt(i)==".")) {...
	} //end of if(strEmail=="") {...
	return msg;	
}


function checkRadio(frmName, rbGroupName) {
	
	var radios = document[frmName].elements[rbGroupName];
	
	for (var i=0; i <radios.length; i++) {
		if (radios[i].checked) {
			return true;
		}
	}
	return false;
} 
// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

	
function inlineMsg(target, string, autohide) {	
  var msg;
  var msgcontent;  
	//alert(string);
  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 = "http://media2point0.com/js/images/msg_arrow.gif"; 
  arrow.src = baseurl + "/js/images/msg_arrow.gif"; 
  //arrow.src = "http://192.168.51.2/humorsite/js/images/msg_arrow.gif"; 
  
}