// JavaScript Document
// mylibrary.js
// Combine all necessary javascript codes
// Revision: Patch 021
// Last update: 7 April 2006

// Global variables
var myWindow = null;

// The openWin function is used to open a new window by setting up some features
// such as toolbar, location, directories, status, menubar, scrollbar, and copyhistory.
// There are 3 features that can be passed into the function: none, basic and full.
// none:  Display a plain window, non-resizable.
// basic: Display a resizable window with scrollbars and menubar,
// full:  Display all features

function openWin(url,targetname,W,H,L,T,thefeatures)
{
	var params			=	"";
	var nofeatures		=	"toolbar=0,location=0,directories=0,status=0,";
		nofeatures		+=	"menubar=0,scrollbars=0,resizable=0,copyhistory=0";
	var basicfeatures	=	"scrollbars=1,resizable=1,menubar=1";
	var morefeatures	=	"toolbar=1,location=1,directories=1,";
		morefeatures	+=	"status=1,copyhistory=1";
	var dimensions		=	"width="    + W + ",height="  + H;
	var placement		=	"left="     + L + ",top="     + T;
		placement		+=	",screenX=" + L + ",screenY=" + T;
	
	switch (thefeatures)
	{
	case "none":
		params	+=	nofeatures;
		break;
	case "basic":
		params  +=  basicfeatures;
		break;
	case "full":
		params  +=	basicfeatures + "," + morefeatures;
		break;
	default:
		params	+=  thefeatures;
	
	}

	params	+=	"," + dimensions + "," + placement;

	
	myWindow = window.open(url,targetname,params);

}

function showSearchWin()
{
	
	if(!document.searchForm.req_domain.value)
	{
	    showWarning("Please enter your preferable domain name");	   
	}
	else
	{
		openWin("search.php?domain="+document.searchForm.req_domain.value,"SEARCH",600,550,100,5,"none");
	}
	
}
// The closeWin function is used to close a window.
function closeWin()
{
	if(myWindow != null)
	{
		myWindow.close();
		myWindow = null;
	}
}

// The showWarning function is used to show warning messages
function showWarning(warningText)
{
	window.alert(warningText);
}

function showMsg(msg)
{
	window.prompt(msg);
}
function showBrowserInfo()
{
   var theApp		= navigator.appName.toLowerCase();
   var UA			= navigator.userAgent.toLowerCase();
   var isIE			= (UA.indexOf('msie') >= 0)?true:false;
   var isNS			= (UA.indexOf('mozilla') >= 0)?true:false;
   var thePlatform	= navigator.platform.toLowerCase();
   var isMAC		= (UA.indexOf('mac') >= 0)?true:false;
   var isWIN		= (UA.indexOf('win') >= 0)?true:false;
   var isUNIX		= (UA.indexOf('x11') >= 0)?true:false;
   var version		= navigator.appVersion;
   var isMajor		= parseInt(version);
   var isMinor		= parseFloat(version);;
   var obsolete		= (document.getElementById)?false:true;
   var tmp			= "";
	
   if(UA.indexOf('compatible')>0)
	{
	   isNS = false;
	}
   
   // Internet Explorer Version 5 on the Mac reports itself as version 4. This code corrects the problem.
   if(isIE && isMAC)
	{
	   if(UA.indexOf("msie 5"))
		{
		   var stringLoc = UA.indexOf("msie 5");
		   isMajor = 5;
		   version = UA.substring(stringLoc + 5,stringLoc + 8);
		}
	}

	if(isNS && isMajor>4)
	{
		if(UA.indexOf("netscape6"))
		{
			var stringLoc = UA.indexOf("netscape6");
			isMajor = 6;
			version = UA.substring(stringLoc + 10,stringLoc + 14);
		}
	}
	
	tmp		=	"User Agent: "	+ UA + "\n";
	tmp		+=	"Platform: "	+ thePlatform + "\n";
	tmp		+=	"Macintosh: "	+ isMAC + "\n";
	tmp		+=	"Windows: "		+ isWIN + "\n";
	tmp		+=	"Application: " + theApp + "\n";
	tmp		+=	"Version: "		+ version + "\n";
	tmp		+=	"Netscape: "	+ isNS + "\n";
	tmp		+=	"Internet Explorer: " + isIE + "\n";
	tmp		+=  "Major Version: " + isMajor + "\n";
	tmp		+=  "Full Version: " + isMinor + "\n";
	tmp		+=	"\n";

	if(obsolete)
	{
		tmp	+=	"You really should upgrade to a modern browser.";
	}
	else
	{
		tmp	+=	"You appear to have a modern browser.";
	}

	showWarning(tmp);
}
function Trim(TRIM_VALUE)
{
	if(TRIM_VALUE.length < 1)
	{
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE=="")
	{
		return "";
	}
	else
	{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0)
	{
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) == w_space)
		{
		}
		else
		{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	} //End While
	return strTemp;

} //End Function

function LTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1)
	{
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) == w_space)
		{
		}
		else
		{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

// The updateStatusBar function is used to display messages on the status bar of browsers.
function updateStatusBar(msg)
{
	window.status = msg;
}

function linkTo(link)
{
	window.location.href = link;
}

function isRadioSelected(radio) 
{
	var idx = -1;
	
	for (var i=radio.length-1; i > -1; i--) 
	{
		if (radio[i].checked) 
		{
			idx = i; 
			i = -1;
		}
	}
	if (idx > -1) 
		return true;
	else 
		return false;
}
function isItemListSelected(list)
{
	for(	var i=0; i < list.options.length; i++ )
	{
		if( list.options[i].selected )
			return true;
	}
	return false;
}
function getSelectedItem(list)
{
	if( isItemListSelected(list) )
	{
		for(	var i=0; i < list.length; i++ )
		{
				if( list.options[i].selected )
				return list.options[i].value;
		}
	}
	else
	{
			return -1;
	}
}
// The isValidEmail is used to validate email address.
function isValidEmail(strEmail){
  validRegExp = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
  

   // search email text for regular exp matches
   if (!validRegExp.test(strEmail)) 
   {
      return false;
   } 
    return true;
}

function addNewGroup()
{
	var gName = Trim(document.addgroup.addgroup.value);
	if(gName.length == 0)
	{
		showWarning("Please enter a group name.");
	}
	else
	{
		document.addgroup.submit();
	}
	
}
function addBrand()
{
	var bName = Trim(document.addbrand.addbrand.value);
	if(bName.length == 0)
	{
		showWarning("Please enter a brand name.");
	}
	else
	{
		document.addbrand.operation.value = "addbrand";
		document.addbrand.submit();
	}
}
function addProduct()
{
			document.productForm.action = "editproduct.php";
			document.productForm.operation.value = "new";
			document.productForm.submit();

}
function submitProduct(Operation)
{
		document.productForm.operation.value = Operation;
		document.productForm.status.value = "product";
		if( Operation == "delete" )
		{
			if(window.confirm("Do you want to delete this product?"))
			{
				document.productForm.action = "item_processes.php";
				document.productForm.operation.value = "delete";
				document.productForm.submit();
			}
		}
		else
		{
			document.productForm.action = "editproduct.php";
			document.productForm.submit();
		}
}
function selectProduct()
{
	var pid = getSelectedItem(document.productForm.prod_id);
	if( pid == -1)
	{
		showWarning("Please select a product");
		
	}
	else
	{
		submitProduct("select");
	}
}
function submitBrand(Operation)
{
		document.brandForm.operation.value = Operation;
		document.brandForm.status.value = "brand";
		if(Operation == "delete")
		{
			if(window.confirm("Do you want to delete this brand?"))
				{
					document.brandForm.submit();
				}
		}
		else
		{
			document.brandForm.submit();
		}
}
function selectBrand()
{
	var bid = getSelectedItem(document.brandForm.brandid);
	if( bid == -1)
	{
		showWarning("Please select a brand of the product");
		
	}
	else
	{
		submitBrand("select");
	}
}
function submitGroup(Operation)
{
		document.groupForm.operation.value = Operation;
		document.groupForm.status.value = "group";
		if( Operation == "delete" )
		{
			if(window.confirm("Do you want to delete this group?"))
			{
				document.groupForm.submit();
			}
		}
		else
		{
			document.groupForm.submit();
		}
}
function selectGroup()
{
	var gid = getSelectedItem(document.groupForm.groupid);
	if( gid == -1)
	{
		showWarning("Please select a group of the product");
		
	}
	else
	{
		submitGroup("select");	
	}
}
function deleteGroup()
{
	var gid = getSelectedItem(document.groupForm.groupid);
	if( gid == -1)
	{
		showWarning("Please select a group of the product");
	}
	else
	{
		submitGroup("delete");
	}
}

function deleteBrand()
{
	var bid = getSelectedItem(document.brandForm.brandid);
	if( bid == -1)
	{
		showWarning("Please select a brand of the product");
	}
	else
	{
		submitBrand("delete");
	}
}

function deleteProduct()
{
	var pid = getSelectedItem(document.productForm.prod_id);
	if( pid == -1)
	{
		showWarning("Please select a product");
	}
	else
	{
			submitProduct("delete");
	}
}
function clearTextBox(obj)
	{
		obj.value="";
	}
function deleteFormProduct()
{
	if(window.confirm("Do you want to delete this product?"))
	{
				document.prodForm.action = "item_processes.php";
				document.prodForm.operation.value = "delete";
				document.prodForm.submit();
	}
}
function isValidProductDetails()
{
	var pName = Trim(document.prodForm.productname.value);
	if( pName == '' )
		return false;
	else true;
}
function saveProduct()
{
	var pName = Trim(document.prodForm.productname.value);
	if( pName == '' )
	{
		alert('Please enter a product name');
	}
	else
	{
		updateRTE('prod_detail');
		document.prodForm.action = "editproduct.php?op=save";
		document.prodForm.submit();
	}
}
function getSelectedList( l )
{
	for(var i=0; i< l.options.length; i++)
	{
		if(l.options[i].selected)
			return l.options[i].value;
	}
	return null;
}
function updateBrand( l, g )
{
  var b = getSelectedList( l );
  if(b == '')
  {
	document.brandForm.action="group.php?group_id="+g;
	document.brandForm.submit();
  }
  else
  {
	document.brandForm.action="group.php?group_id="+g+"&brand_id="+b;
	document.brandForm.submit();  
  }
  
}