

// JavaScript Document

	function redirect(url) { 

		document.location.href = url;
	}

function showandhidesearch(val) { 
			if(val == 'Search') { 
				$("#txtSearch").val("");
			}
		}

		function formsubmit() { 
			if(document.myform.txtSearch.value == '' || document.myform.txtSearch.value == 'Search') { 
				alert('Enter search keyword');
				return false;
			} else { 
				document.myform.method= 'post';
				document.myform.action='search.php';
				document.myform.submit();
			}		
		}

		function showandhidesearchOnBlur(val) { 
			if(val == '') { 
				$("#txtSearch").val("Search");
			}
		}
		
/**
  * Below functions is used to EMAIL the values
  *
  */
	
	function checkEmailFormat(email) {
		invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";
		// Check for invalid characters as defined above
		for (i=0; i<invalidChars.length; i++) {
			badChar = invalidChars.charAt(i);
			if (email.indexOf(badChar,0) > -1) {
				return false;
			}
		}
		
		lengthOfEmail = email.length;
		if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
			return false;
		}
		
		Pos = email.indexOf("@",1);
		if (email.charAt(Pos + 1) == ".") {
			return false;
		}
		
		while ((Pos < lengthOfEmail) && ( Pos != -1)) {
			Pos = email.indexOf(".",Pos);
			if (email.charAt(Pos + 1) == ".") {
				return false;
			}
			if (Pos != -1) {
				Pos++;
			}
		}

	// There must be at least one @ symbol
		atPos = email.indexOf("@",1);
		if (atPos == -1) {
			return false;
		}
		// But only ONE @ symbol
		if (email.indexOf("@",atPos+1) != -1) {
			return false;
		}
		// Also check for at least one period after the @ symbol
		periodPos = email.indexOf(".",atPos);
		if (periodPos == -1) {
			return false;
		}
		if (periodPos+3 > email.length) {
			return false;
		}
		return true;
	}
	

/*
 * Forget Passwords
 *
 */

	function forgetPassword() {
		if (window.XMLHttpRequest) {
			http = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			http = new ActiveXObject("Microsoft.XMLHTTP");
		}
	   
		var url = 'forgetPassword.php?';
		var emailaddress	=	trim(document.getElementById('txtEmailAddress').value);
		
		if(emailaddress != '') {
			
			if(checkEmailFormat(emailaddress) == false) { 
				$("#forget_message").html("");
				document.getElementById('email_address_error').style.display = 'inline';
				document.getElementById('email_address_error').innerHTML = '<img src="images/wrong.gif" border=0>Email address format is wrong';
			
			} else { 
				$("#forget_message").html("");
				document.getElementById('email_address_error').style.display = 'none';
				$("#forget_message").html("<font color=red>Verifying your email address</font>&nbsp;&nbsp;<img src='images/preloader.gif' border=0>");
				var fullurl = url + 'action=forget&username=' + emailaddress;
				http.open("GET", fullurl, true);
				http.send(null);
				http.onreadystatechange = forget_username;
			}
			
		}else{
			$("#forget_message").html("");
			document.getElementById('email_address_error').style.display = 'inline';
			document.getElementById('email_address_error').innerHTML = '<img src="images/wrong.gif" border=0>Email Address field is must';
		}
	}
	
	function forget_username() {
		if (http.readyState == 4) {
			//alert(parseInt(http.responseText));
			
			if(parseInt(http.responseText) == '0'){
				var html	=	'Password has been sent to your Mail Id';	
			}
			
			if(parseInt(http.responseText) == '3'){
				var html	=	'Your request cannot be processed as your registration is pending for approval';	
			} 
			
			if(parseInt(http.responseText) == '2'){
				var html	=	'Email id is not registered';	
			}
			
			htmlconc	=	"<font color=red>"+html+"</font>";
			$("#forget_message").html(htmlconc);
			
		}
	} 


/**
  * Assign Check Boxes
  *
  */
	function assingThisValue(field,k)  {
	    fieldhidden	=	document.getElementById('groupcheckbox');
		var chkstring;		
		chkstring = fieldhidden.value;
		field	=	eval(document.myform_menu_add.assingGroup[k]);
		
		if(eval(field.checked)) {
			if(fieldhidden.value == '') {
				chkstring = ','+field.value+',' ;
				alert(chkstring);
			} else { 
				chkstring += field.value+',' ;
			}
		} else {	
			var sstr = "";
			sstr = fieldhidden.value;
			var objs = sstr.split(",");
			fieldhidden.value ="";
			chkstring="";
			for (var i=0;i<objs.length-1;i++)
				if (objs[i] != field.value)
					chkstring += objs[i] + ",";
		}															
		 fieldhidden.value = chkstring;
	}



