// disabled enter on all forms!
function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}
document.onkeypress = stopRKey; 
// end disabling enter on forms


function confirm_continue() {
	var answer = confirm("Are you sure about this. This action is NOT undoable.")
	if (answer) {
		return true;
	} else {
		return false;
	}
}


function draw_saved_matrixes() {
	var url = '/ajax_functions/draw_saved_matrixes.php';
	var myAjax = new Ajax.Request(url, { method: 'get', onComplete: function(originalRequest) {
		return_val = originalRequest.responseText; 
		$('saved_matrixes').innerHTML = return_val;
	} });
}


function delete_saved_matrix(uid) {
	x = window.confirm("Are you sure you want to delete that matrix? This is not undoable.");
	if (x) {
		var url = '/ajax_functions/delete_saved_matrix.php?uid=' + uid;
		var myAjax = new Ajax.Request(url, { method: 'get', onComplete: function(originalRequest) {
			return_val = originalRequest.responseText; 
			if (return_val == "1")  {
				draw_saved_matrixes();
			} else {
				window.alert('Internal Error. Try again. (#' +return_val + ').');
			}
		} });
	}
}


function cash(amount, commas){ // used for rounding too (in the hardware page)
		var temp = "" + amount;
		var temp2 = temp.replace(/\$/g ,""); // replace all $ with ""
		var temp3 = temp2.replace(/,/g ,""); // replace globaally all "," with ""
		var i = parseFloat(temp3);
        if(isNaN(i)) { i = 0.00; }
        var minus = '';
        if(i < 0) { minus = '-'; }
        i = Math.abs(i);
        i = parseInt((i + .005) * 100);
        i = i / 100;
        s = new String(i);
        if(s.indexOf('.') < 0) { s += '.00'; }
        if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
        s = minus + s;
        if(commas != false) s = CommaFormatted(s);
        return s;
}


function CommaFormatted(amount) {
        var delimiter = ","; // replace comma if desired
        var a = amount.split('.',2)
        var d = a[1];
        var i = parseInt(a[0]);
        if(isNaN(i)) { return ''; }
        var minus = '';
        if(i < 0) { minus = '-'; }
        if (amount == 0) {
        	n = "0";
        } else if (amount < 0) {
			n = new String(Math.abs(i));
		} else { // i is positive
			n = new String(Math.abs(i*-1));// have to put that in there to somehow fix abs(0) which is -0 for some reason
		}
        // window.alert("n is " + n + " and i is " + i);
        var a = [];
        while(n.length > 3)
        {
                var nn = n.substr(n.length-3);
                a.unshift(nn);
                n = n.substr(0,n.length-3);
        }
        if(n.length > 0) { a.unshift(n); }
        n = a.join(delimiter);
        if(d.length < 1) { amount = n; }
        else { amount = n + '.' + d; }
        amount = minus + amount;
        return amount;
}

function openPopWin(winURL, winWidth, winHeight, winFeatures, winLeft, winTop) {
	var popWin = null    // use this when referring to pop-up window
	var winCount = 0
	var winName = "popWinx"
	var d_winLeft = 0  // default, pixels from screen left to window left
	var d_winTop = 0   // default, pixels from screen top to window top
	winName = "popWinx"// + winCount++ // unique name for each pop-up window
	// close any previously opened pop-up window
	if (navigator.appName != "Microsoft Internet Explorer" || parseInt(navigator.appVersion) >=4) //do not close if early IE
	if(popWin != null) if(!popWin.closed) popWin.close() 
	
	if (openPopWin.arguments.length >= 4)  // any additional features? 
		winFeatures = "," + winFeatures
	else 
		winFeatures = "" 
	if (openPopWin.arguments.length == 6) { // location specified
		winFeatures += getLocation(winWidth, winHeight, winLeft, winTop);
	} else {
		winFeatures += getLocation(winWidth, winHeight, d_winLeft, d_winTop);
	}
	popWin = window.open(winURL, winName, "width=" + winWidth  + ",height=" + winHeight + winFeatures);
	if (window.focus) {popWin.focus()}
}

function getLocation(winWidth, winHeight, winLeft, winTop){
		var winLocation = ""
		if (winLeft < 0)
			winLeft = screen.width - winWidth + winLeft
		if (winTop < 0)
			winTop = screen.height - winHeight + winTop
		if (winTop == "cen")
			winTop = (screen.height - winHeight)/2 - 20
		if (winLeft == "cen")
			winLeft = (screen.width - winWidth)/2
		if (winLeft>0 & winTop>0)
			winLocation =  ",screenX=" + winLeft + ",left=" + winLeft     
					+ ",screenY=" + winTop + ",top=" + winTop
		else
			winLocation = ""
		return winLocation
	} 

function trim(inputString) {
	// Removes leading and trailing spaces from the passed string. Also removes
	// consecutive spaces and replaces it with one space. If something besides
	// a string is passed in (null, custom object, etc.) then return the input.
	if (typeof inputString != 'string') { return inputString; }
		var retValue = inputString;
		var ch = retValue.substring(0, 1);
		while (ch == ' ') { // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == ' ') { // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf('  ') != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf('  ')) + retValue.substring(retValue.indexOf('  ')+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
} 
