// JavaScript Document
//////////////////////// TRIM ////////////////////////////
function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}

///////////////////// FOR EMAIL ///////////////////////////
function check_email1(email)
{
	var str = email.value;
	var re = /^([a-zA-Z0-9-_\.]+@([a-zA-Z0-9-_]+\.)+[a-zA-Z]{2,4})$/;
	if (!re.test(str))
	{
		alert("" + str + " is not a valid email address");
		email.focus();
		return false;
	}
}
////////////////// CHECK ALL FIELDS ///////////////////////

function validate_empty(j,frm)
{
	var invalid=" ";
	for (i=0;i<j;i++)
	{
		box = frm.elements[i];
		ename=box.name;
		ename=ename.replace("_"," ");
		e_name=upper_case_first_letters(ename);
		if(box.value=="")
		{
			alert(e_name + ' can not be empty.');
			box.focus();
			return false;
		}
	}
	return true;
}

function upper_case_first_letters(str)
{
		STRING=str;	
		var strReturn_Value = "";
		var iTemp = STRING.length;
		if(iTemp==0){
		return"";
		}
		var UcaseNext = false;
		strReturn_Value += STRING.charAt(0).toUpperCase();
		for(var iCounter=1;iCounter < iTemp;iCounter++){
		if(UcaseNext == true){
		strReturn_Value += STRING.charAt(iCounter).toUpperCase();
		}
		else{
		strReturn_Value += STRING.charAt(iCounter).toLowerCase();
		}
		var iChar = STRING.charCodeAt(iCounter);
		if(iChar == 32 || iChar == 45 || iChar == 46){
		UcaseNext = true;
		}
		else{
		UcaseNext = false
		}
		if(iChar == 99 || iChar == 67){
		if(STRING.charCodeAt(iCounter-1)==77 || STRING.charCodeAt(iCounter-1)==109){
		UcaseNext = true;
		}
		}
		
		
		} //End For
		
		return strReturn_Value;
		//	alert(strReturn_Value);
		
} //End Function

function check_number(frm,str)	
{
	//alert(str);
	var val=str.split(",");
	var type1, rdo, flag, e_name1;
	for(i=0;i<val.length;i++)
	{
		box=frm.elements[val[i]]
		ename=box.name;
		type1=box.type;
		ename=ename.replace("_"," ");
		e_name=upper_case_first_letters(ename);
		if(isNaN(box.value))
			{
				alert(e_name + ' is not a number');
				box.focus();
				return false;
			}
	}
	return true;
}
////////////////// CHECK EMAIL ADDRESS BY NAREN //////////////////
function check_email(fieldname)
{
	
	//alert(fieldname.value);
	if (fieldname.value.length >0 || fieldname.value.length=='') 
	{
		 i=fieldname.value.indexOf("@")
		 j=fieldname.value.indexOf(".",i)
		 k=fieldname.value.indexOf(",")
		 kk=fieldname.value.indexOf(" ")
		 jj=fieldname.value.lastIndexOf(".")+1
		 len=fieldname.value.length
	
		if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
			return true;
		}
		else {
			alert("Please enter an exact email address.\n" +
			fieldname.value + " is invalid.");
			fieldname.focus();
			
			return false;
		}

 	}

}
//////////////////////// IMAGE SET IN A POSITION OF BLANK POSITION/////////////////////
/*
document.getElementById("img1").src=document.recipe.file1.value;
html tag-----
<img src="templates/images/spacer.gif" width="43" height="40" id="img1">

*/
//=============================  CHECK HTML FILE =============
	function checkHTML(current)
	{
		if(current!="")
		{
			myHtml = new Image();
			myHtml.src = current;
			var filetype = new String(current);
			index = filetype.lastIndexOf(".");
			if (index == -1)
			{
				alert("Only HTML file format is allowed");
				return false;
			}
			filetype = filetype.substr(filetype.lastIndexOf(".") + 1);
			filetype = filetype.toUpperCase();
			if(filetype != "HTML")
			{
				alert("Only HTML file format is allowed");
				return false;
			}
		}
		return true;
	}

