function replace(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;
    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;
    var newstr = string.substring(0,i) + by;
    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);
    return newstr;
}

function trim(strText) { 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);
    return strText;
} 

function existsEmail(text) { 
	var sss = text;	
	sss = trim(replace(replace(sss,'\r',' '),'\n',' '));
	if (sss=="") return false; 	
	var temp = Array();
	temp=sss.split(/[<>, ;{}\-\*\%\$\#\&\!\~]/);
	for (var i=0,n=temp.length, s=''; i<n; i++) {
		s = temp[i];
		if (s.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) 
			return true;     	
	}		
	return false; 	
}

function checkData (){
	if (existsEmail(document.forms[0].qtext.value)) {
	  alert("Q&A sessions are monitored.\nPassing of email address or other contact details is a violation of our site rules which can result in a penalty charge.\nModify your question if you need to now.");
	  return false;
	} 	
	if (document.forms[0].qtext.value.length >2500){
	  alert("Please limit your question to 2500 characters. Currently it is "+ document.forms[0].qtext.value.length + " characters long.");
	  return false;
	}
	return true;
}

function showPic (obj) {
	var oldSrc = obj.src;
	if (oldSrc.indexOf("/show.asp?c=n&u=") < 0 ) {
		obj.src = "/show.asp?c=n&u=" + escape(oldSrc) ;
	}
}

function open_win( w_url ) {
	window.open( 'show.asp?u=' + escape(w_url), '_blank', 'directories=0,height=350,width=500,status=1,location=0,resizable=1,menubar=1,scrollbars=1,toolbar=0');
}	

function fixImgWidth (obj, wdth) {
	if (obj.width && (1*obj.width>wdth)) {
		obj.width=wdth;	
	}	
}

function glPopUp(url, name, attr) {
	var newwindow=window.open(url,name,'toolbar=no,scrollbars=yes,resizable=yes,'+attr);
	if (window.focus) {newwindow.focus()}
	return false;
}
