function windowControl(width, height, mode){
	var availWidth = screen.availWidth;
	var availHeight = screen.availHeight;
	if(width==0 || height == 0){}
	else{
		if(mode == 1){ //window at the left corner
			resizeTo(width, height);
			moveTo(0, 0);
		}else if(mode == 2){
			resizeTo(width, height);
			var x = (availWidth - width)/2;
			var y = (availHeight - height)/2;
			moveTo(x, y);
		}
	}
}

function limitLength(obj, length)
{
    var maxlength=length
    if (obj.value.length>maxlength)
        obj.value=obj.value.substring(0, maxlength)
}

function directHome(server){
	var home = server+"/cgw";
	document.location.href=home;
}


function returnHome(){
	var go = confirm("Please note that you are leaving the shopping panel, all the data not checked out will be deleted. \nDo you want to proceed?");
	if(go){
		var merchant = window.open("", "merchantSite", "");
		merchant.close();
		this.window.close();
	}
}
function returnHome(msg){
	var go = confirm(msg);
	if(go){
		var merchant = window.open("", "merchantSite", "");
		merchant.close();
		this.window.close();
	}
}
function returnHomeFromPayment(){
	var go = confirm("Please note that you are leaving the pay shipping page. You may continue your payment next time. \nDo you want to proceed?");
	if(go){
		this.window.close();
	}
}
function returnHomeFromPayment(msg){
	var go = confirm(msg);
	if(go){
		this.window.close();
	}
}
function returnHomeSilently(){
	if(true){
		var merchant = window.open("", "merchantSite", "");
		merchant.close();
		this.window.close();
	}
}

function goHomeSilently(server){
	var home = server+"/cgw";
	document.location.href=home;
}

function isRadioMandatory(radio){
	var chosen = "";
	for(i = 0; i < radio.length; i ++){
		if(radio[i].checked){
			chosen = radio[i].value;
			break;
		}
	}
	if(radio.length == undefined)
		if(radio.checked) chosen = radio.value;
		
	if(chosen == "") return false;
	return true;
}

function getRadioValue(radio){
	var chosen = "";
	for(i = 0; i < radio.length; i ++){
		if(radio[i].checked){
			chosen = radio[i].value;
			break;
		}
	}
	if(radio.length == undefined)
		if(radio.checked) chosen = radio.value;
	
	return chosen;
}


function openHelp(host, topic, section, all) {
	if(all=="all")
  		var maxWidth = "780";
  	else
  		var maxWidth = "560";
	var maxHeight = "600";
	var leftCorner = 300;
	var topCorner = 0;
 	var url = host+"common/com.comgateway.bluesky.commonhelp.HelpQuick.do?topic="+topic+"&section="+section+"&all="+all;
    var feature = "location=no, scrollbars=yes, resizable=yes, toolbar=yes, menubar=no, status=yes, width="+maxWidth+", height="+maxHeight+", left="+leftCorner+", top="+topCorner;
	window.open(url, "help_Screen", feature);
 } 
 
 
 function openHelp2(url, width, height) {
	var availWidth = screen.availWidth;
	var availHeight = screen.availHeight;
	if(width == 0) width = availWidth;
	if(height == 0) height = availHeight;
	var x = (availWidth - width)/2;
	var y = (availHeight - height)/2;
    var feature = "location=no, scrollbars=yes, resizable=yes, toolbar=yes, menubar=no, status=yes, width="+width+", height="+height+", left="+x+", top="+y;
	window.open(url, "help", feature);
 } 
 
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
 
function allowedCharacterEntry(string){
	var allowedCharacters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ~`!@#$%^&*()_+-={}[]\\/\\<>,.:\"\';\\|\\?";
	for(var i = 0; i < string.length; i ++){
		if(allowedCharacters.indexOf(string.charAt(i))<0){
			return false;
		}
	}
}

function allowedCharacterEntry2(string){
	for(var i = 0; i < string.length; i ++){
        if (string.charCodeAt(i) > 255) {
        	return false; 
        }
    }
    return true;
} 

function allowedPhoneEntry(string){
	if(/^[0-9]+$/.test(string)){
		return true;
	}
	return false;
}

function allowedPasswordEntry_old(string){
	if(/^[a-z,0-9,A-Z]+$/.test(string)){
		return true;
	}
	return false;
}

function allowedPasswordEntry(string){
	var allowedCharacters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ~`!@#$%^&*()_+-={}[]\\/\\<>,.:\"\';\\|\\?";
	for(var i = 0; i < string.length; i ++){
		if(allowedCharacters.indexOf(string.charAt(i))<0){
			return false;
		}
	}
	return true;
}

function allowedEmailEntry(string)
{
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(string))
    	return (true)
    return (false)
}
function alloweCreditCard(string){
	
	if(string.length == 16){
		if(/^[0-9]+$/.test(string))
			return (true)
		}
	return (false)
}

