// JavaScript Document
basepath = "";

function getHTTPObject()
{
	var xmlhttp;
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
  		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	 	if(!xmlhttp)
	 	{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
   	}
	return xmlhttp;
}
var http = getHTTPObject();


function chgdefimg(imgpath)
{
	document.getElementById('def_img').innerHTML = '<img src="'+imgpath+'" alt="Jewellery" width="526" height="416" />';
}

function getSubCatData(bpath, cattype, tid, pageno, sort1, sort2, sort3)
{
	basepath = bpath;
	var url = bpath + "php/sub_category.php?bpath="+bpath+"&ct="+cattype+"&td="+tid+"&page="+pageno+"&sort1="+sort1+"&sort2="+sort2+"&sort3="+sort3;
	http.open("GET",url,true);
	http.onreadystatechange = handleHttpResponse_gscd;
	http.send(null);
}
function handleHttpResponse_gscd()
{
	if(http.readyState < 4)
    {}
	if(http.readyState == 4) 
    {
    	if(http.status==200) 
       	{			
           	var results = http.responseText;			
           	document.getElementById('subcatdata').innerHTML = results; 
			return false;
		}
	}
}

function subcatpaging(bpath, cattype, tid, pageno, sort1, sort2, sort3)
{
	getSubCatData(bpath, cattype, tid, pageno, sort1, sort2, sort3);
}

function setsort(sortno, val)
{
	if(sortno == "sort1")
		document.getElementById('hid_sort1').value = val;
	else if(sortno == "sort2")
		document.getElementById('hid_sort2').value = val;
	else if(sortno == "sort3")
		document.getElementById('hid_sort3').value = val;
	
	bpath = document.getElementById('hid_bpath').value;
	cattype = document.getElementById('hid_cattype').value;
	tid = document.getElementById('hid_tid').value;
	pageno = document.getElementById('hid_pageno').value;
	sort1 = document.getElementById('hid_sort1').value;
	sort2 = document.getElementById('hid_sort2').value;
	sort3 = document.getElementById('hid_sort3').value;
	
	getSubCatData(bpath, cattype, tid, pageno, sort1, sort2, sort3);
}

function chkFrmAskExpt()
{
	txtemail = document.getElementById('txtemail');
	txtsub = document.getElementById('txtsub');
	taques = document.getElementById('taques');
	
	if(txtemail.value == "")
	{
		alert("Please enter your E-mail ID");
		txtemail.focus();
		return false;
	}
	else
	{
		if(!echeck(trim(txtemail.value)))
		{
			txtemail.focus();
			return false;
		}
	}
	if(txtsub.value == "")
	{
		alert("Please enter Subject.");
		txtsub.focus();
		return false;
	}
	if(taques.value == "")
	{
		alert("Please enter Your Question.");
		taques.focus();
		return false;
	}
}

function chkTellFrdFields(action, url, bpath, hosturl)
{	
	if(action == "send")
	{
		var fnemail = "";
		var txtto = document.getElementById("txtto");
		var txtfrom = document.getElementById("txtfrom");
		var chkmail = document.getElementById("chkmail").checked;
		var txtmsg = document.getElementById("txtmsg");
				
		if(txtto.value == "")
		{
			alert("To field cannot be left blank. Please specify the email(s).");
			txtto.focus();
			return false;
		}
		else
		{
			txtto2 = txtto.value;
			txtto2 = txtto2.split(",");
			for(var i in txtto2)
			{
				if(trim(txtto2[i]) != "")
				{
					if(echeck(trim(txtto2[i])))
					{
						fnemail += "," + trim(txtto2[i]);
					}
					else
					{
						txtto.focus();
						return false;
					}
				}
			}
			if(fnemail == "")
			{
				alert("Please specify the proper email(s).");
				txtto.focus();
				return false;
			}
			else
			{
				fnemail = fnemail.substring(1,fnemail.length);
			}
			txtto.value = fnemail;
		}
		if(txtfrom.value == "")
		{
			alert("From field cannot be left blank. Please specify your email.");
			txtfrom.focus();
			return false;
		}
		else
		{
			if(!echeck(trim(txtfrom.value)))
			{
				txtfrom.focus();
				return false;
			}
		}
		if(txtmsg.value == "")
		{
			alert("Message field cannot be left blank. Please write your message.");
			txtmsg.focus();
			return false;
		}
		
		sitebp = hosturl+bpath;		
		txtmsg2 = txtmsg.value.replace("\n", "<br>");		 
		var url = bpath + "php/tellafriend.php?txtto="+fnemail+"&txtfrom="+txtfrom.value+"&chkmail="+chkmail+"&txtmsg="+txtmsg2+"&url="+url+"&sitebp="+sitebp;		
		http.open("POST",url,true);
		http.onreadystatechange = handleHttpResponse_taf;
		http.send(null);	
	}
	else if(action == "reset")
	{
		document.getElementById("txtto").value = "";
		document.getElementById("txtfrom").value = "";
		document.getElementById("chkmail").checked = false;
		document.getElementById("txtmsg").value = "";		
	}
}

