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;
}


function echeck(str)
{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
 		 return true
}

function checkAllRecords(a)
{
	if(trimAll(a.cardname.value)=='')
	{
		alert('Please enter a card name!');
		a.cardname.focus();
		a.cardname.value='';
		return false;
	}
	if(a.categories.value=='')
	{
		alert('Please enter a category!');
		a.categories.focus();
		a.categories.value='';
		return false;
	}
}

function delEcardDetail(imageid)
{
	if(confirm('Do you want to delete this record?'))
	{
		window.location="addEcard.php?hmode=D&imageid="+imageid;
	}
}
function delCategory(catid)
{
	if(confirm('Do you want to delete this record?'))
	{
		window.location="addCategory.php?hmode=D&catid="+catid;
	}
}

function showSelectedRecord(a)
{
	window.location="addEcard.php?recShow1="+a.value;
}

function showSelectedRecordUser(a,catSearch)
{
	window.location="ecardDetailInfo.php?recShow1="+a.value+"&catSearch="+catSearch;
}
function checkAllFields(a)
{
	if (echeck(a.from_email.value)==false)
	{
		a.from_email.value="";
		a.from_email.focus();
		return false;
	}
}

function chackAllCategory(a)
{
	if(trimAll(a.category.value)=='')
	{
		alert('Please enter a category!');
		a.category.focus();
		a.category.value='';
		return false;
	}
}
function showSelectedRecordCat(a)
{
	window.location="addCategory.php?recShow1="+a.value;
}

function showSelectedRecordUserProfile(a,usernameid,actionName)
{
	window.location="usersProfile.php?recShow1="+a.value+"&usernameid="+usernameid+"&actionName="+actionName;
}

function delUser(id)
{
	if(confirm('Do you want to delete this record?'))
	{
		window.location="usersProfile.php?hmode=D&id="+id;
	}
}

function checkUserName(a)
{
	if(trimAll(a.DisplayName.value)=='')
	{
		alert('Please eneter Display Name!');
		a.DisplayName.value='';
		a.DisplayName.focus();
		return false;
	}
}

function showSelectedRecordPetProfile(a,actionName,petnameval,SpeciesName,statusName)
{
	window.location="petsProfile.php?recShow1="+a.value+"&actionName="+actionName+"&petnameval="+petnameval+"&SpeciesName="+SpeciesName+"&statusName="+statusName;
}

function delPetProfile(pet_id)
{
	if(confirm('Do you want to delete this record?'))
	{
		window.location="petsProfile.php?hmode=D&pet_id="+pet_id;
	}
}

function CheckPetName(a)
{
	if(trimAll(a.PetName.value)=='')
	{
		alert('Please eneter Pet Name!');
		a.PetName.value='';
		a.PetName.focus();
		return false;
	}
}

function checkProductDetail(a)
{
	if(a.shcategoryname.value=='')
	{
		alert('Please Select Category Name!');
		a.shcategoryname.focus();
		return false;
	}
	if(trimAll(a.productname.value)=='')
	{
		alert('Please Enter Product Name!');
		a.productname.value="";
		a.productname.focus();
		return false;
	}
	if(trimAll(a.sellprice.value)=='')
	{
		alert('Please Enter selling price!');
		a.sellprice.value="";
		a.sellprice.focus();
		return false;
	}

	if(IsNumeric(a.sellprice.value) == false)
    {
    	alert("Please check - non numeric value!");
    	a.sellprice.focus();
		return false;
    }

	if(trimAll(a.qtyavailable.value)=='')
	{
		alert('Please Enter quantity available!');
		a.qtyavailable.value="";
		a.qtyavailable.focus();
		return false;
	}
	if(IsNumeric(a.qtyavailable.value) == false)
    {
    	alert("Please check - non numeric value!");
    	a.qtyavailable.focus();
		return false;
    }
}

function IsNumeric(strString)
   //  check for valid numeric strings
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function showSelectedPrRecord(a)
{
	window.location="addProducts.php?recShow1="+a.value;
}

function delProductDetail(product_id)
{
	if(confirm('Do you want to delete this record?'))
	{
		window.location="../admin/addProducts.php?hmode=D&product_id="+product_id;
	}
}

function checkCategoryDetail(a)
{
	if(trimAll(a.categoryname.value)=='')
	{
		alert('Please Enter Category Name!');
		a.categoryname.value="";
		a.categoryname.focus();
		return false;
	}
}

function showSelectedRecordValue(a,filename)
{
	window.location="manageProductCategory.php?recShow1="+a.value;
}

function delCategoryDetail(cat_id)
{
	if(confirm('Do you want to delete this record?'))
	{
		window.location="../admin/manageProductCategory.php?hmode=D&cat_id="+cat_id;
	}
}

function checkSubCatName(a)
{
	if(trimAll(a.subcategoryname.value)=='')
	{
		alert('Please Enter Sub Category Name!');
		a.subcategoryname.value="";
		a.subcategoryname.focus();
		return false;
	}
	a.actionName.value="add";
	a.submit();
}

function showSelectedSubRecordValue(a,filename,cat_id)
{
	window.location="manageProductCategory.php?recShow1="+a.value+"&cat_id="+cat_id+"&subcat=1";
}

function delSubCategoryDetail(cat_id)
{
	if(confirm('Do you want to delete this record?'))
	{
		window.location="../admin/manageProductCategory.php?subhmode=D&cat_id="+cat_id;
	}
}

function createRequestObject()
{
	var request_o;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_o = new XMLHttpRequest();
	}
	return request_o;
}


var http = createRequestObject();

function getSubCatgoryVal_onload(cat_id,subcat_id)
{
	http.open('get', '../admin/getRelatedSubCategory.php?&cat_id='+cat_id+'&subcat_id='+subcat_id);
	http.onreadystatechange = handleSubCategory;
	http.send(null);
}

function getSubCatgoryVal(a)
{
	http.open('get', '../admin/getRelatedSubCategory.php?&cat_id='+a.value);
	http.onreadystatechange = handleSubCategory;
	http.send(null);
}

function handleSubCategory()
{
		if(http.readyState == 4)
		{
			var response = http.responseText;
			document.getElementById('subcat_name_before').style.display="none";
			document.getElementById('subcat_name_after').style.display="block";
			document.getElementById('subcat_name_after').innerHTML = response;
		}
}