// JavaScript Document
function getSubMenu(menuId, menuName, submenuid) {
	if(submenuid)
		smenu_url = "&submenuid="+submenuid;
	else 	
		smenu_url = "";
	location.href="index.php?page="+menuName+"&menu_id="+menuId+smenu_url;
}

function div_translate(div_name, status) {
	if ((status=='show') && (document.getElementById(div_name).style.display=='none'))
		document.getElementById(div_name).style.display='';
	else 
		document.getElementById(div_name).style.display='none';
}

function val_user() {
var val=true;
if (document.getElementById("user_fname").value =='') {
		alert("Please enter first name");
		document.getElementById("user_fname").focus();
		val = false;
	} else if (document.getElementById("user_lname").value =='') {
		alert("Please enter last name");
		document.getElementById("user_lname").focus();
		val = false;
	} else if (document.getElementById("company_name").value =='') {
		alert("Please enter Company name");
		document.getElementById("company_name").focus();
		val = false;
	} else if (document.getElementById("username").value =='') {
		alert("Please enter user name");
		document.getElementById("username").focus();
		val = false;
	} else if (document.getElementById("pwd").value =='') {
		alert("Please enter password");
		document.getElementById("pwd").focus();
		val = false;
	} else if (email_val(document.users_add.email_add)==false) {
		document.getElementById("email_add").focus();
		val = false;
	} else {
		if (val==true) {
			document.getElementById("form_submit_val").value=1;
			document.users_add.submit();
		} else {
			return false;	
		}
	}
}

function cancel_user() {
	location.href="index.php?page=Home&menu_id=1";
}

function email_val(field) {
	RegExp=/\w+(@{1})+[0-9a-zA-Z]+[.-]+[^+*=$@%][a-zA-Z.]/g;
    ntest=RegExp.test(field.value);

  	if (!ntest)
	{
	   alert("Invalid Email Id!");
	   field.select();
	   field.focus();
	   return false;
	}
	if(field.value!="")
	{
	   var FN = field.value.substring(0,1);
	   if ( FN == ' ')
	   {
		 alert("\n In the Email Id, first character should not be a space");
		 field.select();
		 field.focus();
		 return false;
	   }          
	}
}

function focus_uname() {
if (document.getElementById('uname'))
	document.getElementById('uname').focus();
}


function val_login() {
	if ((document.getElementById("uname").value=='') && (document.getElementById("upass").value=='')) {
		alert("Please enter Username and Password");
		document.getElementById("uname").focus();
		return false;
	} else if (document.getElementById("uname").value=='') {
		alert("Please enter Username");
		document.getElementById("uname").focus();
		return false;
	} else if (document.getElementById("upass").value=='') {
		alert("Please enter Password");
		document.getElementById("uname").focus();
		return false;
	} else {
		return true;
	}
}

function getHTTPObject() {
  var xmlhttp; 
  if(window.XMLHttpRequest) {
	  xmlhttp = new XMLHttpRequest(); 
  } 
  else if (window.ActiveXObject) { 
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
	  if (!xmlhttp){ 
		  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
 	  } 
  } 
  return xmlhttp; 
} 

function change_language(lang_val) {
	var http = getHTTPObject();
 	var url='ajaxfunc.php?change_language=true&lang_value='+lang_val;	

	http.onreadystatechange=function() {
    	if (http.readyState==4 ) {
		   document.getElementById("lang_sel").innerHTML = http.responseText;
		   location.reload();
    	}
  	}
	http.open("GET",url,true);
	http.send(null);
}

function search_content() {
	var search_val = document.getElementById("search").value;
	if(search_val=='') {
		alert("Please enter search criteria");
		document.search_form.action ='';
		document.getELementById("search").focus();
		return false;
	} 
	else if(search_val=='Search Item') {
		alert("Please enter search criteria");
		document.search_form.action ='';
		document.getELementById("search").focus();
		return false;
	} 
	else {
		document.search_form.action = 'index.php?page=search';
		return true;	
	}

}

function check_exists (username) {
	var http = getHTTPObject();
 	var url='ajaxfunc.php?check_uname=true&uname='+username;	

	http.onreadystatechange=function() {
    	if (http.readyState==4 ) {
			if (http.responseText=='1') {
				alert("Username already exists !");
				document.getElementById("username").value ='';
				document.getElementById("username").focus();
			}
		   //document.getElementById("uname_error").innerHTML = http.responseText;
    	}
  	}
	http.open("GET",url,true);
	http.send(null);
}

function val_contact() {
	var val=true;
	var phone_val = document.contact_info.phone;
	if (document.getElementById("company").value =='') {
		alert("Please enter value for Company");
		document.getElementById("company").focus();
		val = false;
	} else if (isValidEmail(document.contact_info.email)==false) {
		alert("Please enter valid email id");
		document.getElementById("email").focus();
		val = false;
	} else if ((checkNumber(phone_val))==false) {
		alert("Please enter valid Phone Number");
		document.getElementById("phone").focus();
		val = false;
	} else {
		if (val==true) {
			document.getElementById("form_submit_contact").value=1;
			document.contact_info.submit();
		} else {
			return false;	
		}
	}
}

function checkNumber(Field){
  if(!containsSomething(Field) || isNaN(Field.value)){
    return false;
  }
  return true;
}

function containsSomething(Field){
	if ((Field.type == "text") || (Field.type == "textarea") || (Field.type == "password") || (Field.type == "hidden")){
		Str = trimXSpaces(Field.value);
		if (Str == ""){
			return false;
		}
	}
	else{
    if (returnSelection(Field) == null){
      return false;
    }
  }

  return true;
}

function trimXSpaces(Str){
	if(!Str.length)
  	return Str;

  while(Str.charAt(0) == ' ')
  	Str = Str.substring(1, Str.length);

  if(!Str.length)
  	return Str;

  while(Str.charAt(Str.length - 1) == ' ')
  	Str = Str.substring(0, Str.length - 1);

  return Str;
}

function returnSelection(radioField){
	var selection = null;
  for (i=0; i < radioField.length; i++){
		if (radioField[i].checked){
			selection=radioField[i].value;
			return selection;
		}
	}
  return selection;
}

function isValidEmail(Field){
	if (!containsSomething(Field)){
  	return false;
  }
  if (Field.value.indexOf("@")==-1 || Field.value.indexOf(".")==-1 || trimXSpaces(Field.value).indexOf(" ")!=-1 || Field.value.length<6){
  	return false;
  }
  else{
  	return true;
  }
}


