//--------------------------------------------------------------------------------
// Function to validate 'Search Products' Form
//--------------------------------------------------------------------------------
function validatesearchform()
{
	if (document.searchform.keywords.value == "" || document.searchform.keywords.value == "Search Products")
	{
		alert('Please fill in Keywords to be searched');
		document.searchform.keywords.focus();
		return false;
	}
}


//--------------------------------------------------------------------------
//Function to postion div of search by keywords and enquiry list link
//-------------------------------------------------------------------------
function layer10divpos()
{
// variables to get screen height and width of window 

var winWidth=screen.width;
var winHeight=screen.height;

// Window width minus resolution of the screen the design is designed for
// Divide by 2 to get outer space between screen border and the main table
// Add width from outer table border to start of element. 
 
 
var left = (winWidth-1024)/2+41;
var top = 38;


document.getElementById("layer10").style.left=left+'px';
document.getElementById("layer10").style.top=top+'px';
document.getElementById("layer10").style["display"]="block" ;
document.getElementById("layer10").style["visibility"]="visible" ;
}

//--------------------------------------------------------------------------------
// Function to validate inner 'Search Products' Form 
//--------------------------------------------------------------------------------
function validateadsearchform()
{
	if (document.adsearchform.keywords.value == "" || document.adsearchform.keywords.value == "Search Products")
	{
		alert('Please fill in Keywords to be searched');
		document.adsearchform.keywords.focus();
		return false;
	}
	else
	{
		document.adsearchform.submit();
	}
}

//--------------------------------------------------------------------------------
// Function to validate 'Validate Feedback' Form
//--------------------------------------------------------------------------------
function validatefeedbackform()
{
	var msg = "";
	
	if (document.feedbackform.name.value == "") { msg = msg+"Name\n"; }
	if (document.feedbackform.company.value == "") { msg = msg+"Company\n"; }
	if (document.feedbackform.address.value == "") { msg = msg+"Address\n"; }
	if (document.feedbackform.city.value == "") { msg = msg+"City\n"; }
	if (document.feedbackform.state.value == "") { msg = msg+"State\n"; }
	if (document.feedbackform.country.value == "") { msg = msg+"Country\n"; }
	if (document.feedbackform.pincode.value == "") { msg = msg+"Pincode\n"; }
	if (document.feedbackform.email.value == "") { msg = msg+"Email\n"; }
	else
	{
		var emailReegxp = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/;
		if (!emailReegxp.test(document.feedbackform.email.value))
		{
			msg = msg+"Invalid Email Address\n";
		}
		
	}	
	if (document.feedbackform.phone.value == "") { msg = msg+"Telephone No.\n"; }
	if (document.feedbackform.mobile.value == "") { msg = msg+"Mobile\n"; }
	if (document.feedbackform.msg.value == "") { msg = msg+"Comments\n"; }
	if (msg != "") { alert("Please fill in following Details\n\n"+msg); return false;}
	 
}

//--------------------------------------------------------------------------------
// Function to validate form for adding product in enquiry list
//--------------------------------------------------------------------------------

function validateaddenqform()
{
	var digits = "0123456789";
	var error = "";
	if (document.addenqform.quantity.value == "")
	{
    	error += "Please fill in Quantity.\n";
	}
	for (var i = 0; i < document.addenqform.quantity.value.length; i++)
	{
	    temp = document.addenqform.quantity.value.substring(i, i+1)
    	if (digits.indexOf(temp) == -1 && document.addenqform.quantity.value != "")
		{
			error += "Quantity must be a number.\n";
		     break;
	    }
	}
	if (error != "")
	{
		alert(error);
		return false;
	}
	else { return true;}
}

//--------------------------------------------------------------------------------
// Function to redirect on clicking on main categories on homepage
//--------------------------------------------------------------------------------
function setdoccookie(menucode,filename)
{
	document.cookie = 'sdmenu_my_menu='+menucode+'; expires=Thu, 2 Aug 20011 20:47:11 UTC; path=/';
	window.location = filename;

}

