
function openWin(url, add_width, add_height) {
	if (typeof(add_width) == 'undef' || isNaN(add_width)) {
		add_width = 0;	
	}
	if (typeof(add_height) == 'undef' || isNaN(add_height)) {
		add_height = 0;	
	}
	var width = 650 + add_width;
	var height = 300 + add_height;
	var handle = window.open(
		url,
		"gDoc",
		"screenX=200,left=50,screenY=200,top=150,width=" + width + ",height="+height+",toolbar=no,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no,dependent=yes");
	handle.opener = self;
	handle.focus();
}

var nav4 = window.Event ? true : false;

function go_to_next(e, next_control) {
	if (nav4) var whichCode = e.which;
	else if (e.type == "keypress") var whichCode = e.keyCode;

	if (whichCode == 13) {
		if (typeof(next_control) != 'undefined' && next_control.enabled)
			next_control.focus();
		return false;
	}
}

function replace(pAmount,pFirst,pSecond)  {
	str = new String();
	str.value="";
	for(i=0;i<eval(pAmount.length);i++)
		if(pAmount.charAt(i)==pFirst)
			str.value += pSecond;
		else
			str.value += pAmount.charAt(i);
	return str.value;
}

function to_money (p) {
	var int_num = p*100;
	var rounded_num = Math.round(int_num);

	//fix bug in rounding
	if ((int_num - rounded_num) >= 0.4999999&&(int_num - rounded_num) <= 0.5000001){
		int_num = rounded_num+1;
	}
	else {
		int_num = rounded_num;
	}	

	var s = new String(int_num/100);
	if (s.indexOf('.')==-1 ) return s+".00";
	if (s.indexOf('.')== s.length-2) return s+"0";
	return s;
}

function jscheckdate(day,month,year) {
        var dd = new Date (year*1,month-1,day*1);
        if ( (day*1)!=(dd.getDate()*1) || (month*1)!=(dd.getMonth()*1+1) || (year*1)!=(dd.getFullYear()*1) ||	
             !(year>1900 && year <= 2017) ) {
           return -1;
        }

	return 0;
}

function trim(string) {
	trexf = /^\s+/;
	trexb = /\s+$/;
	str = new String(string);
	str = str.replace(trexf,'');  
	str = str.replace(trexb,'');
	return str.valueOf();
}

function showDoc(url,width,height) {
	if ( !width ) width=225;
	if ( !height ) height=170;
	var handle = window.open(url, "gDoc", "screenX=430,left=430,screenY=115,top=115,width="+width+",height="+height+",toolbar=no,directories=no,status=no,scrollbars=no,resizable=yes,menubar=no,dependent=yes");
	handle.focus();
}


function isValidCreditCardNumber(ccNum, ccType, type)
{
  if (!LuhnCheck(ccNum, type) || !validateCCNum(ccType,ccNum) )
  {
    return false;
  }
  return true;
}

function isValidCreditCardExpiryDate(ccExpMonth, ccExpYear, strictMonthCheck)
{

  var month = ccExpMonth;
  if ( !strictMonthCheck ) { 
   month %= 12;
  } else {
    if ( month >= 12 || month < 0 ) {
        return false;
    }
  }
  

  
  var year  = ccExpYear;
  if ( month == 0) {
    year ++;
  }
  var exp_date = new Date(year*1, month, 1);
  
  if ( exp_date == 'Invalid Date' ) {
    return false;
  }
  
  var sysdate  = new Date();
  if ( sysdate > exp_date)
  {
    return false;
  }
  return true;
}

function isValidCreditCardStartDate(ccStartMonth, ccStartYear, strictMonthCheck)
{
  var month = ccStartMonth;
  if ( !strictMonthCheck ) { 
   month %= 12;
  } else {
    if ( month > 12 || month < 0 ) {
        return false;
    }
  }
  
  var year  = ccStartYear;
  if ( month == 0) {
    year ++;
  }
  var start_date = new Date(year*1, month, 1);
  
  if ( start_date == 'Invalid Date' ) {
    return false;
  }
    
  var sysdate  = new Date();
  if ( sysdate < start_date)
  {
    return false;
  }
  return true;
}