// ===========================IMAGER SIZE =========================================
	function checkImage(current)
	{
		if(current!="")
		{
			myImage = new Image();
			myImage.src = current;
			var filetype = new String(current);
			index = filetype.lastIndexOf(".");
			if (index == -1)
			{
				alert("Only gif,jpg,jpeg and png file formats are allowed");
				return false;
			}
			filetype = filetype.substr(filetype.lastIndexOf(".") + 1);
			filetype = filetype.toUpperCase();
			if(!(filetype == "GIF" || filetype == "JPG" || filetype == "JPEG" || filetype == "PNG"))
			{
				alert("Only gif,jpg and png file formats are allowed");
				return false;
			}
			var width=myImage.width;
			var height=myImage.height;
			if(width>max_size || height>max_size)
			{
				alert("Image Size must be less than 600X600 pixel dimension!");
				return false;
			}					
		}
		return true;
	}
	function checkImage1(current,max_size)
	{
		if(current!="")
		{
			myImage = new Image();
			myImage.src = current;
			var filetype = new String(current);
			index = filetype.lastIndexOf(".");
			if (index == -1)
			{
				alert("Only gif,jpg,jpeg and png file formats are allowed");
				return false;
			}
			filetype = filetype.substr(filetype.lastIndexOf(".") + 1);
			filetype = filetype.toUpperCase();
			if(!(filetype == "GIF" || filetype == "JPG" || filetype == "JPEG" || filetype == "PNG"))
			{
				alert("Only gif,jpg and png file formats are allowed");
				return false;
			}
			var size=max_size+'X'+max_size;
			var width=myImage.width;
			var height=myImage.height;
			if(width>max_size || height>max_size)
			{
				alert("Image Size must be less than " + size + " pixel dimension!");
				return false;
			}					
		}
		return true;
	}

function validate_empty1(frm,str)
{
	var val=str.split(",");
	var type1, rdo, flag, e_name1;
	for(i=0;i<val.length;i++)
	{
		box=frm.elements[val[i]]
		ename=box.name;
		type1=box.type;
		ename=ename.replace("_"," ");
		e_name=upper_case_first_letters(ename);
		if(type1=="radio")		
		{
			rdo="y";
			e_name1=e_name;
			if(box.checked || flag==1)	{	flag=1;		}
			else{	flag=2;		}
		}
		else
		{
			if(rdo=="y")
			{
				if(flag==2)
				{	rdo="";
					flag=5;
					alert('Please Select '+e_name1);
					box1=frm.elements[val[i-1]]					
					box1.focus();
					return false;
				}
			}
			if(box.value=="" || box.value==0)
			{
				alert(e_name + ' can not be empty.');
				box.focus();
				return false;
			}
		}
	}
	return true;
}
	function checkImage2(current)
	{
		if(current!="")
		{
			myImage = new Image();
			myImage.src = current;
			var filetype = new String(current);
			index = filetype.lastIndexOf(".");
			if (index == -1)
			{
				alert("Only gif,jpg,jpeg and png file formats are allowed");
				return false;
			}
			filetype = filetype.substr(filetype.lastIndexOf(".") + 1);
			filetype = filetype.toUpperCase();
			if(!(filetype == "GIF" || filetype == "JPG" || filetype == "JPEG" || filetype == "PNG"))
			{
				alert("Only gif,jpg and png file formats are allowed");
				return false;
			}
			var width=myImage.width;
			var height=myImage.height;
			if(width>148 || height>204)
			{
				alert("Image Size must be less than 148X204 pixel dimension!");
				return false;
			}					
		}
		return true;
	}
	
  function moveprod(current,id)
  {
		var flag=0;
		cat=current;
		index=current.selectedIndex;
		cat1=cat.split("~");
		if(document.getElementById(id).options.length>0)
		{
			for(i=0;i<document.getElementById(id).options.length;i++)
			{
			if(document.getElementById(id).options[i].value==cat1[0])
			{flag=1;}
			}
		}
		if(flag==0)
		{
				document.getElementById(id).options.add(new Option(cat1[1],cat1[0]));		
		}
		else
		{
			alert('This category already selected');
		}
}		
	function removeprod(current,id)
	{
		
		cat=current.selectedIndex;
		if(cat>-1)
		document.getElementById(id).options[cat]=null;
	}
	
	function fsubmit()
	{
		
/////////////////////////////// selected feature//////////////////
		var prod="";
		var industry=""
		var category=""		
	var len=document.getElementById('selected_feature').options.length;
		
		for(i=0;i<len;i++)
		{
			if(prod)
				{
				cat=document.getElementById('selected_feature').options[i].value;
				prod=prod+","+cat; 
				}
			else
				{
				cat=document.getElementById('selected_feature').options[i].value;
				prod=cat;
				}
		}
		document.getElementById('feature_value').value=prod;		 	 
		
/////////////////////////////// selected industry//////////////////

		var len=document.getElementById('selected_industry').options.length;
		for(i=0;i<len;i++)
		{
			if(industry)
				{
				cat=document.getElementById('selected_industry').options[i].value;
				industry=industry+","+cat; 
				}
			else
				{
				cat=document.getElementById('selected_industry').options[i].value;
				industry=cat;
				}
		}
		document.getElementById('industry_value').value=industry;		 		
////////////////////////////// selected category ///////////////////

}

		//===========================Admin Validation===========================//
