/*****************************************************************************/

function addtocart(prodid, catid)
{
	var msg="";
	//msg=confirm("This product will be added \n to your shopping cart.\nContinue?");
	//if(msg)
		//{
			window.location.href="addtocart.php?prodid="+prodid+"&catid="+catid;
		//}
}

/*****************************************************************************/

function viewdetails(prodid, catid, lid)
{
	window.location.href="viewselectedproduct.php?id="+prodid+"&catid="+catid+"&left_id="+lid;
}

/*****************************************************************************/
function deleteprod(prodid)
{
	window.location.href="deleteproduct.php?prodid="+prodid;
}
/*****************************************************************************/

function login()
{
	var msg="";

  if(document.loginfrm.username.value.length==0)
	msg+="Enter Username!\n";
  if(document.loginfrm.password.value.length==0)
	msg+="Enter Password!\n";

	if(msg.length>0)
		alert(msg)
	else			
		document.loginfrm.submit();

}

/***************************************************************************/
function registration_a()
{
var msg="";
if(document.registerfrm.username.value.length==0)
	msg+="Enter Username\n";
if(document.registerfrm.password.value.length==0)
{
	msg+="Enter Password\n";
}
else
{
	if(document.registerfrm.password.value != document.registerfrm.cpassword.value)
	msg+="Passwords does not match\n";
}
if(document.registerfrm.firstname.value.length==0)
	msg+="Enter First Name\n";
if(document.registerfrm.lastname.value.length==0)
	msg+="Enter Last Name\n";
if(document.registerfrm.email.value.length==0)
	msg+="Enter Email\n";
if(msg.length>0)
	alert(msg)
else
	document.registerfrm.submit();		
}


function registration()
{
	// for registration
	var msg;
	msg ="";

	for(i=0;i<document.regfrm.elements.length;i++)
		{
			if(document.regfrm.elements[i].value.length==0)
			 {
			 	msg="Enter all Fields!";
				break;	
			 }		
		}	
	if(msg.length > 0)
	{
		alert(msg)
	}
	else
	{
	//chk for password fields validity
	if(document.regfrm.password.value != document.regfrm.cpassword.value)
		alert("Passwords does not match");
	else		
		document.regfrm.submit()
	}	
}

/***************************************************************************/


//	TO SELECT FROM SELECTION BOX
function selectmenu(obj,val)
{

  var len=obj.options.length

  for(var i=0;i<len;i++)
  {
    if(obj.options[i].value==val)
     {
       obj.selectedIndex=i;
     }
  } 
}

/***************************************************************************/

function forgotpassword()
{
  if(document.form1.username.value=="" || document.form1.temailaddress.value=="")
  {
    alert("Enter both Username and Email address");
  }
	else
		document.form1.submit();
}

 /**************************EMAIL CHECKING***************************/
 
 function checkmail(fldname)
  {  

	//for all email fields in any forms - BOTH FOR ADMIN/USERS
  
	email = fldname.value;
	var len = email.length;              
	if(len<5)
		{
			alert("Invalid Email - id")
            fldname.value="";
		}
	else
		{
          
	var index1 = email.indexOf('@');
	var subind = email.substring(0,index1);
	var indlen = subind.length;
	var index2 = email.indexOf('.');
	var subind1=email.substring(indlen+1,index2);
	var indlen1 = subind1.length;
	var index3 = email.indexOf(' ');
	var subind3=email.substring(index2+1,len)
	var indlen2 = subind3.length;
	if((index1!= -1)&&(index2!= -1)&&(index3==-1))
	{	
		if((indlen!=0)&&(indlen1!=0)&&(indlen2!=0))
		{
//   			alert("valid Email -id");		  	 	
		} 
        else
		{
			alert("Invalid Email - id")
            fldname.value="";
		}
	}
	else
	{
   		alert("Invalid Email - id");
        fldname.value="";
	}		
	} // end of else if len<5
  }	

/***************************************************************************/
function numcheck(obj)
  {
    var msg = ""
    if(isNaN(obj.value)|| (obj.value.indexOf(' ')!=-1))
	{
	  msg = "Enter Integer Quantities !\n"
          obj.value = ""          
    }

/*
    if (obj.value.length <= 0)
	{
	  obj.value = 1
	}
*/        
	
    if(msg.length > 0)
		alert(msg)

  }