function numericOnly(e)
{
    var unicode=e.charCode? e.charCode : e.keyCode
    if (unicode!=8) //backspace
    { 
        if (unicode<48||unicode>57) //non numeric
            return false //disable key press
    }
}
function ValidateURL(string) {
 	var tomatch2= /[A-Za-z0-9\.-]{1,}\.[A-Za-z]{1}/;
 	if(!tomatch2.test(string))
 		return false;
 	return true;
}

function URLTrimmer(url){
	var HTTP = "HTTP";
	var HTTP_HEADER = "HTTP://";
	var HTTPS_HEADER = "HTTPS://";
	var WWW = "WWW";
	var SLASH = "/";
	var DOT = ".";
	
	if(url != "" || url != ""){
		url = url.toUpperCase();
		if(url.indexOf(HTTP_HEADER)>-1)
			url = url.substring(7);
				
		else if(url.indexOf(HTTPS_HEADER)>-1)
			url = url.substring(8);
			
		if(url.indexOf(WWW)>-1){
			var dot = url.indexOf(DOT);
			url = url.substring(dot+1);
		}
		
		if(url.indexOf(SLASH)>-1)
			url = url.substring(0, url.indexOf(SLASH));
		return url;
	} else {
		return "";
	}
}
function URLTrimmerHttp(url){
	var HTTP = "HTTP";
	var HTTP_HEADER = "HTTP://";
	var HTTPS_HEADER = "HTTPS://";
	var WWW = "WWW";
	var SLASH = "/";
	if(url != "" || url != ""){
		url = url.toUpperCase();
		if(url.indexOf(HTTP_HEADER)>-1)
			url = url.substring(7);
				
		else if(url.indexOf(HTTPS_HEADER)>-1)
			url = url.substring(8);
			
		if(url.indexOf(WWW)>-1){
			var dot = url.indexOf(DOT);
			url = url.substring(dot+1);
		}
		return url;
	} else {
		return "";
	}
}

function allowedSpecialCharEntry(string){
																							 
	var allowedCharacters = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ~`!@#$%^&*()_+-={}[]\\/|,.:\"\';?";
	for(var i = 0; i < string.length; i ++){
		if(allowedCharacters.indexOf(string.charAt(i))<0){
			return false;
		}
	}
	return true;
}

function allowedNameEntry(string){
	var allowedCharacters = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,()-";
	for(var i = 0; i < string.length; i ++){
		if(allowedCharacters.indexOf(string.charAt(i))<0){
			return false;
		}
	}
	return true;
}


function doCollapse(id) {
	try {
		document.getElementById(id + "_content").style.display = "none";
		document.getElementById(id + "_collapse").style.display = "none";
		document.getElementById(id + "_expand").style.display = "inline";
	}
	catch(e) {}
}

function doExpand(id) {
	try {
		var _as = document.getElementsByTagName('a');
		
		for(var i=0; i < _as.length; i++) {		
			var aa = _as[i];
	
				if(aa.name && document.getElementById(id + "_content")) {
				//alert('aa.name : '+aa.name +' , id : '+ id);
					if(aa.name == id) {
						try {
							document.getElementById(id + "_content").style.display = "inline";
							document.getElementById(id + "_collapse").style.display = "inline";
							document.getElementById(id + "_expand").style.display = "none";
						}
						catch(e) {}
					}
					else {
						doCollapse(aa.name);
					}
				}			
		}
	}
	catch(e) {} 
}

function doExpandSub(main,id) {
	try {
		var _as = document.getElementsByTagName('a');
		
		for(var i=0; i < _as.length; i++) {		
			var aa = _as[i];
				if(aa.name && document.getElementById(main+"_"+id + "_content")) {
				
					if(aa.name == main+"_"+id) {
						try {
							document.getElementById(main+"_"+id + "_content").style.display = "inline";
							document.getElementById(main+"_"+id + "_collapse").style.display = "inline";
							document.getElementById(main+"_"+id + "_expand").style.display = "none";
						}
						catch(e) {}
					} else if(aa.name != main){
						doCollapse(aa.name);
					}
				}			
		}
	}
	catch(e) {} 
}

function openPopUp(url, width, height){
	var maxWidth = width;
	var maxHeight = height;
	var leftCorner = 300;
	var topCorner = 0;
	var feature = "location=no, scrollbars=yes, resizable=yes, toolbar=no, menubar=no, status=no, width="+maxWidth+", height="+maxHeight+", left="+leftCorner+", top="+topCorner;
	window.open(url, "help_Screen", feature);
}

function popupCenter(url,x,y) 
{
 var width  = x;
 var height = y;
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,'windowname5', params);
 if (window.focus) {newwin.focus()}
 return false;
}

