//var ie4 = document.all ? 1:0;
//var ns6 = document.getElementById?1:0;
//var ns4 = document.layers?1:0;

try {
	document.domain = 'apollo.lv';
} catch(err){}

//----------------------------------------
function popupWindow (URL, w, h, resize, scroll)
{

	var day = new Date();
	var id = day.getTime();

	var str = "page" + id + '=window.open("' + URL + '","_blank","' +
	  'toolbar=no,location=no,status=no,menubar=0,statusbar=0,left=20,top=20,' +
      'resizable=' + resize + ',' + 'scrollbars=' + scroll + ',' + 'width=' + w + ',' + 'height=' + h + '");';

	eval (str); 
	
//    return "page" + id;
}

//----------------------------------------
function openPopup(url, width, height) {
	newwindow=window.open(url,'name','height='+height+',width='+width);
	if (window.focus) {newwindow.focus()}
	//return false;
}

//----------------------------------------
function openUnder (url)
{
	parentWindow = window.opener;
		
	if (parentWindow)
	{
		parentWindow.location=url;
		parentWindow.focus();
	}
	else
		window.location=url;
}	


//----------------------------------------
function resize_window (pic_name, def_w, def_h, add_w, add_h)
{
	el_pic = document.getElementById (pic_name);
	
	if (el_pic)
	{
		w = el_pic.width;
		h = el_pic.height;
		
		scr_h = screen.availHeight;
		scr_w = screen.availWidth;
	
//		alert ("width: " + scr_w + " height: " + scr_h);
//		alert ("width: " + w + " height: " + h);
		
		if (w + add_w > def_w)
			new_w = w + add_w + 0;
		else
			new_w = def_w;
			
		if (h + add_h > def_h)
			new_h = h + add_h + 0;
		else
			new_h = def_h;

		new_h = Math.min(new_h,scr_h);
		new_w = Math.min(new_w,scr_w);

//		alert ("width: " + new_w + " height: " + new_h);

		window.resizeTo (new_w, new_h);
	}
	else
	{
//		alert ('No pic!');
	}
}

//----------------------------------------
function SetCookie (name,value,expires,path,domain,secure)
{
	cookie = name + "=" + value +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	
//	alert (cookie);	
	document.cookie = cookie;
//	alert (document.cookie);
}

//----------------------------------------
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


//----------------------------------------
function confirm_delete (alert_str, form_name)
{
	if (form_name)
		the_form = document.forms[form_name];
	else
		the_form = document.forms[0];

//	var selectObject = document.forms[0].elements['action'];
	var selectObject = the_form.elements['action'];
	var selectValue  = selectObject.value;
	var is_confirmed = true;
	
	if (selectValue == 'delete')
		is_confirmed = confirm (alert_str);

	return is_confirmed;
}

//----------------------------------------
function confirm_ask (alert_str)
{
	is_confirmed = confirm (alert_str);
	return is_confirmed;
}


//----------------------------------------
function check_all (the_form, the_name, do_check)
{
	var		elts = (typeof (document.forms[the_form].elements[the_name]) != 'undefined')
				  ? document.forms[the_form].elements[the_name]
				  : 0;

	var 	elts_cnt  = (typeof (elts.length) != 'undefined')
				  ? elts.length
				  : 0;

	if (elts_cnt)
	{
		for (var i = 0; i < elts_cnt; i++)
		{
			elts[i].checked = do_check;
		}
	}
	else
	{
		elts.checked = do_check;
	}
	
	return (true);
}

//----------------------------------------
function validate_login_field (form_name, field_name)
{
	var re = /([^\.-_A-Za-z0-9])/g;
	var arg = document.forms[form_name].elements[field_name].value;
		
	var t = arg.replace(re,"");
	
	t = t.replace("@","");
	t = t.replace("eapollo.lv","");

	if (t.length > 20)
		t = t.substring(0,19);

	document.forms[form_name].elements[field_name].value = t;
}

