
function trim(str) {
	return str.replace(/^\s+|\s+$/g, '')
};

function isNumeric(str) {
	var validChars = "0123456789.";
	var isNumber=true;
	var cchar;
 
	for (i=0; i<str.length; i++) { 
		cchar = str.charAt(i); 
		if (validChars.indexOf(cchar) == -1) {
	 isNumber = false;
			break;
		}
	}

	return(isNumber);
}
   
function isEmail(s) {
	var i = 1;
	var sLength = s.length;

	// look for @
	while ((i < sLength) && (s.charAt(i) != "@")) { 
		i++; 
	}

	if ((i >= sLength) || (s.charAt(i) != "@")) {
		return false;
	} else {
		i += 2;
	}

	// look for .
	while ((i < sLength) && (s.charAt(i) != ".")) { 
		i++; 
	}

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) {
		return false;
	} else {
		return true;
	}
}

function hasValue(val) {
    if (val == null) {
	return(false);
    }

    var s = trim(val);

    if (s == "") {
	return(false);
    }

    return(true);

}

function isBlank(val) {
	return(!hasValue(val));
}

// Returns a random positive integer > 0 <= upperBound
function randomInt(upperBound) {
	return( Math.round((Math.random()*upperBound) + 0.5) );
}

function popup(link) {
	win = window.open(link, "popup")
}

function setColour(id,ON){
	var el = document.getElementById(id);
	if(ON){
		el.style.backgroundColor='#C6CFDC';
	} else {
		el.style.backgroundColor='transparent';
	}
}

function logout() {
	if (confirm("Are you sure you wish to logout?")) {
		location = "logout.php";
	}
}

function highlightRow(id) {
	var elmt = document.getElementById(id);
	elmt.style.backgroundColor='#C6CFDC';
}

function unHighlightRow(id) {
	var elmt = document.getElementById(id);
	elmt.style.backgroundColor='transparent';
}

function getExtension(value) {
	return value.substring(value.lastIndexOf('.') + 1,value.length); 
}

function toggleDisplay(elementId) {
	var elm = document.getElementById(elementId);
	var isDisplayed;
	var currentDisplay;

	if (null === elm) {
		alert("Fatal error: Could not locate element with id '" + elementId + "'");
	} else {
		currentDisplay = elm.style.display;

		if ("block" == currentDisplay) {
			elm.style.display = "none";
			isDisplayed = false;
		} else {
			elm.style.display = "block";
			isDisplayed = true;
		}
	}

	return(isDisplayed);
}

function hideElement(elementId) {
	var elm = document.getElementById(elementId);

	if (null === elm) {
		alert("Fatal error: Could not locate element with id '" + elementId + "'");
	} else {
		elm.style.display = "none";
	}
}


function getHTTPRequest() {
	var rqObject;

	if(navigator.appName == "Microsoft Internet Explorer") {
		rqObject = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		rqObject = new XMLHttpRequest();
	}

	return(rqObject);
}

function callLocation(target) {
	
	if (confirm("Are you sure?")) {
		//document.write(target);
		location = target;
	}
}

function browseFiles(path) {
            window.SetUrl=function(value){
               				
				document.getElementById(path).value = value;
            }
            			
            var filemanager='/fckeditor/editor/filemanager/browser/default/browser.html';
            var connector='http://' +window.location.host + '/fckeditor/editor/filemanager/connectors/php/connector.php';

            window.open(filemanager+'?Connector='+connector+'&Type=Image','fileupload','modal,width=600,height=400');
        }

function browseGalleryFiles(path) {
            window.SetUrl=function(value){
               			
				document.getElementById(path).value = value;
				document.getElementById("imagePreview").src = value;	
				document.getElementById("imagePreview").style.display = "block";	
				
            }
            			
            var filemanager='/fckeditor/editor/filemanager/browser/default/browser.html';
            var connector='http://' +window.location.host + '/fckeditor/editor/filemanager/connectors/php/connector.php';

            window.open(filemanager+'?Connector='+connector+'&Type=Image','fileupload','modal,width=600,height=400');
        }

/*
function getSectionOptions(parentId) {
	var rqo = getHTTPRequest();
	var dest = "http://dms.towerinteractive.com/ajax/getsectionoptions.php?parentId=" + parentId;

	rqo.onreadystatechange=function() {
		if(rqo.readyState == 4) {
			if (rqo.status == 200) {
				responseText = rqo.responseText;

				if (responseText == "noauth") {
					alert("Your login has expired, please re-login.");
					toggleBusy();
					location = "/logout.php";
				} else {
				       alert(rqo.responseText);
				}
			}
		}
	};
}
*/

function importCatalog() {
        target = "/include/cataloguploadform.php";
        win = window.open(target, "Catalog", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function exportOrders() {
        target = "/include/orderexportform.php";
        win = window.open(target, "Export", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}


