/* general.js */


var imgLoading = new Image();
imgLoading.src = '/loading_big.gif';
//var sending_loader = '<div style="padding:10px;text-align:center;"><img src="'+imgLoading.src+'" /><br />אנא המתן ...</div>';
var sending_loader = '<div style="padding:10px;text-align:center;"><img src="'+imgLoading.src+'" /><br /> ... </div>';


var StacTinyBox = {
	Over : {id: 'tiny-over'},
	Load : {id: 'tiny-load'},
	Tbox : {id: 'tiny-tbox'},
	Tret : {id: 'tiny-return'},
	Url : '/send.php'
};

var StacPixels = {};


function bg_send_lead(frm_id)
{
	StacTinyBox.Rform = frm_id;
	StacTinyBox.Params = $(frm_id).serialize(true);
	TinyBox_show();
}

function TinyBox_show()
{
	if (Prototype.Browser.IE) {
		$$('select').invoke('setStyle', {visibility: 'hidden'});
		StacTinyBox.scroll = document.viewport.getScrollOffsets();
		window.scrollTo(0, 0);
		$$('html, body').invoke('addClassName','tiny-ie-hack');

	}
	$$('.div_flash_obj').invoke('hide');
	$('btn_send_confirm').enable();
	$(StacTinyBox.Tbox.id).setStyle({left: Math.round((Element.getWidth(document.body) - Element.getWidth(StacTinyBox.Tbox.id)) / 2 ) + "px"});

	Element.show(StacTinyBox.Over.id);
	new Ajax.Updater(StacTinyBox.Tret.id, StacTinyBox.Url, {
		parameters: StacTinyBox.Params,
		onCreate  : function() {
			 Element.show(StacTinyBox.Load.id);
		},
		onSuccess: function() {
			Element.hide(StacTinyBox.Load.id);
			new Effect.Fade(StacTinyBox.Tbox.id, {
				from: 0.1, to : 0.9, duration : 0.5,
				beforeStart : function() { Element.show(StacTinyBox.Tbox.id); }
			});
/* removed in favor of direct calling for pixels in send.php
			if (StacTinyBox.Params['phone1'].length>0 && StacTinyBox.Params['name'].length>0 )
			{
				remoteCall( {uri: '/pixels.php?p='+StacTinyBox.Params['p']});
			}
*/
			window.setTimeout(function(){
					if ($(StacTinyBox.Tret.id).innerHTML.length == 0) {
						$('btn_send_no_confirm').value='Close';
						if ($('label_close_coreg_win')) {
							$('btn_send_no_confirm').value = $('label_close_coreg_win').value;
						}
						$('btn_send_confirm').remove();
					}
			}, 100);

		}
	});



}

function send_coreg()
{
	var coregs = $$('.coreg_list_checks');
	var n_checks = 0;

	StacTinyBox.Params['coreg']=1;
    StacTinyBox.Params['real_p']=StacTinyBox.Params['p'];
	StacTinyBox.Params['p']=2232;
	StacTinyBox.Params['multiple_ad_ids']='';
	//console.log(StacTinyBox.Params);
	for (var i=0; i<coregs.length; i++) {
		if (coregs[i].checked == true) {

			StacTinyBox.Params['a']=coregs[i].value;
			new Ajax.Request('/send.php', {
				parameters: StacTinyBox.Params
			});

			n_checks++;
		}
	}

	return n_checks;
}

function redirecter() {
	TinyBox_hide();

	var myNewStr = new Template($F('thank_you_redirect_url'));
	var final_url = myNewStr.evaluate(StacTinyBox.Params);

    if(($('thank_you_redirect_parent') != null) && ($F('thank_you_redirect_parent') == '1'))
	    window.top.location=final_url;
    else
        window.location=final_url;
}

function TinyBox_confirm(_to_send)
{
	_to_send = _to_send || false;
	var timess = 1;
	if (_to_send)  timess = send_coreg();

	if ($('btn_send_confirm')) {
		$('btn_send_confirm').disable();
	}

	$(StacTinyBox.Tret.id).update(sending_loader);


	if ($('thank_you_redirect_url') && $F('thank_you_redirect_url').length) {
		setTimeout('redirecter()', 1000*timess);
	} else {
		setTimeout('TinyBox_hide()', 2000);
	}
}

function TinyBox_hide()
{
	if (Prototype.Browser.IE) {
		$$('html, body').invoke('removeClassName','tiny-ie-hack');
		window.scrollTo(StacTinyBox.scroll.left, StacTinyBox.scroll.top);
	}

	Effect.Fade(StacTinyBox.Tbox.id, {
		from : 0.9, to : 0.1, duration : 0.5,
		afterFinish : function() { Element.hide(StacTinyBox.Tbox.id); }
	});

	Element.hide(StacTinyBox.Over.id);

	if (Prototype.Browser.IE)	{
		$$('select').invoke('setStyle', {visibility: 'visible'});
	}
	$$('.div_flash_obj').invoke('show');
}



function remoteCall(listener)
{
	if (listener && listener.uri)
	{
		ifrm = document.getElementById("ifrm_remotecall");

		if (ifrm) {
			// .. delete it.
			ifrm.parentNode.removeChild(ifrm);
		}

		// new script element.
		ifrm = document.createElement("iframe");
		ifrm.setAttribute("id", "ifrm_remotecall");
		ifrm.setAttribute("src", listener.uri);
		ifrm.setAttribute("style", 'width:0px;height:0px');
		document.getElementsByTagName("body")[0].appendChild(ifrm);
	}
}

// This will stop image flickering in IE6 when elements with images are moved
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(e) {}

