//email chk
function emailCheck(emailStr) {
          var sw = true;
          var checkTLD=0;
          var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
          var emailPat=/^(.+)@(.+)$/;
          var specialChars="\\(\\)>@,;:\\\\\\\"\\.\\[\\]";
          var validChars="\[^\\s" + specialChars + "\]";
          var quotedUser="(\"[^\"]*\")";
          var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
          var atom=validChars + '+';
          var word="(" + atom + "|" + quotedUser + ")";
          var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
          var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
          var matchArray=emailStr.match(emailPat);
          if (matchArray==null) { sw = false; return sw; }
          var user=matchArray[1];
          var domain=matchArray[2];
          for (i=0; i<user.length; i++) {
          if (user.charCodeAt(i)>127) sw = false;
          }
          for (i=0; i<domain.length; i++) {
          if (domain.charCodeAt(i)>127) sw = false;
          }
          if (user.match(userPat)==null) sw = false;
          var IPArray=domain.match(ipDomainPat);
          if (IPArray!=null) {
          for (var i=1;i<=4;i++) {
          if (IPArray[i]>255) sw = false;
          }
          return true;
          }
          var atomPat=new RegExp("^" + atom + "$");
          var domArr=domain.split(".");
          var len=domArr.length;
          for (i=0;i<len;i++)
          {
          if (domArr[i].search(atomPat)==-1) sw = false;
          }
          if (checkTLD && domArr[domArr.length-1].length!=2 &&
          domArr[domArr.length-1].search(knownDomsPat)==-1) sw = false;
          if (len<2) sw = false;
          return sw;
          }

function LTrim( value ) 
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim( value ) 
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim(value) 
{
	return LTrim(RTrim(value));
}

function focus_frm(col_name){
	
	document.getElementById(col_name).focus();
	}
//admin login
function admin_chk(frm){
	if(trim(frm.uname_admin.value)==""){
			alert("Please Enter User Name");
			frm.uname_admin.focus();
			return false;
			
	}
	if(frm.pass_admin.value==""){
			alert("Please Enter Password");
			frm.pass_admin.focus();
			return false;
			
	}
	return true;
}
//change_password
function change_password(frm){
	if(frm.old_pass.value==""){
			alert("Please Enter Old Password");
			frm.old_pass.focus();
			return false;
			
	}
	if(frm.new_pass.value==""){
			alert("Please Enter New Password");
			frm.new_pass.focus();
			return false;
			
	}
	return true;
}
//ADD Category
function category_valid(frm){
	if(trim(frm.cat_name.value)==""){
			alert("Please Enter Category Name");
			frm.cat_name.focus();
			return false;
			
	}
	return true;
}
function activateParent()
{
	if(document.add_cat.subcat.checked == true)	{
		document.add_cat.parent_id.disabled = false;
	}
	else	{
		document.add_cat.parent_id.disabled = true;
	}
}
function activate_category(id,status)	{
	
	//alert(id+' ,'+status)
	if(status=='yes')	{
		if(confirm("Do you want to deactivate this category")){
			
			location.href="view_category.php?mod=y&cat_id="+id+"&status=no";
			return true;
				 }
		
	}else if(status=='no')	{
		if(confirm("Do you want to activate this category")){
			location.href="view_category.php?mod=y&cat_id="+id+"&status=yes";
			return true;
		}
		
	}
}
function Confirm_del_cat(catid) // To ask for confirmation before deleting the record.

{

if(confirm("Do you really want to delete this record ? "))

  {

    location.href='view_category.php?deleteid='+catid; // If record delete move to this url.

  } 

}

function delete_catimg(img_name,col,editid)	{
	if(img_name!="")	{
		if(confirm("Are you sure you want to delete "+img_name))	{
			window.location.href="add_category.php?del_image_name="+img_name+"&col="+col+"&editid="+editid;
		}
	}
}