//----------------------------------------
function validate_email_field (form_name, field_name)
{
	var re = /([^\@\.\-_A-Za-z0-9])/g;
	var arg = document.forms[form_name].elements[field_name].value;
	var t = arg.replace(re,"");
	if (t.length > 40)
		t = t.substring(0,39);
	document.forms[form_name].elements[field_name].value = t;
}

//----------------------------------------
function validate_phone_field (form_name, field_name)
{
	var re = /([^\- 0-9])/g;
	var arg = document.forms[form_name].elements[field_name].value;
	var t = arg.replace(re,"");
	if (t.length > 20)
		t = t.substring(0,19);
	document.forms[form_name].elements[field_name].value = t;
}


//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function XMLHTTP_request(xmlhttpO, url, handler, method, send) {
	xmlhttpO.open(method, url, true);
	xmlhttpO.onreadystatechange=handler;
	xmlhttpO.send(send);
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
	//------------------------------------------------------
	function wopen(url, name, w, h)
	{
  		// Fudge factors for window decoration space.
  		// In my tests these work well on all platforms & browsers.
  		w += 32;
  		h += 96;
  		wleft = (screen.width - w) / 2;
  		wtop = (screen.height - h) / 2;
  		var win = window.open(url,
    		name,
    		'width=' + w + ', height=' + h + ', ' +
    		'left=' + wleft + ', top=' + wtop + ', ' +
    		'location=no, menubar=no, ' +
    		'status=no, toolbar=no, scrollbars=no, resizable=no');
  		// Just in case width and height are ignored
  		win.resizeTo(w, h);
  		// Just in case left and top are ignored
  		win.moveTo(wleft, wtop);
  		win.focus();
	}
	
	var scroll_x = 0;
	var scroll_y = 0;
	var cant_scroll = false;
	var innerpopup_exists = false;
	var timerID;
	//------------------------------------------------------
	function onscroll_scroll_back(){
		if (window.addEventListener) {
			window.addEventListener("scroll",scroll_window, 1);
		}
		else if (window.attachEvent) {
			window.attachEvent("onscroll",scroll_window);
		}
		else if (document.getElementById) {
			window.onscroll=scroll_window;
		}
	}
	//------------------------------------------------------
	function scroll_window(){
		if (cant_scroll == true){
			window.scroll(scroll_x, scroll_y)
		}
	}
	//------------------------------------------------------
	function innerpopup_open(text){
		if (/\?/.test(text)==false)
			document.getElementById('innerpopup_frame').src = text + '?popup=1';
		else
			document.getElementById('innerpopup_frame').src = text + '&popup=1';
		scroll_x = parent.document.getElementsByTagName('html')[0].scrollLeft;
		scroll_y = parent.document.getElementsByTagName('html')[0].scrollTop;
		cant_scroll = true;
	}
	//------------------------------------------------------
	function innerpopup_close(){
		document.getElementById('innerpopup_table').src = '';
		document.getElementById('innerpopup_table').style.visibility = 'hidden';
		cant_scroll = false;
		clearTimeout(timerID);
	}
	//------------------------------------------------------
	function popup_show(){
		if (parent){
			parent.timerID = setTimeout("resize_popup_table()", 10);
			parent.document.getElementById('innerpopup_table').style.visibility = 'visible';
			parent.document.getElementById('innerpopup_table').style.top = parent.document.getElementsByTagName('html')[0].scrollTop+'px';
		}
	}
	//------------------------------------------------------
	function resize_popup_table(){
		if (parent){
			parent.document.getElementById('innerpopup_table').style.width = parent.document.getElementsByTagName('html')[0].clientWidth;
			parent.document.getElementById('innerpopup_table').style.height = parent.document.getElementsByTagName('html')[0].clientHeight;
			parent.timerID = setTimeout("resize_popup_table()", 100);
		}
	}