function LuhnCheck(str, type)
{
  var result = true;
  if ( type == 'T') {
	  return true;
  }

  var sum = 0;
  var mul = 1;
  var strLen = str.length;

  for (i = 0; i < strLen; i++)
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0)
    result = false;

  return result;
}

function to_4digit_year(year_2digit) {
    var year = year_2digit*1;
    if ( year < 10 ) {
        year = "0".concat(year);
    }
    var res = "20".concat(year);
    return res;
}

function validateCCNum(cardType,cardNum)
{
        var result = false;
        cardType = cardType.toUpperCase();
        var cardLen = cardNum.length;
        var firstdig = cardNum.substring(0,1);
        var seconddig = cardNum.substring(1,2);
        var first4digs = cardNum.substring(0,4);
        switch (cardType)
        {
                case "VISA":
                        result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
                        break;
                case "AMEX":
                        var validNums = "47";
                        result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
                        break;
                case "MASTERCARD":
                        var validNums = "12345";
                        result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
                        break;
                case "DISCOVER":
                        result = (cardLen == 16) && (first4digs == "6011");
                        break;
                case "DINERS":
                        var validNums = "068";
                        result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
                        break;
                case "LASER":
						var first2dig = firstdig + seconddig;
                        result = (first2dig == "56" || first2dig == "63" || first2dig == "67") && (cardLen == 16 || cardLen == 18 || cardLen == 19);
                        break;
                case "SWITCH":
						var first2dig = firstdig + seconddig;
                        result = (first2dig == "49" || first2dig == "63" || first2dig == "67") && (cardLen == 16 || cardLen == 18 || cardLen == 19);
                        break;
                case "MAESTRO":
						var first2dig = firstdig + seconddig;
                        result = (firstdig == "5" || firstdig == "6") && (cardLen == 16 || cardLen == 17 || cardLen == 18 || cardLen == 19);
                        break;
                case "SOLO":
						var first2dig = firstdig + seconddig;
                        result = (first2dig == "63" || first2dig == "67") && (cardLen == 16 || cardLen == 18 || cardLen == 19);
                        break;
                case "JCB":
                        var min = 3528;
						var max = 3589;
                        result = (cardLen == 16) && (first4digs*1 <= 3589) && (first4digs*1 >= 3528);
                        break;
                case "CARTEBLEUE":
                		result = true;
                		break;
        }
        return result;
}

function setCursorInTextAt(myField, pos) {
  //IE support
  if (document.selection) {

  var r = myField.createTextRange()
     r.move("character",pos)
     r.select();
    }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') {
		myField.selectionStart = pos - 1;     
		myField.selectionEnd =myField.selectionStart;
  }
} 