/*****************************************************************************/
function popupwindow(obj)
{
	window.open(obj, '', "scrollbar=no, menu=no, status=no, resizable=no, width=350, height=250");
}
/***************************************************************************/


/***********************************************************************************/

function addcat()
{

	var message="";
	if(document.addcategoryfrm.catname.value=="")
		message=message + "Enter Category Name!\n"

	if(message.length >0)
		alert(message)
	else
		document.addcategoryfrm.submit();
}

function addman()
{

	var message="";
	if(document.addcategoryfrm.supplier_name.value=="")
		message=message + "Enter Supplier Name!\n"

	if(message.length >0)
		alert(message)
	else
		document.addcategoryfrm.submit();
}


/***********************************************************************************/

function addsubcat()
{

	var message="";
	if(document.addcategoryfrm.subcatname.value=="")
		message=message + "Enter Subcategory Name!\n"

	if(message.length >0)
		alert(message)
	else
		document.addcategoryfrm.submit();
}



 
/***********************************************************************************/
function addproduct()
{
	var msg="";
/*
	if(document.addproductfrm.category.value==-1)
			msg+="Choose Category!\n";
*/

	for(i=0;i<document.addproductfrm.elements.length;i++)
	{
		if(document.addproductfrm.elements[i].name=="category[]")
			{
				if(document.addproductfrm.elements[i].value==-1 || document.addproductfrm.elements[i].value.length==0)
				 {	
					msg+="Choose a Product category!\n";	
					break;
				 }
			}			
	}
	
	if(document.addproductfrm.name.value.length==0)
			msg+="Enter Product Name!\n";

	if(document.addproductfrm.description.value.length==0)
			msg+="Enter Product description!\n";

	if(document.addproductfrm.price.value.length==0)
			msg+="Enter Product Price!\n";

	if(msg.length > 0)
		alert(msg)
	else
		document.addproductfrm.submit();						
}
/***********************************************************************************/

/*************************Manual Scripts**********************************/

function manual_sales()
{
var msg="";
	if(document.manual.fname.value.length==0)
			msg+="Enter First Name!\n";

	if(document.manual.lname.value.length==0)
			msg+="Enter Last Name!\n";
/*
	if(document.manual.email.value.length==0)
			msg+="Enter Email Address!\n";
*/
	if(document.manual.amount.value.length==0)
			msg+="Enter Total Amount!\n";

	if(msg.length > 0)
		alert(msg)
	else
		document.manual.submit();						
}

/*****************************Discount Validations**************************************/

function discount_settings()
{
var msg="";
	if(document.discount.disc_code.value.length==0)
			msg+="Enter Discount Code!\n";

	if(document.discount.disc_percent.value.length==0)
			msg+="Enter Discount Percentage!\n";

	if(document.discount.date.value==0)
			msg+="Enter Date!\n";

	if(document.discount.month.value==0)
			msg+="Enter Month!\n";

	if(document.discount.year.value==0)
			msg+="Enter Year!\n";

	if(msg.length > 0)
		alert(msg)
	else
		document.discount.submit();						
}

function payment()
{
	// for registration
	var msg;
	msg ="";

	for(i=0;i<document.regfrm.elements.length;i++)
		{
			if(document.regfrm.elements[i].value.length==0)
				if(document.regfrm.elements[i]!=document.regfrm.street2&&document.regfrm.elements[i]!=document.regfrm.b_address2)
				{
				msg="Please Enter all Fields.";
				break;	
				}		
		}
	


	if(msg.length > 0)
	{
		alert(msg)
	}
	else
	{
	//chk for password fields validity
		document.regfrm.submit()
	}	
}

function quantity_check(obj)
  {
    var msg = ""

	numcheck(obj);
	
	if(obj.value < 12)
	{
	msg+="Minimum order quantity is 12 !\n"
	}
	
	if((obj.value%6) != 0)
	{
		msg += "Additional orders can be made in increments of 6 bags !\n"
		
	}

/*
    if (obj.value.length <= 0)
	{
	  obj.value = 1
	}
*/        
	
    if(msg.length > 0)
		alert(msg)

  }

function popupwindow_prod(obj,ht_width)
{
	var param="";
	param="scrollbar=yes, menu=yes, status=yes, resizable=yes,"+ht_width;
	window.open(obj, '', param);
}

function deleteprod_wholesale(prodid)
{
	window.location.href="deleteproduct_wholesale.php?prodid="+prodid;
}
