function checkSearchBox()
{
	var len=document.library.date.length;
	var tg=0;
	for(var i=0;i<len;i++)
	{
		if (document.library.date[i].checked)
			tg = "true"; 
	}
	if (!tg)
	{
		if (document.library.categ_code.value=="" && document.library.keywd.value=="")
			alert("Please input search keyword or select a topic");
		else
		    document.library.submit();
	} else 
		    document.library.submit();
}


function show_demo(mylink)
{
	window.open(mylink,'new_window','top=150,left=300,width=450,height=500,scrollbars=yes');
}

//* pop window for product image *//
function viewPic(img)
{ 	
    picfile = new Image(); 
    picfile.src =(img); 
    fileCheck(img); 
}

function fileCheck(img)
{ 	
    if( (picfile.width!=0) && (picfile.height!=0) )
    { 
        makeWindow(img); 
    }
    else 
    {
        funzione="fileCheck('"+img+"')"; 
        intervallo=setTimeout(funzione,50); 
    }
}

function makeWindow(img)
{ 	
    ht = picfile.height + 20;
    wd = picfile.width + 20; 

    var args= "height=" + ht + ",innerHeight=" + ht;
    args += ",width=" + wd + ",innerWidth=" + wd;
    if (window.screen) 
    { 
        var avht = screen.availHeight; 
        var avwd = screen.availWidth;
        var xcen = (avwd - wd) / 2; 
        var ycen = (avht - ht) / 2;
        args += ",left=" + xcen + ",screenX=" + xcen;
        args += ",top=" + ycen + ",screenY=" + ycen + ",toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no"; 	
    }
    return window.open(img, 'new_window', args); 
} 

function showGlossary(tg,id,flat)
{
	if (tg==1)
	{
	    if(flat=='l1')
	    	prefix = '.';
	    else
	    	prefix = '..';
	    var url=prefix+"/glossary.html#"+id;
	} else if (tg==2)
		var url='../accessory.html#'+id;
	window.open(url,'new_window','top=150,left=300,width=500,height=500,scrollbars=yes,resizable=yes');
}

function showManual(id,tg,sign)
{
    if (tg=='faq' || tg=='mnl')
    {
    	if (tg=='faq')
    	{
		var i = document.getElementById('faq').value;
		var txt='faq_';
	}
	else  if (tg == 'mnl')  	
	{
		var i = document.getElementById('mnl').value;
		var txt='mnl_';
	}
	for (var j=1;j<=i;j++)
	{
		var mid = txt+j;
		if (mid == id)
		{
			if (document.getElementById(id).style.display == 'block')
			{
				document.getElementById(id).style.display = 'none';
			} else {
				document.getElementById(id).style.display = 'block';

			}
		} else {
			document.getElementById(mid).style.display = 'none';
		}
	}
    } else {
	var i = document.getElementById(tg).value;
	var txt=tg+'_';
	for (var j=1;j<=i;j++)
	{
		var mid = txt+j;
		if (mid == id)
		{
			if (document.getElementById(id).style.display == 'block')
			{
				document.getElementById(id).style.display = 'none';
				document.getElementById(sign).setAttribute("src", "./images/plus.jpg");
			} else {
				document.getElementById(id).style.display = 'block';
				document.getElementById(sign).setAttribute("src", "./images/minus.jpg");
			}
		} else {
			var msg="sg_"+txt+j;
			document.getElementById(msg).setAttribute("src", "./images/plus.jpg");
			document.getElementById(mid).style.display = 'none';
		}
	}

//    	document.getElementById(id).style.display = 'block';
    }
}

function showProduct(url)
{
	window.location = url;
}

function compareProduct(url,layer)
{
    if (layer=='l2')
    	var prefix='../..';
    else
    	var prefix='..';
    var arr = document.getElementsByName("pro_id[]");
    var ct=0;
    for (var i=0; i<arr.length; i++)
    {
    	if (arr[i].checked)
    	    ct++;
    }
    if (ct <= 0)
    {
    	alert("Please select at least 1 product");
    } else if (ct>3) {
    	alert("Compare up to 3 products");
    } else {
	var link = prefix+'/compare.php'+"?url="+url;
	document.proLst.action=link;
	document.proLst.submit();
    }
}

function sortProducts(target,id,pid)
{ 
	var sid=Math.random();
	var url = "getSortResult.php?tg="+target+"&id="+id+"&pid="+pid+"&sid="+sid;
	window.location=url;
}