var xmlHttp;
var globali
//--------------------------------------------------------------------------------
// AJAX for 'add to Enquiry List'
//--------------------------------------------------------------------------------
function save_to_enqlist(ctr1,totalctr2)
{
	globali=ctr1;
	var j="";
	var quantity_ctr = "";
	var avail_str = "";

	quantity_ctr = document.addtoenqlistform['quantity'+ctr1].value;
	for(j=0;j<=totalctr2-1;j++)
	{	
		if (document.addtoenqlistform['avail'+ctr1+'_'+j].checked == true)
		{
			avail_str += document.addtoenqlistform['avail'+ctr1+'_'+j].value;
			var test = parseInt(j)+1;
			
			if (document.addtoenqlistform['avail'+ctr1+'_'+test])
			{
				if (document.addtoenqlistform['avail'+ctr1+'_'+test].checked == true)
				{
					avail_str += ","
				}
			}
		}
	}
	prodid = document.addtoenqlistform['prod_id'+ctr1].value;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request");
		 return;
	 }
	var url="addtoenqlist.php";
	url += "?avail="+avail_str;
	url += "&quantity="+quantity_ctr;
	url += "&prod_id="+prodid;
//	alert(url);
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("txtHint"+globali).innerHTML=xmlHttp.responseText;
	} 
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	 {
	// Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {	
	 //Internet Explorer
		try
		 {
			  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	 	}
		catch (e)
		 {
			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	 }
	}
	return xmlHttp;
}


//--------------------------------------------------------------------------------------
// Function to take confirmation to delete an item from Enquiry List
//--------------------------------------------------------------------------------------
function confirmdelete(srno)
{
	var answer = confirm("Are tou sure you want to delete this item from Enquiry List?")
	if (answer){
		window.location = "enqlist.php?action=delete_item&sr_no="+srno;
	}
}

//--------------------------------------------------------------------------------------
// Function Redirect to 'Buyer Details' form
//--------------------------------------------------------------------------------------
function redirect(url)
{
	window.location = url;
}

//--------------------------------------------------------------------------------------
// Function to validate finalform (enquirylist form)
//--------------------------------------------------------------------------------------
function validatefinalform(ctr)
{
	for	(var i=0;i<=ctr-1;i++)
	{
				
	}
	document.finalform.submit();
}



//--------------------------------------------------------------------------------------
// Function Validate to 'Buyer Details' form
//--------------------------------------------------------------------------------------
function chkbuyerdetailsform()
{
	var msg = "";

	if (document.buyerdetailsform.bill_name1.value == "") { msg = msg + "Name\n";}
	if (document.buyerdetailsform.bill_name2.value == "") { msg = msg + "Name\n";}
	if (document.buyerdetailsform.bill_telephone.value == "") { msg = msg + "Phone\n";}
	if (document.buyerdetailsform.bill_mobile.value == "") { msg = msg + "Mobile\n";}
	if (document.buyerdetailsform.bill_email.value == "") { msg = msg + "Email\n";}
	else 
	{
		var houseReegxp = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/;
		if (houseReegxp.test(document.buyerdetailsform.bill_email.value) == false)
		{
			msg = msg + "Invald Email Address\n";
		}
	}
	if (document.buyerdetailsform.bill_address.value == "") { msg = msg + "Customer Address\n";}
	if (document.buyerdetailsform.bill_city.value == "") { msg = msg + "City\n";}
	if (document.buyerdetailsform.bill_state.value == "") { msg = msg + "State\n";}
	if (document.buyerdetailsform.bill_country.value == "") { msg = msg + "Country\n";}
	if (document.buyerdetailsform.bill_pin.value == "") { msg = msg + "Pin\n";}

	if (msg != "") { alert("Please fill following fields \n\n" + msg ); return false;	}
	
}


//--------------------------------------------------------------------------------------
// Function Add text field dynamically in 'ordering info' form
//--------------------------------------------------------------------------------------

var counter_add_txt = 0;
var limit_add_txt = 1;