function hasNonAlphaSymb(field) {
		var reg = /^\w+((-|,|\.|\'|\s)*\w+)*\.?$/;
		var asciiSymbs = substituteWideCharsWith(field.value, ''); //clear non ascii symbols for regexps in iexlplorer does not work with non ascii symbols
		if ( trim(asciiSymbs).length == 0 ) {
				return 0;
		}
		if (!reg.test(asciiSymbs)) {
				return 1;
		}
		return 0;
}
function substituteWideCharsWith (str, replaceWith) {
	var len = str.length;
	var res = "";
	for (var i = 0; i < len; i ++) {
		if ( str.charCodeAt(i) > 127 ) {
			res =  res + replaceWith; 
		} else {
			res = res + str.charAt(i);
		}		
	}
	return res;
}

function checkPost( post, country ){ //check postcode format is valid
	if ( country == "GBR") {
		ukPost = post;
		size = ukPost.length;
		
		if (size < 5 || size > 7){ //Code length rule
			return false;
		}
		
		if (!(isNaN(ukPost.charAt(0)))){ //leftmost character must be alpha character rule
			return false;
		}
		
		if (isNaN(ukPost.charAt(size-3))){ //first character of inward code must be numeric rule
			return false;
		}
		
		if (!(isNaN(ukPost.charAt(size-2)))){ //second character of inward code must be alpha rule
			return false;
		}
	
		if (!(isNaN(ukPost.charAt(size-1)))){ //third character of inward code must be alpha rule
			return false;
		}
	}
	return true;
}

function select_option(fld, key) {
	for ( var i = 0; i < fld.options.length; i++ ) {
		if ( fld.options[i].value == key ) {
			fld.options[i].selected = true;
		}
	}
}

<!-- Script Size:  3.78 KB -->
//always save in unicode format!!!
var mbl_num_regexp = /^\+(\d{1,3}) +(\d+)$/;
var amount_regexp = /^[1-9]\d*([.,]\d+)?|\d+[.,]\d+$/;

function do_nothing(){};

function generic_submit_form(form, field, value) {
	if ( field != null && typeof(field) != 'undefined')
		field.value = value; 
	
	if ( !form.onsubmit ) {
		form.submit(); 
	} else { 
		if ( form.onsubmit() ) { 
			form.submit(); 
		} else {  
			if ( field != null && typeof(field) != 'undefined')
				field.value = ''; 
		}
	}
	
	return false;
}

function bold_div(div_name) {
	var tmp = getObj(div_name);
	if (tmp != null && typeof(tmp) != 'undefined') {
		tmp.style.fontWeight = "bold";
	}
}


function unbold_div(div_name) {
	var tmp = getObj(div_name);
	if (tmp != null && typeof(tmp) != 'undefined') {
		tmp.style.fontWeight = "normal";
	}
}

function remove_useles_chars_from_post(code) {
	code.value=code.value.replace(/-/g, "");
}

function is_number(num) {
	var lReg=/^\d{0,19}(\.\d{1,6})?$/;
	if ( !lReg.test(num) || isNaN(num) ) {
	    return false;
	}
	return true;
}
// Email may have consecutive "-", "_", or "."
// Email domain must end with >= 2 letters and must have at least one subdomain
//
function normal_email_check(email) {
    if (/^\w+[\.\-\w]*@\w+(?:[\.-]?\w+)*(?:\.[a-zA-Z]{2,}?)+$/.test(email)){
	return (true)
    }

    return (false)
}

// Email can't have consecutive "-" or "."
// Email must end with >= 2 letters and must have at least one subdomain
//
function strict_email_check(email) {
    if (/^\w+(?:[\.-]?\w+)*@\w+(?:[\.-]?\w+)*(?:\.[a-zA-Z]{2,}?)+$/.test(email)){
	return (true)
    }

    return (false)
}

function popup(text, width) {
      p = '<table class="hint" border="0" cellspacing="0" cellpadding="0">'
      p+=     '<tr><th>' + text + '</th></tr>'
      p+=     '<tr><td><img height="10" align="top" src="/images/query_bg_bot3.gif"></td></tr>'
      p+= '</table>'
      return p;
}

function disable_button(el, disabled_action) {
    if (typeof(el) != 'undefined') {
        el.className = el.className + " button_disabled"
        el.disabled = true;
    }
}

function enable_button(el, enable_action, enable_href) {
    if (typeof(el) != 'undefined') {
        el.className = el.className.replace(/button_disabled/g, "");
        el.disabled = false;
    }
}
var owidth=200, iwidth, ooffsetx=13, ooffsety=(ns4)?-20:10, ox=0, oy=0, our=null, html='', delay=250, g_timeout, g_on=false, tr_style = 'table-row'

// see jquery and prototype how they do it.
var DOM2=document.getElementById
var AgentName = navigator.userAgent;

var ie4 = (document.all)?true:false
var ns4 = false;//drop support for ns4
var ie5, ie6, ie7
if (ie4) {
  if (navigator.userAgent.indexOf('MSIE 5')>0) ie5=true
  if (navigator.userAgent.indexOf('MSIE 6')>0) ie6=true
  if (navigator.userAgent.indexOf('MSIE 7')>0) ie7=true
} else ie5 = ie6 = false

var ie=ie4||ie5||ie6||ie7
var opera = !!window.opera
var ns6 = ( navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1 )
        ||( navigator.userAgent.indexOf('AppleWebKit/') > -1)
//treat safari as ns6... too much legacy code refering to 'ns' :)
ns6 = (ns6 && DOM2 && !ie && !opera)?true:false
if (/^Opera\/9\.5/.test(AgentName)) {
//treat opera9.5x as ns6...
	ns6 = true;
}
else if (/^Opera/.test(AgentName)) {
//... and all the other Opera versions should best match ie
	ie = true;
}
var ns=ns6||ns4

if((ns)||(ie)){
	document.onmousemove=mouseMove
	if(ns4) document.captureEvents(Event.MOUSEMOVE)
	if (ie && !opera) tr_style = 'block'
} else {
	ourlib=no_lib
	nd=no_lib
}

function no_lib () {
	return true
}

function lwr (what, where) {
// Commented line below is inserting parasite space character in places that it should'n be
// Example: /app/download.pl?posted=destination 
//			->> after bank fee when you select radiobutton

//	what+="\n";

    if (!what) what = '&nbsp;';		// insert space if no text to make sure that design will be ok

    if (DOM2) document.getElementById(where).innerHTML = what
		else if (ns) {var l=document.layers[where].document; l.open(); l.write(what); l.close();} 
			else document.all[where].innerHTML = what
}

function lwr_text (what, where) {
// Commented for the same reason like function "lwr"  !!!

//	what+="\n";

    if (!what) what = '&nbsp;';		// insert space if no text to make sure that design will be ok

	if (ns6) {
	  var HTMLElement = document.getElementById(where);
	  if(typeof HTMLElement!="undefined") {
		  var parsedText = document.createTextNode(what);
		  HTMLElement.innerHTML = "";
		  HTMLElement.appendChild( parsedText );
	  }
	} else if (DOM2) document.getElementById(where).innerText = what
	else if (ns) {var l=document.layers[where].document; l.open(); l.write(what); l.close();} 
	else document.all[where].innerText = what
}

//move div above to fit the page
function dontOverflow ( divName ) {
		var div = getObj(divName);

		var divBottom = getBottom(div);
		var clientHeight;
		if (document.body) {
			clientHeight = document.body.clientHeight
		} else if (document.documentElement && document.documentElement.clientHeight) {
			clientHeight = document.documentElement.clientHeight;
		} 

		var divHeight = divBottom - getTop(div);
		var newOy = oy;
		if ( clientHeight < oy + divHeight ) {						
				newOy = clientHeight - divHeight
		} 			
		repositionTo(getObject(divName), null, newOy);//internet explorer keeps vertical distance and must be reset even if there is no overflow, because there could be overflow of the same div before
}

function ourlib_delayed () {
	if (typeof(g_timeout) != 'undefined' && g_timeout) clearTimeout(g_timeout) 
	lwr(html, 'ourDiv')
	if (our) { 
		showObject(our)	
		dontOverflow('ourDiv');
	}
}


function ourlib (p_html, ofs_x, ofs_y) {
  if(ns4) our=document.ourDiv;
  if(ie) our=ourDiv.style;
  if(ns6) our=document.getElementById("ourDiv");
  if (ofs_x) ooffsetx = ofs_x; else ooffsetx=13;
  if (ofs_y) ooffsety = ofs_y; else ooffsety=10;
	repositionTo(our, ox, oy)
	dontOverflow('ourDiv');	
	html=p_html
	if (typeof(g_timeout) != 'undefined' && g_timeout) clearTimeout(g_timeout)
	g_timeout=setTimeout('ourlib_delayed()', delay)
	g_on=true
}

function nd () {
	if (typeof(g_timeout) != 'undefined' && g_timeout) clearTimeout(g_timeout)
	if (our) {
		hideObject(our)
		delete our
	}
	g_on=false
	return true
}

function getObject(obj_id) {
  var obj;
  // alert('searching for ' + obj_id);
  if(ns4) obj=eval('document.'+obj_id);
  if(ie) { 
	try {
	  obj = eval(obj_id+'.style');
	} catch (e) { return; };
  }
  if(ns6) obj=document.getElementById(obj_id);
  //  alert('found a ' + typeof(obj_id));
  return obj;
}

function getObj(obj_id) {
  var obj;
  if(ns4) obj=eval('document.'+obj_id);
  if(ie) { 
	try {
	  obj = eval('document.all.'+obj_id);
	} catch (e) { return; };
  }
  if(ns6) obj=document.getElementById(obj_id);
  return obj;
}

function hideObject(obj, no_display){
  if (typeof(obj) == 'undefined' || obj == null ) return false;
  if (ie) { obj.visibility="hidden"; if (!no_display) obj.display = 'none'; }
  else if (ns6||DOM2) { obj.style.visibility="hidden"; if (!no_display) obj.style.display = 'none'; }
  else if (ns4) obj.visibility="hide"
}

function showObject(obj, no_display, d_style){
  if (typeof(obj) == 'undefined' || obj == null ) return false;
  if (!d_style) d_style = 'block';
   if (ie) { obj.visibility="visible"; if (!no_display) obj.display = d_style; }
  else if (ns6||DOM2) { obj.style.visibility="visible"; if (!no_display) obj.style.display = d_style; }
  else if (ns4) obj.visibility="show"
}

function repositionTo(obj,xL,yL){
	if(ns6){
		if ( xL != null )
			obj.style.left=xL + "px"
		if ( yL != null )
			obj.style.top=yL+ "px"
	} else if ((ns4)||(ie)) {
		if ( xL != null )
			obj.left=xL
		if ( yL != null )
			obj.top=yL
	}
}

function placeLayer(){
	var placeX, placeY
	var winoffset = 0;
	if ( ie ) {
		if ( !document.body ) {
			return;
		}
		winoffset=document.body.scrollLeft;
	} else {
		winoffset=window.pageXOffset;
	}

	if (document.documentElement && document.documentElement.clientWidth) {
		iwidth=document.documentElement.clientWidth
	} else if (document.body && document.body.clientWidth) {
		iwidth=document.body.clientWidth
	}		

	if (ns4) iwidth=innerWidth
	if (ns6) iwidth=outerWidth
	
	placeX=ox+ooffsetx
	if((eval(placeX)+ eval(owidth))>(winoffset + iwidth)){
		placeX=iwidth + winoffset - owidth
		if(placeX<0) placeX=0
	}
	
	var scrolloffset=(ie4)? document.body.scrollTop : window.pageYOffset
	placeY=oy + ooffsety
	if (g_on) {		
		repositionTo(our, placeX, placeY)
		dontOverflow('ourDiv');		
	}
}

function mouseMove(e){
	if (ns){
		ox=e.pageX
		oy=e.pageY
	}

	if (ie4) {
		ox=event.x
		oy=event.y
	}

	if ( ie ) {
		ox=event.x+document.documentElement.scrollLeft
		oy=event.y+document.documentElement.scrollTop
	}
	placeLayer()
}

function hide_row(id) {
	var obj;
	var i = 0;
  
	while ( typeof(obj = getObject(id+i)) != 'undefined' && obj ) {
		hideObject(getObject(id+i));
		i++;
	}
}

function show_row(id) {
	var obj;
	var i = 0;
  
	while ( typeof(obj = getObject(id+i)) != 'undefined' && obj ) {
		showObject(getObject(id+i), false, ie?'block':'table-row');
		i++;
	}
}

function getTop(element) {
    var valueT = 0;
    do {
      valueT += element.offsetTop  || 0;
      element = element.offsetParent;
    } while (element);
	return valueT;
}


function getBottom(element) {
    var valueB = getTop(element);
	return valueB + element.clientHeight;
}


function push_anchor_to_id (id, anchor) {
	if ( typeof(hint_anchors) != 'undefined' ) {
		for ( var i = 0; i < hint_anchors.length; i++ ) {
			if ( id == hint_anchors[i].id) {
				if ( typeof(hint_anchors[i].anchors) == 'undefined' ) {
					hint_anchors[i].anchors = new Array();
				}
			
				hint_anchors[i].anchors.push(anchor);
			}
		}
	}	
}

function q_visible (element) {	
	var max_loop_cnt = 100;
	var loop_cnt = 0;
    while ( element.parentNode != null) {
	  loop_cnt ++;
	  if ( element.style.visibility == 'hidden' || loop_cnt > max_loop_cnt) {
		return false;
	  }	 	
      element = element.parentNode;
	}

	return true;	
}

function repositionHints() {
	if ( typeof(hint_anchors) != 'undefined' ) {
		var maxBottom = 0;
		for ( var i = 0; i < hint_anchors.length; i++ ) {
		    var el = getObject(hint_anchors[i].id);
			
			if ( !el ) {//if element is not found, skip iteration
				continue;
			}
			
			var anchor = undefined;
			if ( typeof(hint_anchors[i].anchors) != 'undefined' ) { //if elements is attached to more than one anchors, position the element next to the virst visible anchor
				for ( var j = 0; j < hint_anchors[i].anchors.length; j ++) {
					var tmp_anchor = getObj((hint_anchors[i].anchors)[j]);
					if ( typeof(tmp_anchor) != 'undefined' && tmp_anchor != null )
					  if ( q_visible(tmp_anchor) ) {
						anchor = tmp_anchor;
						break;
  					  }
				}
			} else {
				anchor = getObj(hint_anchors[i].id + '_anchor');
			}

			var depend_offset = 0;
			if ( typeof(hint_anchors[i].depend_on) != 'undefined' ) {
				var depend_on_obj = getObj(hint_anchors[i].depend_on);
				if ( typeof(depend_on_obj) != 'undefined' && depend_on_obj != null ) {
					depend_offset = getBottom(depend_on_obj);				
				}
			} else 	if ( typeof(hint_anchors[i].depend_on_array) != 'undefined' ) {
				for ( var j = 0; j < hint_anchors[i].depend_on_array.length; j ++) {
					var tmp_depend = getObj((hint_anchors[i].depend_on_array)[j]);
					if ( typeof(tmp_depend) != 'undefined' && tmp_depend != null )
					  if ( q_visible(tmp_depend) ) {
						depend_offset = getBottom(tmp_depend);				
						break;
  					  }
				}				
			}

			var elObj = getObj(hint_anchors[i].id);
			
			if ( typeof(anchor) == 'undefined' ) {
				hideObject(el);
			} else {
				var pos = Math.max(getTop(anchor) + hint_anchors[i].offset, depend_offset);
				var offset = pos + hint_anchors[i].offset;
				if ( hint_anchors[i].show_on_reposition ) {
					showObject(el);
				}
				repositionTo(el, null, offset);
				if ( q_visible(elObj) ) {//maxbottom must be calculated only if element is visible
					maxBottom = Math.max(maxBottom, getBottom(elObj));
				}
			}	
		}

		var rightColumn = getObj('left_right_r');
		var centerColumn = getObj('left_right_l');		
		if ( rightColumn != null && centerColumn != null) {
			if ( getBottom(centerColumn) < maxBottom ) {
				rightColumn.style.height = (maxBottom - getTop(rightColumn)) + "px";
			}
		}
	}
}