/**
  * 
  * Belwo function is used to check number
  */

	function isInteger(s) {
		var i;
		for (i = 0; i < s.length; i++){   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		return true;
	}

/**
  * Below functions is used to check the alpha numaric variables
  *
  */

	function checkAlphabetsResult(sText){
	   var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
	   var IsNumber=true;
	   var Char; 
	   for (i = 0; i < sText.length && IsNumber == true; i++)  {
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1) {
		 	IsNumber = false;
		  }
	   }
	   return IsNumber;   
	}
/**
  * Below functions is used to trim the values
  *
  */
	function trim(s) {
		while (s.substring(0,1) == ' ') {
			s = s.substring(1,s.length);
		}
		while (s.substring(s.length-1,s.length) == ' ') {
			s = s.substring(0,s.length-1);
		}
		 return s;		
	}


/**
  * Belwo function is used to trim all function
  *
  **/
  function trimAll(sString){
		while (sString.substring(0,1) == ' '){
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' '){
			sString = sString.substring(0,sString.length-1);
		}
		return sString;
	}


/**
  * Below functions is used to Check The URL
  *
  */
	
	
	function checkUrl(theUrl){
		if(theUrl.match(/^w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) || theUrl.match(/^mailto\:\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w{2,4}$/i))  {
			return true;
		} else {
			return false;
		}
	}

