// Removes default text from a text box
function clearDefaultText(ctl, defaultText){
	if (ctl.value == defaultText){
		ctl.value ="";
	}
}
function resetDefaultText(ctl, defaultText){
	if (ctl.value == ""){
		ctl.value = defaultText;
	}
}
/** tests if the passed variable is null
*/
function isNull(what){
   return what==null
}

/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }
 
    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}


function setBookmark(url,str){
if(str=='')str=url;
if (document.all)window.external.AddFavorite(url,str);
else alert('Press CTRL and D to add a bookmark to:\n"'+url+'".');
}


function checkEmail (strng) {
	var error="";
	if (strng == "") {
		error = "You didn't enter an email address.\n";
	}

	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) {
	   error = "Please enter a valid email address.\n";
	} else {
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (strng.match(illegalChars)) {
			error = "The email address contains illegal characters.\n";
		}
	}
	return error;
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   //error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters. \n";

    }
    if ((stripped.length < 5)) {
	error = "The phone number is the wrong length. \n";
    }
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers

    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    }
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }
return error;
}


// username - 4-10 chars, uc, lc, and underscore only.
function checkUsername (strng, field) {
	var error = "";
	if (strng == "") {
		error = "You didn't enter a \""+field+"\".\n";
	}
    var illegalChars = /[^a-zA-Z0-9_ ]/; // allow letters, numbers, and underscores
    if ((strng.length < 2) || (strng.length > 30)) {
       error = "Invalid "+field+"\n";
    }
    else if (illegalChars.test(strng)) {
	    error = "Illegal characters in \""+field+"\".\n";
    }
	return error;
}

function checkName (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a valid name.\n";
}


    var illegalChars = /\s\W/; // allow letters, numbers, and underscores
    if ((strng.length < 2) || (strng.length > 30)) {
       error = "Invalid Name\n";
    }
    else if (illegalChars.test(strng)) {
    error = "Illegal characters in Name Field.\n";
    }
return error;
}

// non-empty textbox
function isEmpty(strng, field) {
	var error = "";
  	if (strng.length == 0) {
    	error = "Required field \""+field+"\" is missing.\n";
	}
	return error;
}

// was textbox altered
function isDifferent(strng) {
var error = "";
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}



// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please select an Option.\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "Please select Registration Option.\n";
    }
return error;
}



// Requires Prototype.js
function getXmlNodeValue(xmlNode){
	return Try.these(
		function() {return xmlNode.nodeValue;},
		function() {return ""}
	);
}

function popUpDateSelector(url){
	window.open(url, 'popUpDateSelector', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=230,height=220,left = 312,top = 84');
}

// Removes any non alpha and numeric characters from ctl
// usage onkeyup="alphaNumeric($('ctlName'));"
function alphaNumeric(ctl){
   var regEx =  /\W/;  
   var result = ctl.value.replace(regEx, '');
   ctl.value = result;
}


// set all columns height to the tallest
function autoHeight(menu, content, right, wrapper){
	var height = $(wrapper).offsetHeight;
	
	if ($(menu).offsetHeight < height){
		var newHeight = height;
		var top = getStyle($(menu),"padding-top");
		var bottom = getStyle($(menu),"padding-bottom");
		newHeight = height - top.replace("px","") - bottom.replace("px","");
		$(menu).style.height = newHeight+"px";
	}
	if ($(content).offsetHeight < height){
		var newHeight = height;
		var top = getStyle($(content),"padding-top");
		var bottom = getStyle($(content),"padding-bottom");
		newHeight = height - top.replace("px","") - bottom.replace("px","");
		$(content).style.height = newHeight+"px";
	}
	if (right.length > 0 && $(right).offsetHeight < height){
		var newHeight = height;
		var top = getStyle($(right),"padding-top");
		var bottom = getStyle($(right),"padding-bottom");
		newHeight = height - top.replace("px","") - bottom.replace("px","");
		$(right).style.height = newHeight+"px";
	}
}

// get the CSS style value of elements
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

/** tests if the passed variable is an array
 */
function isArray(obj) {
	if (obj.constructor.toString().indexOf("Array") == -1)
		return false;
	else
		return true;
}

/* check if the item has a unique value in the database
 * table: the table name that the checking will be performed on
 * ownField: the database column that used to exclude yourself from checking while editing a product (so it will not say your name is not unique because you yourself has taken the name)
 * ownValue: the value for the above column
 * checkField: the database column that are going to be checked on
 * checkValue: the value for the above column
 * resDisplay: the ID of the <span> that are going to display the result of validation
 * resField: the hidden form field that used to store the validation result
 * uniqueText: the content that are going to display when it is an unique record (include the <span>, the text formatting and the tick image) i.e. <span style='color:#016e3a;'><img src='../images/tick.png'/> This product name is available</span>
 * notUniqueText: same as the above but this is for displaying result for non-unique record
 */
function checkUnique(table, ownField, ownValue, checkField, checkValue, resDisplay, resField, uniqueText, notUniqueText){
	var url = '../ajax/checkUnique.php?table='+table+'&ownField='+ownField+'&ownValue='+ownValue+'&checkField='+checkField+'&checkValue='+checkValue;

	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(req) {
			if (req.responseText == "true"){
				$(resDisplay).innerHTML = uniqueText;
				$(resField).value = "true";
			} else {
				$(resDisplay).innerHTML = notUniqueText;
				$(resField).value = "false";
			}
		}
	});
}
