 var ie4 = document.all;
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all; 
var opera = navigator.appName.indexOf('Opera') == -1 ? false : true;

function openNewWindow(URL, name, top, left, width, height) {
    if (name = ''){ name = '_blank'; }
	newwindow = window.open(URL, name, 
		'top=' + top + 
		',left=' + left + 
		',width=' + width + 
		',height=' + height + 
		',scrollbars=0,menubar=0,status=0,resizable=0,toolbar=0,directories=0,location=0'); 
	return newwindow; 
}

function openNewWindowNormal(URL, name, top, left, width, height) {
    if (name = ''){ name = '_blank'; }
	newwindow = window.open(URL, name, 
		'top=' + top + 
		',left=' + left + 
		',width=' + width + 
		',height=' + height + 
		',scrollbars=2,menubar=0,status=1,resizable=1,toolbar=0,directories=0,location=0'); 
	return newwindow; 
}
function openNewWindowNormalFullSize(URL, name) {
    if (name = ''){ name = '_blank'; }
    sizes = getWindowSize();
	newwindow = window.open(URL, name, 
		'top=0' +
		',left=0' + 
		',width=' + sizes[0] + 
		',height=' + sizes[1] + 
		',scrollbars=2,menubar=1,status=1,resizable=1,toolbar=1,directories=0,location=1'); 
	return newwindow; 
}

<!-- This code is in the public domain. Feel free to link back to http://jan.moesen.nu/ -->
function sprintf(){
	if (!arguments || arguments.length < 1 || !RegExp)
	{
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
		else
		{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length)
			{
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
			       if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			  else if (pPad) pad = pPad;
			var justifyRight = true;
			       if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
			       if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
			       if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
			       if (pType == 'b') subst = parseInt(param).toString(2);
			  else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
			  else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
			  else if (pType == 'u') subst = Math.abs(param);
			  else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			  else if (pType == 'o') subst = parseInt(param).toString(8);
			  else if (pType == 's') subst = param;
			  else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
			  else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

function clickRadio(obj, value){
    for(var i = 0; i < obj.length; i++){
        if (obj[i].value == value){
            obj[i].checked = true;
            break;
        }
    }
}

function hideId(id){
    if (document.getElementById(id)){
        document.getElementById(id).style.visibility = 'hidden';
        document.getElementById(id).style.display = 'none';
    }
}

function showId(id){
    if (document.getElementById(id)){
        document.getElementById(id).style.visibility = 'visible';
        document.getElementById(id).style.display = 'block';
    }
}

// тут я зачем-то повторил код свыше. мой мне кажется удобным :)
function showInfo(str, url) {
	if (typeof url === 'string') {
		var doc = window.open(url, 'infoUrl');
	} else {
		var doc = window.open('', 'info', 'left=800,top=10,height=450,width=400,scrollbars=0,menubar=0,status=0,resizable=0,toolbar=0,directories=0,location=0');
		doc.document.write('<div style="font-family: tahoma; text-align: justify; font-size: 12px;">' + str + '</div>');
		doc.document.write('<br/><a style="display: block; font-size: 12px; font-family: tahoma; text-align: center;" href="javascript:window.close();">Закрыть окно</a>');
	}
	return false;
}

// а вот теперь кажется я понял зачем
// отдаём id дива (или чего угодно!) в виде '#infoDiv', а получаем открытое окно, в котором содержимое дива, и окно подогнанно под размер дива.
function showInfoDiv(divID, minwidth) {
	var div = $(divID); 
	if (!div.get(0))
		return 0;
	var x = screen.width - div.width() - 50;
	var y = 10;
	var doc = 
		window.open('', 'info', 
				'left=' + x + 
				',top=' + y + 
				',height=' + (div.height() + 50) + 
				',width=' + (div.width() + 80) + 
				',scrollbars=1,menubar=0,status=0,resizable=0,toolbar=0,directories=0,location=0');
	doc.document.write('<div style="font-family: tahoma; text-align: justify; font-size: 12px;">' + div.html() + '</div>');
	doc.document.write('<br/><a style="display: block; font-size: 12px; font-family: tahoma; text-align: center;" href="javascript:window.close();">Закрыть окно</a>');
	return false;
}



function isValidDate(date_string, format) {
    var days = [0,31,28,31,30,31,30,31,31,30,31,30,31];
    var year, month, day, date_parts = null;
    var rtrn = false;
    var decisionTree = {
        'm/d/y':{
            're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{2}|\d{4})$/,
            'month': 1,'day': 2, year: 3
        },
        'mm/dd/yy':{
            're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{2})$/,
            'month': 1,'day': 2, year: 3
        },
        'mm/dd/yyyy':{
            're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{4})$/,
            'month': 1,'day': 2, year: 3
        },
        'y/m/d':{
            're':/^(\d{2}|\d{4})[./-](\d{1,2})[./-](\d{1,2})$/,
            'month': 2,'day': 3, year: 1
        },
        'yy/mm/dd':{
            're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{1,2})$/,
            'month': 2,'day': 3, year: 1
        },
        'yyyy/mm/dd':{
            're':/^(\d{4})[./-](\d{1,2})[./-](\d{1,2})$/,
            'month': 2,'day': 3, year: 1
        },
        'dd/mm/yyyy':{
            're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{4})$/,
            'month': 2,'day': 1, year: 3
        }
    };
    var test = decisionTree[format];
    if (test) {
        date_parts = date_string.match(test.re);
        if (date_parts) {
            year = date_parts[test.year];
            month = date_parts[test.month];
            day = date_parts[test.day];

            test = (month == 2 && 
                    isLeapYear() && 
                    29) || 
                    days[Number(month)] || 0;

            rtrn = 1 <= day && day <= test;
        }
    }

    function isLeapYear() {
        return (year % 4 != 0 ? false : 
            ( year % 100 != 0? true: 
            ( year % 1000 != 0? false : true)));
    }
    return rtrn;
}//eof isValidDate


function check_adv_form(){
	if ($('#fio').val() == '') {
		window.alert('Введите фамилию, имя и отчество контактной персоны.');
		$('#fio').focus();
		return false;
	}
	if ($('#email').val() == '') {
		window.alert('Введите e-mail для связи.');
		$('#email').focus();
		return false;
	}
	if ($('#siteurl').val() == '') {
		window.alert('Введите адрес Вашего сайта.');
		$('#siteurl').focus();
		return false;
	}
	if ($('[name=mission]').get(0) && $('[name=mission]:checked').val() == undefined) {
		window.alert('Выберите цель рекламной кампании.');
		return false;
	}
	return true;
}