function add_txt(divName,dropdown,label,cnt)
{
	var ctr = dropdown.length;
	var newdivname = label+'_div';
	if (dropdown.selectedIndex == (ctr-1))
	{
		if (document.getElementById(newdivname))
		{
			counter_add_txt = "1";
		}
		else
		{
			counter_add_txt = "0";
		}

		if (counter_add_txt != limit_add_txt)
		{
			
			var newdiv = document.createElement('div');
			newdiv.setAttribute('id',newdivname); 
	        newdiv.innerHTML = "Other, Please Specify<br><input type='text' class='innerbody' name='"+label+"_others'>";
	        document.getElementById(divName).appendChild(newdiv);
	        counter_add_txt++;
		}
	}
	else 
	{
		var d = document.getElementById(divName);
		var olddiv = document.getElementById(newdivname);
		if (olddiv)	
		{
			d.removeChild(olddiv);
	        counter_add_txt=0;
		}
	}
}

//--------------------------------------------------------------------------------------
// Function Validate Ordering Info form
//--------------------------------------------------------------------------------------

function validateorderinfoform()
{
	var msg = "";
	
	if (document.orderinfoform.cust_name.value == "") { msg = msg+"Name\n"; }
	if (document.orderinfoform.comp_name.value == "") { msg = msg+"Company\n"; }
	if (document.orderinfoform.add.value == "") { msg = msg+"Address\n"; }
	if (document.orderinfoform.city.value == "") { msg = msg+"City\n"; }
	if (document.orderinfoform.state.value == "") { msg = msg+"State\n"; }
	if (document.orderinfoform.country.value == "") { msg = msg+"Country\n"; }
	if (document.orderinfoform.office_contact.value == "") { msg = msg+"Office Contact no.\n"; }
	if (document.orderinfoform.alternate_contact.value == "") { msg = msg+"Alternate Contact no.\n"; }
	if (document.orderinfoform.mobile_contact.value == "") { msg = msg+"Mobile\n"; }
	if (document.orderinfoform.fax.value == "") { msg = msg+"Fax\n"; }
	if (document.orderinfoform.email.value == "") { msg = msg+"Email\n"; }
	else
	{
		var emailReegxp = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/;
		if (!emailReegxp.test(document.orderinfoform.email.value))
		{
			msg = msg+"Invalid Email Address\n";
		}
		
	}	
	if (document.orderinfoform.ref.value == "") { msg = msg+"Reference\n"; }
	if (msg != "") { alert("Please fill in following Details\n\n"+msg); return false;}
	 
}

//--------------------------------------------------------------------------------------
// Function Validate Ordering Info form
//--------------------------------------------------------------------------------------

function validateorderinfoform1()
{
	var msg = "";
	
	if (document.orderinfoform1.cust_name.value == "") { msg = msg+"Name\n"; }
	if (document.orderinfoform1.comp_name.value == "") { msg = msg+"Company\n"; }
	if (document.orderinfoform1.add.value == "") { msg = msg+"Address\n"; }
	if (document.orderinfoform1.city.value == "") { msg = msg+"City\n"; }
	if (document.orderinfoform1.state.value == "") { msg = msg+"State\n"; }
	if (document.orderinfoform1.country.value == "") { msg = msg+"Country\n"; }
	if (document.orderinfoform1.office_contact.value == "") { msg = msg+"Office Contact no.\n"; }
	if (document.orderinfoform1.alternate_contact.value == "") { msg = msg+"Alternate Contact no.\n"; }
	if (document.orderinfoform1.mobile_contact.value == "") { msg = msg+"Mobile\n"; }
	if (document.orderinfoform1.fax.value == "") { msg = msg+"Fax\n"; }
	if (document.orderinfoform1.email.value == "") { msg = msg+"Email\n"; }
	else
	{
		var emailReegxp = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/;
		if (!emailReegxp.test(document.orderinfoform1.email.value))
		{
			msg = msg+"Invalid Email Address\n";
		}
		
	}	
	if (document.orderinfoform1.ref.value == "") { msg = msg+"Reference\n"; }
	if (msg != "") { alert("Please fill in following Details\n\n"+msg); return false;}
	 
}

		