function playSound(layer) {

    switch(layer)
    {
    	case 'l1':
		document.getElementById("radioShow").innerHTML=
		"<embed src='./mp3/episode101.mp3' hidden='true' autostart='true' loop='false' /></embed>	<BGSOUND src='./mp3/episode101.mp3' loop='false' >";
		break;
    	case 'l2':
		document.getElementById("radioShow").innerHTML=
		"<embed src='../mp3/episode101.mp3' hidden='true' autostart='true' loop='false' /></embed>	<BGSOUND src='../mp3/episode101.mp3' loop='false' >";
		break;
    	case 'l4':
		document.getElementById("radioShow").innerHTML=
		"<embed src='../../../mp3/episode101.mp3' hidden='true' autostart='true' loop='false' /></embed>	<BGSOUND src='../../../mp3/episode101.mp3' loop='false' >";
		break;
    }
}

function stopSound() {
	document.getElementById("radioShow").innerHTML="";
}

// * dealer download page * //
function validate()
{
	if(document.logForm.user.value=="" && document.logForm.passwd.value=="")
		alert("User Name and Password can not be empty");
	else if(document.logForm.user.value=="")
		alert("User Name can not be empty.");
	else if(document.logForm.passwd.value=="")
		alert("Password can not be empty.");
	else {
		document.logForm.submit();
	}
}

// for download page with login function.
function getPDF(v)
{
	url = "download.php?bt="+v;
	window.location = url;
	
}




// subscribe form page
function isValidEmail() {
   var fname = document.subForm.fname.value;
   var lname = document.subForm.lname.value;
   var email = document.subForm.email.value;
   var tel = document.subForm.tel.value;
   var note = "";
   if (fname == "")
   	note = note + "First Name can not be empty.\n";
   if (lname == "")
   	note = note + "Last Name can not be empty.\n";
   if (email == "" || (email.indexOf(".") <= 2) || (email.indexOf("@") <= 0))
   	note = note + "Invalid email address\n";
   if (tel=="")
   	note = note + "Telephone number can not be empty.\n";
   
   if (note=="")
   	document.subForm.submit();
   else
   	alert(note);
}


function detectKey() {
//alert(event.keyCode);
	if (event.keyCode ==13)
		validate();
}

function isValid(email) {
   if (email != "" && ( (email.indexOf(".") <= 2) || (email.indexOf("@") <= 0)))
   {	var note = "Invalid email address";
   	alert(note);
   	return false;
   } else
   	return true;
}

function recalculate(id,qty,pid)
{
	var url = "./updateqty.php?id="+id+"&qty="+qty+"&pid="+pid;
	window.location=url;
}

function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}


function validForm(form,par)
{
   var note = "";
   var parList = par.split(";");
   var sign = false;

   for (var i=0;i<parList.length;i++)
   {
   	var varValue=trim(eval("document."+form+"."+parList[i]+".value"));

   	var varName = parList[i];
   	if (varValue == "")
   	{
   		sign = true;
   	} 
   	if (varName == "email")
   	{
   	    if (varValue == "" || (varValue.indexOf(".") <= 2) || (varValue.indexOf("@") <= 0))
   		note = note + "Invalid email address\n";
   	} 
   	if (varName == "password") 
   	{
   	    if (varValue.length<5)
   		note = note + "Password must be a minimum of 5 characters\n";
   	}
   }
   if (sign)
   {
   	note = note + "Please fill up all required fields.\n";
   }
   if (note=="")
   	eval("document."+form+".submit()");
   else
   	alert(note);
}

function updateQty(v,url)
{
    document.orderfm.action="updateqty.php?tg="+v+"&url="+url;
    
}

function isChecked(box,id,pid)
{
recalculate(id,0,pid);
//    if (box.checked==true)
//	recalculate($part_id,0,$price,$pid);
}

function showParts(pid,tg)
{
	var url = tg+"/air-purifiers/parts_accessories.php?pid="+pid;
	window.location = url;
}

function genPWD()
{
    if (isValid(document.orderfm.email.value))
    {
//	alert('Please check your email to get new password') ;
   	window.location = "forgotpwd.php?em="+document.orderfm.email.value;
    }
}

function showJobDetail(id)
{
    var str=document.getElementById('idstr').value;
    var arr=str.split(",");
    for(var i=0;i<arr.length;i++)
    {
 	var jid=arr[i];
 	if (jid==id)
    	    document.getElementById(jid).style.display = "block";
    	else
    	    document.getElementById(jid).style.display = "none";
    	
    }
}