/**
  * Below functions is used to Check The Number
  *
  */

	function checkPhoneNumber(sText){
		var ValidChars = "0123456789-";
		var IsNumber=true;
		var Char; 
		for (i = 0; i < sText.length && IsNumber == true; i++) { 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) {
				IsNumber = false;
			}
		}
		return IsNumber;   
	}	

	// Number key check
	function isNumberKey(evt) {
		
		var charCode = (evt.which) ? evt.which : event.keyCode
		
		if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

		return true;
	}


	function isNumberKeyd(evt){
		var charCode = (evt.which) ? evt.which : event.keyCode
		if (charCode > 31 && (charCode < 48 || charCode > 57)) {
			if (charCode != 46) {

				return false;
			}
		}	
	
		return true;
	}
	
	function on_blur(val,div_error,div_sucess){
	  //validate name field
	 
	  if(val==""){
		document.getElementById(div_error).style.display="inline";
		document.getElementById(div_sucess).style.display="none";
	  }else{
		document.getElementById(div_sucess).style.display="inline";
		document.getElementById(div_error).style.display="none";
	  }
	}

	function on_click(div_error,div_sucess){
	  //Clear the Name Box
	  //document.getElementById("name").value="";
	  //Hide error and success Messages
	  document.getElementById(div_error).style.display="none";
	  document.getElementById(div_sucess).style.display="none";
	}
	function on_blur_form(id,val,div_error,div_sucess,msg,type,mandatoryStatus){

		var res	=	true;
		  // Check Empty
		if(mandatoryStatus == true) { 
			if(val == ""){
			
				document.getElementById(div_error).style.display="inline";
				document.getElementById(div_error).innerHTML = '<img src="images/wrong.gif"/>&nbsp;'+msg+" is must";
				document.getElementById(div_sucess).style.display="none";
				res	=	false;
			}
		} 
		// Check Alphabets	  
		 
		if(mandatoryStatus == true || (mandatoryStatus == false && val != '')) {  
			 if(type == "Alphabets") { 
				if(checkAlphabetsResult(trim(val)) == false) { 
					document.getElementById(div_error).style.display="inline";
					document.getElementById(div_error).innerHTML = '<img src="images/wrong.gif"/>&nbsp;'+msg+" Only allowed Alphapets";
					document.getElementById(id).value = '';
					res	=	false;
				}	
			  }
			  // Check Email
			  if(type == "Email" && res == true) { 
				if(checkEmailFormat(trim(val)) == false) { 
					document.getElementById(div_error).style.display="inline";
					document.getElementById(div_error).innerHTML = '<img src="images/wrong.gif"/>&nbsp;'+msg+" Format is Wrong";
					document.getElementById(id).value = '';
					res	=	false;
				}	
			  }
			  // Check Website
			  if(type == "URL") { 
				if(checkUrl(trim(val)) == false) { 
					document.getElementById(div_error).style.display="inline";
					document.getElementById(div_error).innerHTML = '<img src="images/wrong.gif"/>&nbsp;'+msg+" Format is Wrong";
					res	=	false;
				}	
			  }
			  // Check Indian Phone Numbers
			  if(type == "Phone") { 
					if(checkPhoneNumber(trim(val)) == false) { 
						document.getElementById(div_error).style.display="inline";
						document.getElementById(div_error).innerHTML = '<img src="images/wrong.gif"/>&nbsp;'+msg+" Format is Wrong";
						document.getElementById(id).value = '';
						res	=	false;
					}	
			  }
			 // Numerb check
 			  if(type == "Number") { 
				if(isInteger(trim(val)) == false) { 
					document.getElementById(div_error).style.display="inline";
					document.getElementById(div_error).innerHTML = '<img src="images/wrong.gif"/>&nbsp;'+msg+" is not Number";
					document.getElementById(id).value = '';
					res	=	false;
				}	
			  }
			  
			  
			  if(type == "DropDown") { 
					if(val == 0) { 
						document.getElementById(div_error).style.display="inline";
						document.getElementById(div_error).innerHTML = '<img src="images/wrong.gif"/>&nbsp;'+msg+" is must";
						res	=	false;
					}	
			  }
			 
			 
			 if(res == true){
				document.getElementById(div_sucess).style.display="inline";
				document.getElementById(div_error).style.display="none";
			  }
		} else { 
				document.getElementById(div_sucess).style.display="none";
				document.getElementById(div_error).style.display="none";
		}
	}
	
	function on_blur_form_price(id,val,val1,div_error,div_right) { 
		
		if(val1 == true) { 
		
			if(trim(val) == '') { 
				document.getElementById(div_error).style.display="inline";
				document.getElementById(div_error).innerHTML = '<img src="images/wrong.gif"/>&nbsp;Price is must';
			} else if(isInteger(trim(val)) == false) { 
				document.getElementById(div_error).style.display="inline";
				document.getElementById(div_error).innerHTML = '<img src="images/wrong.gif"/>&nbsp;Price is not number';
			} else { 
				document.getElementById(div_right).style.display="inline";
			}
		}
	
	}
	
	function approveRejectedDeleted(id) { 
	
		if (window.XMLHttpRequest) {
			http = new XMLHttpRequest();
	   } else if (window.ActiveXObject) {
			http = new ActiveXObject("Microsoft.XMLHTTP");
	   }
	   
	   $("#msg_user_status").html("Mail Send Processing.");
	   $("#msg_user_status").fadeIn(500);
	   
	   comments=prompt("Please enter your Comments","Enter your comments here");
	   
	   var url = 'rejectedstatus.php?id='+id+'&comments='+comments;
	   http.open("GET", url, true);
	   http.send(null);
	   http.onreadystatechange = approve_rejected_del;
	
	}

	function approve_rejected_del() {
		
		if (http.readyState == 4) {
			$("#msg_user_status").html("<font color=red>User Status Updated successfully</font>");
	   		$("#msg_user_status").fadeIn(1000).fadeOut(1000).fadeIn(1000).fadeOut(1000).fadeIn(1000).fadeOut(500);
			pagination(0);
		}
	} 

	
	function approveRejected(field_name,table_name,type,status,id) { 
	  //alert('Type->'+type);
	   if (window.XMLHttpRequest) {
			http = new XMLHttpRequest();
	   } else if (window.ActiveXObject) {
			http = new ActiveXObject("Microsoft.XMLHTTP");
	   }
	   
	   $("#msg_user_status").html("Mail Send Processing.");
	   $("#msg_user_status").fadeIn(500);
	   
	   var url = 'approvestatus.php?';
	   if(status == 2) {
	   		comments=prompt("Please enter your Comments","Enter your comments here");
	   		var fullurl = url +'comments='+comments+'&field_name='+field_name+'&table_name='+table_name+'&status='+status+'&type='+type+'&id='+id;
	   } else { 
	   		var fullurl = url +'&field_name='+field_name+'&table_name='+table_name+'&status='+status+'&type='+type+'&id='+id;
	   }
	   
	   
	   
	   http.open("GET", fullurl, true);
	   http.send(null);
	   http.onreadystatechange = approve_rejected;
	   
	
	}

	function approve_rejected() {
		
		if (http.readyState == 4) {
			var strResult	=	http.responseText;
			var srtArray	=	strResult.split("||");
			var div			=	srtArray[0];
			var content		=	srtArray[1];
			
			$("#msg_user_status").html("<font color=red>User Status Updated successfully</font>");
	   		$("#msg_user_status").fadeIn(1000).fadeOut(1000).fadeIn(1000).fadeOut(1000).fadeIn(1000).fadeOut(500);
			pagination(0);
		}
	} 

	
	function groupCheck(title) { 
	 
	  document.getElementById('group_already_exist_error').style.display = 'none';
	   if (window.XMLHttpRequest) {
			http = new XMLHttpRequest();
	   } else if (window.ActiveXObject) {
			http = new ActiveXObject("Microsoft.XMLHTTP");
	   }
	   
		var url = 'ajax_group_check.php?';
		if(title != '') {
			var id	=	document.getElementById('group_id').value;
			var fullurl = url + 'action=group&title=' + trim(title)+"&id="+id;
			http.open("GET", fullurl, true);
			http.send(null);
			http.onreadystatechange = group_check;
		}
	}
	
	function group_check() {
		if (http.readyState == 4) {
			var strResult	=	http.responseText;
			var responseResult  =	strResult.split("loginresult:");
			if(responseResult[1] == '1') { 
				
				document.getElementById('txtGroups').value = '';
				//document.getElementById('txtSubRegionTitle').value = '';
				
				document.getElementById('title_right').style.display = 'none';				
				document.getElementById('group_already_exist_error').style.display = 'inline';
				document.getElementById('group_already_exist_error').innerHTML = '<img src="images/wrong.gif" border=0>Title already Exists';

			} else { 
				document.getElementById('group_already_exist_error').style.display = 'none';				
			}
		}
	} 

	function sub_menuCheck(title) { 
	 
	  document.getElementById('sub_menu_already_exist_error').style.display = 'none';
	   if (window.XMLHttpRequest) {
			http = new XMLHttpRequest();
	   } else if (window.ActiveXObject) {
			http = new ActiveXObject("Microsoft.XMLHTTP");
	   }
	   
		var url = 'ajax_sub_menu_check.php?';
		if(title != '') {
			var id	=	document.getElementById('sub_menu_id').value;
			var fullurl = url + 'action=sub_menu&title=' + trim(title)+"&id="+id;
			http.open("GET", fullurl, true);
			http.send(null);
			http.onreadystatechange = sub_menu_check;
		}
	}
	
	function sub_menu_check() {
		if (http.readyState == 4) {
			var strResult	=	http.responseText;
			var responseResult  =	strResult.split("loginresult:");
			if(responseResult[1] == '1') { 
				
				document.getElementById('txtSubMenu').value = '';
				//document.getElementById('txtSubRegionTitle').value = '';
				
				document.getElementById('title_right').style.display = 'none';				
				document.getElementById('sub_menu_already_exist_error').style.display = 'inline';
				document.getElementById('sub_menu_already_exist_error').innerHTML = '<img src="images/wrong.gif" border=0>Sub Menu already Exists';

			} else { 
				document.getElementById('sub_menu_already_exist_error').style.display = 'none';				
			}
		}
	} 


