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 to_money_cut (amount) {
	var number = amount * 1;
	if (isNaN(number)) return amount;
	var strobj = new String(amount);
	strobj = strobj.replace(/(\.\d\d)\d*$/,'$1').replace(/\.$/,'');
	if (strobj.indexOf('.') == -1 ) return strobj + '.00';
	if (strobj.indexOf('.') == strobj.length - 2) return strobj + '0';
	return strobj;
}

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 check_18_years ( day, month, year ) {
    var dateToCheck = new Date( year, month - 1, day );
    var eighteenYearsAgo = new Date();

    eighteenYearsAgo.setYear( eighteenYearsAgo.getFullYear() - 18 );
    eighteenYearsAgo.setHours( 0 );
    eighteenYearsAgo.setMinutes( 0 );
    eighteenYearsAgo.setSeconds( 1 );

    if ( dateToCheck.getTime() > eighteenYearsAgo.getTime() ) {
        return false;
    }

    return true;
}

function check_valid_dob ( objForm, msgValidDate, msgUnder18 ) {
    var day   = objForm.txtBDay.value,
        month = objForm.txtBMonth.value,
        year  = objForm.txtBYear.value
    ;

    if ( jscheckdate( day, month, year ) != 0 ) {
        alert( msgValidDate );
        objForm.txtBDay.focus();
        return false;
    }

    if ( ! check_18_years( day, month, year ) ) {
        alert( msgUnder18 );
        return false;
    }

    return true;
}

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();
  var cur_month = new Date(sysdate.getFullYear(), sysdate.getMonth(), 1);
  if (cur_month > 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(/[-\s]/g, "");
}

function is_number(num) {
	var lReg=/^\d{0,19}(\.\d{1,6})?$/;
	if ( !lReg.test(num) || isNaN(num) ) {
	    return false;
	}
	return true;
}

