/*
 * A dummy javascript that performs nothing for troubleshooting purpose
 */
function doNothing() {}



/*
 * A simple echo message for debugging purpose
 */
function debug(msg) { if(msg) alert(msg); }



/**
 * Given a text string, determine if this text string contains the
 * set of special characters as identified by Visa TS.
 * @param textstring - the text string passed in to be checked
 * @return boolean - true if this text string contains special characters
 */
function hasSpecialCharacters(textstring) {
    var specialstring = "<>\"';()%&";
    if( !textstring ) { return false; }
    if( textstring.length <= 0 ) { return false; }
    for(j=0; j<specialstring.length; j++) {
        if( textstring.indexOf(specialstring.charAt(j)) != -1 ) { return true; }
    }
    return false;
}


/**
 * Given a text string, examine if it is a valid email address
 */
function isValidEmail(s) {
	var regexp = "^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})$";
	if( s ) { return s.match(regexp); }
	else { return false; }
}


/**
 * Given a text string, examine if it is a valid english name
 */
function isValidName(name) {
	var regexp = "^[a-zA-Z]+[a-zA-Z- ,]+[a-zA-Z]+$";
	if( name ) { return name.match(regexp); }
	else { return false; }
}



/**
 * Performs left trim of a string to remove whitespaces9947
 * @param str - the string for which whitespaces at left will be removed
 * @return string - the string with whitespaces at left removed
 */
function ltrim(str) {
	if (str==null){return str;}
	for (var i=0; str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\r" || str.charAt(i)=="\t"; i++);
	return str.substring(i,str.length);
}



/**
 * Performs right trim of a string to remove whitespaces
 * @param str - the string for which whitespaces at right will be removed
 * @return string - the string with whitespaces at right removed
 */
function rtrim(str) {
	if (str==null){return str;}
	for (var i=str.length-1; str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\r" || str.charAt(i)=="\t"; i--);
	return str.substring(0,i+1);
}



/**
 * Trim leading and trailing whitespaces of a string
 * @param str - the string for which whitespaces at left & right will be removed
 * @return string - the string with whitespaces at left & right removed
 */
function trim(s) { return ltrim(rtrim(s)); }



/**
 * Map a layer of shade over the whole screen
 * The CSS must define the style for div ID overlay
 */
function showOverlay() {
	bod 		= document.getElementsByTagName('body')[0];
	overlay 	= document.createElement('div');
	overlay.id	= 'overlay';	
	bod.appendChild(overlay);
}



/**
 * Remove the layer of shade over the whole screen
 * The CSS must define the style for the div ID
 */
function hideOverlay() {
	$('overlay').style.display = 'none';
}



/**
 * Evoke the preloader panel with a pre-defined width and height
 * and set the title and message of the loading space as well as
 * whether it is in modal mode.
 * @param width - the width of the preloader panel
 * @param height - the height of the preloader panel
 * @param title - the title text of the preloader panel
 * @param message - the prompting text of the preloader panel
 * @param modal - the boolean value to trigger overlay
 */
function preload(pwidth, pheight, title, message, modal) {    
	winW = getWinDim('w');
	winH = getWinDim('h');
	if( !pwidth )  { pwidth = 300; }
	if( !pheight ) { pheight = 80; }
	$('loadingpane').style.top  = (winH-pheight)/2 + 'px';
	$('loadingpane').style.left = (winW-pwidth)/2 + 'px';
	$('loadingstatus').innerHTML = title;
	$('loadingtext').innerHTML = message;
	if( modal ) { showOverlay(); }
	$('loadingpane').style.display = "block";
}


/**
 * Close the preloader panel
 *
 */
function preloadOff(modal) {
	if( modal ) { hideOverlay(); }
	$('loadingpane').style.display = "none";
}



function getWinDim(dimension) {
	var frameWidth, frameHeight;
	if (self.innerWidth) {
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}
	else if (document.body) {
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}
	if( dimension=='w' ) { return frameWidth; }
	else if( dimension=='h' ) { return frameHeight; }
	else { return 0; }
}


function winpop(url, name, w, h) {
	var features = "width="+w+",height="+h+",status=no,scrollbars=no,resizable=no;toolbar=no,menubar=no,location=no";
	window.open(url, name, features);
}

function highlightnav(navID) {
	if( !navID ) { return; }
	$(navID).style.backgroundColor = 'orange';
	$(navID).style.color = 'white';
}


function evaluateXPath(aNode, aExpr) {
  var xpe = new XPathEvaluator();
  var nsResolver = xpe.createNSResolver(aNode.ownerDocument == null ? aNode.documentElement : aNode.ownerDocument.documentElement);
  var result = xpe.evaluate(aExpr, aNode, nsResolver, 0, null);
  var found = [];
  var res;
  while (res = result.iterateNext())
    found.push(res);
  return found;
}