function admin_validate(frm)
{
 //alert(pwd);
	str="0,1,2";
	if(validate_empty1(frm,str)==false)
	{
		return false;
	}
	if(document.getElementById('new_password').value!= document.getElementById('confirm_password').value)	
	{
		alert('New password and confirm passwword must be same !' );
		document.getElementById('confirm_password').focus();
		return false;
	}
}

function validate_add(frm)  
{ 
	//alert(frm); 
	var str="0,1,2,3,4,5,6,7,8,9,10";
	if(validate_empty1(frm,str)==false)  	
	{  		
		return false;  	
	}  	
	
	var str2="4,7,8,9";  	
	if(check_number(frm,str2)==false)  	
	{  		
		return false;  	
	}  
}
	

function validate_acc(frm)  
{ 
	//alert(frm); 
	var str="0,1";
	if(validate_empty1(frm,str)==false)  	
	{  		
		return false;  	
	}
	var fieldname=frm.email;
	if(check_email(frm.email)==false)  	
	{  		
		return false;  	
	} 
	
}

function validate_search(frm)  
{ 
	//alert(frm);
	if (document.search_pr.search_p.value=="")
	{
		alert("Please Enter Text.");
		document.search_pr.search_p.focus();
		return false;
	}
			
}

/*function validate_sc(frm, ca_sz, ca_co)  
{ 
	//alert(frm);
	var frm=frm, ca_sz, ca_co;
	if(ca_sz=='Y'){
		if (frm.size.value=="")
		{
			alert("Please Select Size.");
			frm.size.focus();
			return false;
		}
	}
	if(ca_co=='Y'){
		if (frm.color.value=="")
		{
			alert("Please Select Color.");
			frm.color.focus();
			return false;
		}
	}
	
	if(parseInt(frm.quantity.value) <= 0)
	{
		alert("Please Enter quantity greater than zero.");
		frm.quantity.focus();
		return false;
	}
	else if(parseInt(frm.quantity.value) > parseInt(frm.av_stock.value))
	{
		alert("Please Enter quantity less than or equal to available stock.");
		frm.quantity.focus();
		return false;
	}
	
	
}*/

function validate_enq(frm)  
{ 
	//alert(frm); 
	var str="0,2,4,8";
	if(validate_empty1(frm,str)==false)  	
	{  		
		return false;  	
	}
	var fieldname=frm.email;
	if(check_email(frm.email)==false)  	
	{  		
		return false;  	
	} 
	/*var str2="9,10,11,12";  	
	if(check_number(frm,str2)==false)  	
	{  		
		return false;  	
	}*/  
	
}
//******************** Reffer Friend Validation *********************//
function validate_reffer(frm)  
{ 
	//alert(frm); 
	var str="0";
	if(validate_empty1(frm,str)==false)  	
	{  		
		return false;  	
	}
	var fieldname=frm.email;
	if(check_email(frm.email)==false)  	
	{  		
		return false;  	
	} 
}

//validate_subscribe
function validate_subscribe(frm)  
{ 
	//alert(frm); 
	var str="0";
	if(validate_empty1(frm,str)==false)  	
	{  		
		return false;  	
	}
	var fieldname=frm.email;
	if(check_email(frm.email)==false)  	
	{  		
		return false;  	
	} 
}
// validation of post comments.
function comment_validation(frm)  
{ 
	//alert(frm); 
	var str="0,1,2";
	if(validate_empty1(frm,str)==false)  	
	{  		
		return false;  	
	}
	var fieldname=frm.email;
	if(check_email(frm.email)==false)  	
	{  		
		return false;  	
	} 
}

//************* Validation for Quantity field in enquiry form

/*function check_qty()
{
if (isNaN(frm_addcart.qty.value)) 
		{
		alert("Please Fill Numeric Value!");
		frm_addcart.qty.focus(); 
		return false;
		}	
}*/