//------------------------------------------------------
function getXmlHttp(){
	var xmlHttp;
    try{
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }catch (e){
        // Internet Explorer
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            try{
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    return xmlHttp;
}
	 
//------------------------------------------------------
function ajaxLoadFile(filename){
    var xmlHttp;
    xmlHttp = getXmlHttp();
    xmlHttp.onreadystatechange=function(){
        if(xmlHttp.readyState==4){
            document.getElementById('ajax_' + filename).innerHTML=xmlHttp.responseText;
        }
        else {
            true;
        }
    }
    xmlHttp.open("GET",'/upload/ajax/' + filename + ".html",true);
    xmlHttp.send(null);
}


//------------------------------------------------------
function voteHomePool(poll, choice){
	// disable links
	var objs = document.getElementById("home_poll").getElementsByTagName("a");
	for (i=0;i<objs.length;i++) {
		objs[i].style.display = 'none';
	}
	// send results
	var xmlHttp;
	xmlHttp = getXmlHttp();
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			document.getElementById('home_poll').className = '';
			document.getElementById('home_poll').innerHTML = xmlHttp.responseText;
		}
		else {
			true;
		}
	}
	xmlHttp.open("GET",'/modules/poll_voting/poll_voting.php?poll=' + poll + "&choice=" + choice,true);
	xmlHttp.send(null);
	
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 3600000 * 24 * 1);
	SetCookie('home_poll_' + poll + '_voted', 1, expire);
	
	return false;
}

//------------------------------------------------------
function voteChanelPool(){
	poll = document.getElementById('POLL_ID').value;
	for( i = 0; i < document.poll_form.CHOICE_ID.length; i++ ){
		if( document.poll_form.CHOICE_ID[i].checked == true )
			choice = document.poll_form.CHOICE_ID[i].value;
	}
	cid = document.getElementById('C_ID').value;
	// disable button
	document.getElementById("subm").style.display = "none";
	
	// send results
	var xmlHttp;
    xmlHttp = getXmlHttp();
    xmlHttp.onreadystatechange=function(){
        if(xmlHttp.readyState==4){
        	document.getElementById('f_res').className = '';
            document.getElementById('f_res').innerHTML = xmlHttp.responseText;
        }
        else {
            true;
        }
    }
    xmlHttp.open("GET",'/modules/poll_voting/poll_voting.php?type=chanel&poll=' + poll + "&choice=" + choice + "&cid=" + cid,true);
    xmlHttp.send(null);
    
    var today = new Date();
    var expire = new Date();
    expire.setTime(today.getTime() + 3600000 * 24 * 1);
    SetCookie('apollo_poll_' + poll, "yes", expire);
    
    return false;
}

//------------------------------------------------------
function city24_upwg(aa){
    var t=0;
    var c=1;
    var q=0;
    aa=parseInt(aa);
    
    while(c < parseInt(aa+1))
    {
    	if(document.getElementById('a'+aa).style.top=='140px') 
        {return;} 
        else
        {
            t=parseInt(document.getElementById('a'+c).style.top)-2;
            if(t==4){q=1;}
            document.getElementById('a'+c).style.top=t+'px';
            c=parseInt(c+1);
         }
    }
    if(q==1) return;
    setTimeout("city24_upwg("+aa+")",20);
}

//------------------------------------------------------
function city24_downwg(aa){
    var t=0;
    var q=0;
    aa=parseInt(aa);
    var d=aa;
    while(d >0)
    {
        if(document.getElementById('a1').style.top=='4px')
        {return;}
        else
        {
            t=parseInt(document.getElementById('a'+d).style.top)+2;
            if(t==4){ q=1;}
            document.getElementById('a'+d).style.top=t+'px';
            d=parseInt(d-1);
            }
    }
    if(q==1) return;
    setTimeout("city24_downwg("+aa+")",20);
}