function handleHttpResponse_taf()
{
	if(http.readyState < 4)
    {}
	if(http.readyState == 4) 
    {
    	if(http.status==200) 
       	{			
           	var results = http.responseText;			
			var res = results.split("<><>");
			if(res[1] == 1)
			{				
				alert(res[0]);
				document.getElementById("txtto").value = "";
				document.getElementById("txtfrom").value = "";
				document.getElementById("chkmail").checked = false;
				document.getElementById("txtmsg").value = "";
				document.getElementById("tellafriend").style.display = "none";
			}
			return false;
		}
	}
}


// TRIM FUNCTION
function trim(stringToTrim)
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

// EMAIL VALIDATION FUNCTION
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 ValidateForm()
{
	var emailID=document.frmSample.txtEmail
	
	if((emailID.value==null)||(emailID.value==""))
	{
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if(echeck(emailID.value)==false)
	{
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
 
 
 
function closeTellAFrd()
{
	document.getElementById("txtto").value = "";
	document.getElementById("txtfrom").value = "";
	document.getElementById("chkmail").checked = false;
	document.getElementById("txtmsg").value = "";
	
	document.getElementById("tellafriend").style.display = "none";
}

function getSearchData(bpath, srhterm, pageno, sort1)
{	
	basepath = bpath;
	var url = bpath + "php/search.php?bpath="+bpath+"&srhterm="+srhterm+"&page="+pageno+"&sort1="+sort1;
	http.open("GET",url,true);
	http.onreadystatechange = handleHttpResponse_gsd;
	http.send(null);
}

function handleHttpResponse_gsd()
{
	if(http.readyState < 4)
    {}
	if(http.readyState == 4) 
    {
    	if(http.status==200) 
       	{			
           	var results = http.responseText;			
           	document.getElementById('searchdata').innerHTML = results;
			return false;
		}
	}
}

function setsrhsort(sortno, val)
{
	if(sortno == "sort1")
		document.getElementById('hid_sort1').value = val;	
	
	bpath = document.getElementById('hid_bpath').value;
	srhterm = document.getElementById('hid_srhterm').value;
	//pageno = document.getElementById('hid_pageno').value;
	pageno = 1;
	sort1 = document.getElementById('hid_sort1').value;
	
	getSearchData(bpath, srhterm, pageno, sort1);
}

function srhpaging(bpath, srhterm, pageno, sort1)
{
	getSearchData(bpath, srhterm, pageno, sort1);
}

function setsrhfocus()
{
	var srhval = document.getElementById('search').value;
	
	if(srhval == "Search")
	{
		document.getElementById('search').value = "";
	}
}

function setsrhblur()
{
	var srhval = document.getElementById('search').value;
	
	if(srhval != "Search" && srhval == "")
	{
		document.getElementById('search').value = "Search";
	}
}

function chkKeyCode(e,bpath)
{
	var srhval = document.getElementById('search').value;
	
	if(e.keyCode == 13)
	{
		document.getElementById('frmsrh').action = bpath + "search?search=" + srhval;
		document.getElementById('frmsrh').submit();
	}
}

/*********************************************** NEWSLETTER - START ************************************************/

function ts_focus()
{
	var email = document.getElementById("txtsubs");
	if(email.value == "enter your email...")
	{
		email.value = "";
		email.focus();
		return false;
	}
}

function ts_blur()
{
	var email = document.getElementById("txtsubs");
	if(email.value == "")
	{
		email.value = "enter your email...";		
		return false;
	}
}

function subs_user(bpath)
{
	var emailID = document.getElementById("txtsubs");	
		
	if((emailID.value==null)||(emailID.value==""))
	{
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if(echeck(emailID.value)==false)
	{
		emailID.focus()
		return false
	}

	var url = bpath + "php/subs_user.php?email="+emailID.value;	
	http.open("GET",url,true);
	http.onreadystatechange = handleHttpResponse_subs_user;
	http.send(null);
}

function handleHttpResponse_subs_user()
{
	if(http.readyState < 4)
    {}
	if(http.readyState == 4) 
    {
    	if(http.status==200) 
       	{
           	var results = http.responseText; 
           	alert(results);						
			return false;
		}
	}
}

function chkkey_subs(e, bpath)
{
	if(e.keyCode == 13)
	{
		subs_user(bpath);
	}
}

/*********************************************** NEWSLETTER - END ************************************************/