//***********************************************************************
// common.js
// This is a collection of commonly used utilities which includes string
// manipulation, email format validation, password format validation etc
// To ensure that these functions are generic and highly reusable, there
// will only be validation logic and no custom error or warning messages
// being output from the functions.
//***********************************************************************


/**
 * Calls the Parent reference
 */

function parentRef(URL){
	parent.location.href=URL;
}



/**
 * Validates email address format
 */
function validateEmailFormat(email) {
	if( email==undefined || email==null || email.length==0 ) { return false; }
	var theStr = new String(email);
	var index  = theStr.indexOf("@");
	if (index > 0) {
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1)) { return true; }
		else { return false; }
	}
	else { return false; }
}


/**
 * Validates password format
 * - disallow special characters
 * - minimum length is 8 characters or more by default
 * @return 0  : pass
 * @return -1 : string is lesser than 8 characters
 * @return -2 : string contains white space in between
 * @return -3 : string contains special characters
 * @return -9 : string is null or undefined
 */
function validatePasswordFormat(p) {
	if( p==undefined || p==null ) { return -9; }
	if( p.length < 8 ) { return -1; }

	return 0;
}



/**
 * Performs left trim of a string to remove whitespaces
 */
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
 */
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
 */
function trim(s) { return ltrim(rtrim(s)); }



/**
 * Open a pop up window
 */
function winopen(tgturl, tgtname, tgtwidth, tgtht) {
  var startX = (window.screen.availWidth  - tgtwidth)/2;
  var startY = (window.screen.availHeight - tgtht)/2;
  var features = "top=" + startX + ",left=" + startY + ",width=" + tgtwidth + ",height=" + tgtht + ",toolbar=no,menubar=no,location=no,status=no,resizable=no,scrollbars=no";
  window.open(tgturl, tgtname, features);
}
// tools - shopping safely
function openwin2(url){
	window.open(url,'MyMoneySkills','width=791,height=590,scrollbars=yes');
}

function openwin3(url){
	window.open(url,'MyMoneySkills','width=415,height=170,scrollbars=no');
}

//Index - Lost your Card?

function openNewWindow_toll_no()
{
   window.open("http://www.visa-asia.com/ap/Subscription/gcas.jsp", "_blank",
      "height=366px width=360px");
}