//*********
//ADD Products
function product_valid(frm){
	if(trim(frm.product_name.value)==""){
			alert("Please Enter Product Name");
			frm.product_name.focus();
			return false;
			
	}
	if(trim(frm.product_sku.value)==""){
			alert("Please Enter Product Code");
			frm.product_sku.focus();
			return false;
			
	}
	if(trim(frm.product_prc.value)==""){
			alert("Please Enter Product Price");
			frm.product_prc.focus();
			return false;
			
	}
	/*if(frm.c_discount.checked == true)	{
		if(!frm.coupen_id.value){
			alert("Please Select Coupen Value");
			frm.coupen_id.focus();
			return false;
		}
	}*/
	
	return true;
}

function activate_product(id,status,pageNum){
	
	//alert(id+' ,'+status)
	if(status=='yes')	{
		if(confirm("Do you want to deactivate this product")){
			location.href="view_product.php?mod=y&pro_id="+id+"&status=no&page="+pageNum;
			return true;
		}
		
	}else if(status=='no')	{
		if(confirm("Do you want to activate this product")){
			
			location.href="view_product.php?mod=y&pro_id="+id+"&status=yes&page="+pageNum;
			return true;
		}
		
	}
	
}
function Confirm_del_pro(catid) // To ask for confirmation before deleting the record.

{

if(confirm("Do you really want to delete this record ? "))

  {

    location.href='view_product.php?deleteid='+catid; // If record delete move to this url.

  } 

}

function Confirm_del_ordpnd(catid,st) // To ask for confirmation before deleting the record.

{

if(confirm("Do you really want to delete this record ? "))

  {

    location.href='ord_pnd.php?deleteid='+catid+'&status='+st; // If record delete move to this url.

  } 

}


function delete_img(img_name,col,editid)	{
	if(img_name!="")	{
		if(confirm("Are you sure you want to delete "+img_name))	{
			window.location.href="add_product.php?del_image_name="+img_name+"&col="+col+"&editid="+editid;
		}
	}
}
function delete_extra_img(img_name,img_id,col,editid,page)	{
	if(img_name!="")	{
		
		if(confirm("Are you sure you want to delete "+img_name))	{
			//alert(page);
			window.location.href="add_product.php?del_image_name="+img_name+"&img_id="+img_id+"&col="+col+"&editid="+editid+"&page="+page;
		}
	}
}

//pop_up
function pop_up(url,nam,w,h){
		window.open (url,nam,'location=0,status=0,resizable=0,left=0,top=0,scrollbars=0,width='+w+',height='+h);
}

//Add User

function user_valid(frm){
	
	var Numeric_Reg_Exp=/^[0-9]+$/;
	if(trim(frm.user_fname.value)==""){
			alert("Please Enter User Name");
			frm.user_fname.focus();
			return false;
			
	}
	if(trim(frm.user_lname.value)==""){
			alert("Please Enter Last Name");
			frm.user_lname.focus();
			return false;
			
	}
	if(trim(frm.user_address.value)==""){
			alert("Please Enter Address");
			frm.user_address.focus();
			return false;
			
	}
	if(trim(frm.user_city.value)==""){
			alert("Please Enter City Name");
			frm.user_city.focus();
			return false;
			
	}
	if(trim(frm.user_phone.value)==""){
			alert("Please Enter Phone Number");
			frm.user_phone.focus();
			return false;
	}
	if(frm.user_phone.value!=""){
		 if(!frm.user_phone.value.match(Numeric_Reg_Exp)){
			alert("Please Enter the Numeric value");
			frm.user_phone.focus();
			return false;	
		}
		if(frm.user_phone.value.length<10){
			alert("Enter Minimum 10 Numeric Character");
			frm.user_phone.focus();
			return false;	
		}
	}
	if(trim(frm.user_zip.value)==""){
			alert("Please Enter Post Code");
			frm.user_zip.focus();
			return false;
			 
	}
	if(trim(frm.country.value)==""){
			alert("Please Select country");
			frm.country.focus();
			return false;
			 
	}
	if(trim(frm.country.value)=="India") {
		if(trim(frm.states.value)==""){
			alert("Please Select state");
			frm.states.focus();
			return false;
			 
		}
	}
	
	if(trim(frm.user_email.value)==""){
			alert("Please Enter Email ID");
			frm.user_email.focus();
			return false;
			
	}
	if(!emailCheck(frm.user_email.value)){
			alert("Please Enter Valid Email ID");
			frm.user_email.focus();
			return false;
			
	}
	if(trim(frm.confirm_user_email.value)==""){
			alert("Please Enter Confirm Email ID");
			frm.confirm_user_email.focus();
			return false;
			
	}
	 else if(frm.confirm_user_email.value!=frm.user_email.value)
      {
       alert("Email ID doesn't match.");
      frm.confirm_user_email.focus();
       return(false);
      }
	//alert(frm.email1.value);
	if(frm.email1){
		if(frm.email1.value==1){
				alert("Email ID alreay exits");
				frm.user_email.focus();
				return false;
				
		}
	}
	if(trim(frm.user_passw.value)==""){
			alert("Please Enter Password");
			frm.user_passw.focus();
			return false;
			
	}
	var pass =frm.user_passw.value;
	var len = 0;
	var len = pass.length;
	if(len < 5) {
		alert("Please Enter minimume 5 character");
			frm.user_passw.focus();
		return false;
	}
	if(frm.codee){
		if(trim(frm.codee.value)==""){
				alert("Please fill Security Code");
				frm.codee.focus();
				return false;
				
		}
	
		if(frm.code.value!=frm.codee.value){
			
			alert("Please enter correct security code");
			return false;
		}
	}
	return true;
	
}