function sub_sub_menuCheck(title) { 
	 
	  document.getElementById('sub_sub_menu_already_exist_error').style.display = 'none';
	   if (window.XMLHttpRequest) {
			http = new XMLHttpRequest();
	   } else if (window.ActiveXObject) {
			http = new ActiveXObject("Microsoft.XMLHTTP");
	   }
	   
		var url = 'ajax_sub_menu_check.php?';
		if(title != '') {
			var id	=	document.getElementById('sub_sub_menu_id').value;
			var fullurl = url + 'action=sub_sub_menu&title=' + trim(title)+"&id="+id;
			http.open("GET", fullurl, true);
			http.send(null);
			http.onreadystatechange = sub_sub_menu_check;
		}
	}
	
	function sub_sub_menu_check() {
		if (http.readyState == 4) {
			var strResult	=	http.responseText;
			var responseResult  =	strResult.split("loginresult:");
			if(responseResult[1] == '1') { 
				
				document.getElementById('txtSubSubMenu').value = '';
				//document.getElementById('txtSubRegionTitle').value = '';
				
				document.getElementById('title_right').style.display = 'none';				
				document.getElementById('sub_sub_menu_already_exist_error').style.display = 'inline';
				document.getElementById('sub_sub_menu_already_exist_error').innerHTML = '<img src="images/wrong.gif" border=0>Sub Menu (Level 2) already Exists';

			} else { 
				document.getElementById('sub_sub_menu_already_exist_error').style.display = 'none';				
			}
		}
	} 
