function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function ltrim(str) {
	if(str==null) {
		return null;
	}
	for(var i=0;str.charAt(i)==" ";i++);
	return str.substring(i,str.length);
}
function rtrim(str) {
	if(str==null) {
		return null;
	}
	for(var i=str.length-1;str.charAt(i)==" ";i--);
	return str.substring(0,i+1);
}

function trim(s) { 
// Remove leading spaces and carriage returns 
	return ltrim(rtrim(s));
	return s; 
} 

/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 *
 * @return  boolean  always true
 */
function setCheckboxes(the_form, do_check,element_name){

	var elts      = document.forms[the_form].elements[element_name];
	var elts_cnt  = (typeof(elts.length) != 'undefined')
				  ? elts.length
				  : 0;

	if (elts_cnt) 
	{
		for (var i = 0; i < elts_cnt; i++) {
			elts[i].checked = do_check;
		} // end for
	} else {
		elts.checked        = do_check;
	} // end if... else

	return true;
} // end of the 'setCheckboxes()' function
	
// confirm items deletion
function confirmDelete()
{
        var agree=confirm("Are you sure you wish to delete this?");
        if (agree)
                return true ;
        else
                return false ;
}



function printpage(){
	window.print();  
}

// show upload picture

function ShowPic(what, type, imgname) {

	var YourPictureURL = "/supplier/images/product_preview.gif";

	<!--Change this to your blank Image, or download mine for yourself-->

	var YourMessage = 'Please select a valid image file.';

	<!--Change this to your message you want on error-->

	var BlankImgHeight = "60";

	<!--Change this to your blank images height-->

	var BlankImgWidth = "69";

	<!--Change this to your blank images width-->

	var MaxWidth = "150";

	<!--Change this to your images maximum width, the hieght is changed by the ratio of the uploaded Image-->

	var CanExeOrNot = "TRUE";

	<!--Change this to "FALSE" or "TRUE", true meaning upload required and false meaning not required, leaving the double quotes and using only uppercase letters-->

	if (CanExeOrNot=="FALSE") {

		return true;

	} 

	else {


		var filename = "";

		var TheWidth = what.width;

		var TheHeight = what.height;

		var PicIndex = what.lastIndexOf('.', what.length - 1);

		var PicFileExtension = what.substr(PicIndex + 1).toLowerCase();

		var tmpimage = eval('document.images.'+imgname);

		if (what == ""){
			tmpimage.src=YourPictureURL;
			tmpimage.height=BlankImgHeight;
			tmpimage.width=BlankImgWidth;
			return true;
		}


		if ((PicFileExtension != 'jpg') && (PicFileExtension != 'gif') && (PicFileExtension != 'jpeg') && (PicFileExtension != 'bmp') && (PicFileExtension != 'png')) 

		{

			window.alert(YourMessage);

			tmpimage.src=YourPictureURL;

			//tmpimage.height=BlankImgHeight;

			//tmpimage.width=BlankImgWidth;

			return false;

		} 

		else {

			filename = "file:///" + what;

			tmpimage.src=filename;

			var TempRatio = ""

			var TempImage = new Image();

			TempImage.src = filename;

			if (!TempImage.width) {

				ShowPic(what, type, imgname);

			} 

			else {

				TempWidth = TempImage.width;

				TempHeight = TempImage.height;

				TempRatio = TempWidth / TempHeight;

				if (TempWidth == TempHeight) {

					MaxHeight=MaxWidth;

					tmpimage.height=MaxHeight;

					tmpimage.width=MaxWidth;

				} 

				else {

					var TempMathEqu = Math.round(TempRatio*100)/100;

					var TempMathFxw = 150 / TempMathEqu;

					var TempMathPqr = Math.round(TempMathFxw);

					tmpimage.width=MaxWidth;

					tmpimage.height=TempMathPqr;

				}

			}

		}

	}

}

// validate user_email

function isEmail(str) {

	// are regular expressions supported?

	var supported = 0;

	if (window.RegExp)

	{

			var tempStr = "a";

			var tempReg = new RegExp(tempStr);

			if (tempReg.test(tempStr)) supported = 1;

	}

	if (!supported) return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");

	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");

	return (!r1.test(str) && r2.test(str));

}

function checkFileSize(filenames,max_value,preview_pic){

	if (filenames != ""){
		var tmpimage = eval('document.images.'+preview_pic);
		var size = Math.round((tmpimage.fileSize)*1/1000); // Get image value in KB
		
		if (size > max_value){
			alert ("You are not allowed to upload an image more than "+ max_value + " KB");
			return false;
		}
	}
	return true;	
}


function checkInt(field) {
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	
	if (ok == "no") {
		//alert("Invalid entry!  Only numbers are accepted!");
		field.value = field.value.substring(0, field.value.length - 1);
		field.focus();
   }
} 

function checkName(field) {
	var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_."
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	
	if (ok == "no") {
		//alert("Invalid entry!  Only letters, numbers, or hyphens are accepted!");
		field.value = field.value.substring(0, field.value.length - 1);
		field.focus();
   }
}


function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function showHide(elemID,imageID,plusURL,minusURL) {
	if (document.getElementById) {
		if (document.getElementById(elemID).style.display == "block") {
			document.getElementById(elemID).style.display = "none";
			document.images[imageID].src = plusURL;
		} else {
			document.getElementById(elemID).style.display = "block";
			document.images[imageID].src = minusURL;
		}
	} else if (document.layers) {
		if (document[elemID].display == "block") {
			document[elemID].display = "none";
			document.images[imageID].src = plusURL;
		} else {
			document[elemID].display = "block";
			document.images[imageID].src = minusURL;
		}
	}
}

var checkflag = "false";
function selectAllCheckBox(the_form,field) {

	var elts      = document.forms[the_form].elements[field];
	var elts_cnt  = (typeof(elts.length) != 'undefined')
				  ? elts.length
				  : 0;


	if (elts_cnt) 
	{
		if (checkflag == "false") {
			for (i = 0; i < elts_cnt; i++) {
				elts[i].checked = true;
			}
			checkflag = "true";
		}
		else {
			for (i = 0; i < elts_cnt; i++) {
				elts[i].checked = false; 
			}
			checkflag = "false";
		}
	}
	else {
 		if (checkflag == "false") {
			elts.checked = true;
			checkflag = "true";
		}
		else{
			elts.checked = false;
			checkflag = "false";
		}
    } 
	
}

function expand_tree(id){
	hide_layer(id+'_normal');
	show_layer(id+'_edit');	
}

function collapse_tree(id){
	hide_layer(id+'_edit');
	show_layer(id+'_normal');
}

// Hide Layer
function hide_layer(name){
	var elm = document.getElementById(name);
	if (elm){ elm.style.display = 'none'; }
}

// Show Layer
function show_layer(name){
	var elm = document.getElementById(name);
	if (elm){ elm.style.display = 'block'; }
}