function Confirm_del_user(catid) // To ask for confirmation before deleting the record.

{

if(confirm("Do you really want to delete this record ? "))

  {

    location.href='view_user.php?deleteid='+catid; // If record delete move to this url.

  } 

}
//Country_valid

function Country_valid(frm){
	if(trim(frm.country_name.value)==""){
			alert("Please Enter Country Name");
			frm.country_name.focus();
			return false;
			
	}
	if(trim(frm.country_cod.value)==""){
			alert("Please Enter Country Code");
			frm.country_cod.focus();
			return false;
			
	}
	return true;
}
function Confirm_del_country(catid) // To ask for confirmation before deleting the record.

{

if(confirm("Do you really want to delete this record ? "))

  {

    location.href='view_country.php?deleteid='+catid; // If record delete move to this url.

  } 

}

//shipping_valid

function ship_valid(frm){
	if(trim(frm.country.value)=="" && document.add_ship.row.checked == false){
			alert("Please Select the Country or ROW");
			frm.country.focus();
			return false;
			
	}
	if(trim(frm.sh_service_name.value)==""){
			alert("Please Enter Service Name");
			frm.sh_service_name.focus();
			return false;
			
	}
	if(trim(frm.sh_from.value)==""){
			alert("Please Enter From Weight");
			frm.sh_from.focus();
			return false;
			
	}
	if(trim(frm.sh_to.value)==""){
			alert("Please Enter TO Weight");
			frm.sh_to.focus();
			return false;
			
	}
	if(trim(frm.sh_rate.value)==""){
			alert("Please Enter Rate");
			frm.sh_rate.focus();
			return false;
			
	}
	return true;
}
function Confirm_del_ship(catid,val) // To ask for confirmation before deleting the record.

{

if(confirm("Do you really want to delete this record ? "))

  {

    location.href='view_shipping.php?deleteid='+catid+'&contid='+val; // If record delete move to this url.

  } 

}
function activate_new_product(id,status,pageNum){
	
	//alert(id+' ,'+status)
	if(status=='1')	{
		if(confirm("Do you want to deactivate this product")){
			location.href="view_new_pro.php?mod=y&pro_id="+id+"&status=0&page="+pageNum;
			return true;
		}
		
	}else if(status=='0')	{
		if(confirm("Do you want to activate this product")){
			
			location.href="view_new_pro.php?mod=y&pro_id="+id+"&status=1&page="+pageNum;
			return true;
		}
		
	}
	
}
function AccessDenied() {
	alert('Access Denied You do not have permission to modify user groups!');
}