//------------------------------------------------------
function innerPopupOpen(type, where_to_go){
	if (!type) type = 1;
	if (type == 1) { // register window
		if (!where_to_go) where_to_go = '';
		SetCookie ('where_to_go',where_to_go,null,'/');
		document.getElementById('innerPopupContainer').style.display = 'block';
		innerPopupClose(2);
    innerPopupClose(3);
	} else if (type == 2) { // simple innerpopup
		innerPopupClose(1);
		innerPopupClose(2);
    innerPopupClose(3);
		document.getElementById('innerPopupFrame2').src = where_to_go;
	} else if (type == 3) { // simple innerpopup
		innerPopupClose(1);
		innerPopupClose(2);
    innerPopupClose(3);
		document.getElementById('innerPopupFrame3').src = where_to_go;
	}
}
//------------------------------------------------------
function innerPopupClose(type){
  var el;
	if (!type) type = 1;
	if (type == 1) {
    el = document.getElementById('innerPopupContainer');
    if (el) el.style.display = 'none';
	} else if (type == 2) {
    el = document.getElementById('innerPopupContainer2');
		if (el) el.style.display = 'none';
  } else if (type == 3) {
		el = document.getElementById('innerPopupContainer3');
    if (el) el.style.display = 'none';
  }
}

//-------------------------------------------------------
function openVankuveraSurvey(step, url){
    var xmlHttp;
    xmlHttp = getXmlHttp();
    if (step == 1) {
        xmlHttp.onreadystatechange=function(){
            if(xmlHttp.readyState==4){
                document.getElementById('survey_iframe').innerHTML=xmlHttp.responseText;
                document.getElementById('survey_iframe').style.display = 'block';
                document.getElementById('survey_close').onclick = function() {
                    document.getElementById('survey_iframe').style.display = 'none';
                    return false;
                };
                document.getElementById('survey_next').onclick = function() {
                    var answer = escape(document.getElementById('survey_answer').value);
                    var survey_id = document.getElementById('survey_id').value;
                    openVankuveraSurvey(2, '/portal/innerpopup/survey/'+survey_id+'/2?answer='+answer);
                    document.getElementById('survey_next').disabled = true;
                    document.getElementById('survey_close').disabled = true;
                    return false;
                };
            }
        }
    }
    else if (step == 2) {
        xmlHttp.onreadystatechange=function(){
            if(xmlHttp.readyState==4){
                document.getElementById('survey_iframe').innerHTML=xmlHttp.responseText;
                document.getElementById('survey_close').onclick = function() {
                    document.getElementById('survey_iframe').style.display = 'none';
                    return false;
                }
                document.getElementById('survey_next').onclick = function() {
                    var answer = escape(document.getElementById('survey_answer').value);
                    var survey_id = document.getElementById('survey_id').value; 
                    var sname = escape(trim(document.getElementById('s_user_name').value));
                    var ssname = escape(trim(document.getElementById('s_sur_name').value));
                    var semail = escape(trim(document.getElementById('s_email').value));
                    var sphone = escape(trim(document.getElementById('s_phone').value));
                    if (sname.length < 1 || ssname.length < 1 || semail.length < 1 || sphone.length < 1){
                        alert('Nav aizpildīti visi lauki!');
                        return false;
                    }
                    if (/^.+@.+\..{2,4}$/.test(semail)==false){
                        alert('Jūs ievadījās nepareizu e-pasta adresi!');
                        return false;
                    }
                    if (/\d{8}/.test(sphone)==false){
                        alert('Jūs ievadījās nepareizu tālruņa numuru! Mēs nevarēsim ar Jums sazināties.');
                        return false;
                    }
                    openVankuveraSurvey(3, '/portal/innerpopup/survey/'+survey_id+'/3?answer='+answer+'&uname='+sname+' '+ssname+'&uemail='+semail+'&uphone='+sphone);
                    document.getElementById('survey_next').disabled = true;
                    document.getElementById('survey_close').disabled = true;
                    return false;
                }
            }
        }
    }
    else if (step == 3) {
        xmlHttp.onreadystatechange=function(){
            if(xmlHttp.readyState==4){
                document.getElementById('survey_iframe').innerHTML=xmlHttp.responseText;
                document.getElementById('survey_close').onclick = function() {
                    document.getElementById('survey_iframe').style.display = 'none';
                    return false;
                }
            }
        }
    }
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return false;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
