// JavaScript Document
var OpenedFrame = '';
var xmlHttp;
var DivID;
var SecondURL = '';
var SecondDivID = '';
var isIE = document.all;
var IE = document.all ? true : false;
var tempX = 0;
var tempY = 0;

//  ---------------------------------------
// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
    for( var i = +b || 0, l = this.length; i < l; i++ ) {
        if( this[i]===v || s && this[i]==v ) {
            return i;
        }
    }
    return -1;
};

function setFullScreen(objID)
{
    var oFrame;
    var SearchPanel;
    var xWidth;
    var obj = document.getElementById(objID);
    obj.width = "95%";
    obj.height = "98%" ;

}

//  ---------------------------------------

function ResizeBody()
{
    var oBody;
    var oFrame;
    var SearchPanel;
    var xWidth;
    oFrame	 = document.body;
    xWidth = oFrame.scrollWidth + (oFrame.offsetWidth - oFrame.clientWidth) - 20;
    if (xWidth < 999)
    {
        xWidth = 999;
    }
    if (objectExists(document.getElementById("tDataArea")))
    {
        oBody	 = 	document.getElementById("tDataArea");
        oBody.style.width = xWidth + 'px';
    }
    if (objectExists(document.getElementById("SearchPanel")))
    {
        oBody	 = 	document.getElementById("SearchPanel");
        oBody.style.width = xWidth + 'px';
    }
}

//  ---------------------------------------


function CloseMe()

{
    var fObj;


    // fObj = top.document.getElementById(this.name);
    fObj = parent.document.getElementById(this.name);

    // alert(fObj.name);
    // alert(fObj.style.zIndex);
    fObj.height = 1;
    try
    {
        this.parent.location.href = this.parent.location.href;
    }
    catch(e)
    {
        window.status = 	'Error: ' + e.number + '; ' + e.description;
    }

}

//  ---------------------------------------

function loadHTMLFragment(elemId, HTMLFragment)
{
    var el;
    if (document &&
        document.getElementById &&
        document.getElementById(elemId) &&
        document.createElement &&
        document.appendChild &&
        document.getElementsByTagName)
        {
        el = document.getElementById(elemId);
        if(isIE)
        {
            HTMLFragment = "&nbsp;" + HTMLFragment;
        // The & nbsp; is a hack to cause IE to process the
        // script elements if the first node in the
        // HTMLFragment is a script element.
        }
        el.innerHTML = HTMLFragment;
        var d = el.getElementsByTagName('script');
        var t = d.length;
        var newScript ;
        var s;
        var OldScript;
        for (var x = 0; x < t; x ++ )
        {
            newScript = document.createElement('script');
            newScript.type = "text/javascript";
            if(d[x].src)
            {
                newScript.src = d[x].src;
            }
            else
            {
                if(isIE)
                {
                    newScript.text = d[x].text;
                }
                else
                {
                    s = document.createTextNode(d[x].text);
                    newScript.appendChild(s);
                }
            }
            el.appendChild(newScript);
        }
        for (var y = 0; y < t; )
        {
            OldScript = el.getElementsByTagName("script")[0];
            //    el.removeChild(OldScript);
            OldScript.parentNode.removeChild(OldScript);
            y ++ ;
        }
    }

    document.documentElement.style.cursor = "auto";

}

//  ---------------------------------------


function viewFrame(fObjID, fType, url)
{
    var fObj;
    try
    {

        if ( OpenedFrame != fObjID && OpenedFrame !== '')
        {
            fObj = document.getElementById(OpenedFrame);
            fObj.height = 1;
        }
        OpenedFrame = fObjID;
        fObj = document.getElementById(fObjID);

        if ( self.frames[fObjID].location.href.indexOf(url) == - 1)
        {
            fObj.height = 750 ;
            //     reSize(fObj);
            self.frames[fObjID].location.href = url;
        }
        else
        {
            if (fObj.height == 750)
            {
                fObj.height = 1;
            }
            else
            {
                fObj.height = 750 ;
            }
        }
    }
    catch(e)
    {
        window.status = 	'Error: ' + e.number + '; ' + e.description;
    }

}

//  ---------------------------------------

function showInfoSPost(URL, ID, params, OnStateChanged)
{
    //debugger;
    DivID = ID;
    document.getElementById(DivID).innerHTML = 'Please wait ...';
    document.documentElement.style.cursor = "wait";
    xmlHttp = GetXmlHttpObject();
    document.getElementById(DivID).innerHTML = "Please Wait";
    xmlHttp.open("POST", URL, false);

    // Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    // document.documentElement.style.cursor = "auto";

    xmlHttp.send(params);

    if (typeof(OnStateChanged) == 'undefined')
    {
        OnStateChanged = 'stateChanged()';
    }
    eval(OnStateChanged);

}

//  ---------------------------------------


function showInfoS(URL, ID, OnStateChanged)
{
    document.documentElement.style.cursor = "wait";
    DivID = ID;
    if (document.getElementById(DivID)== null) {
        var newDiv = document.createElement('div')
        var target = document.getElementsByTagName('BODY')[0];
        newDiv.id = DivID;
        newDiv.style.position = "fixed";
        newDiv.style.left = "50%";
        newDiv.style.top = "50%";
        newDiv.style.margin = "-150px 0 0 -250px";
        target.appendChild(newDiv);
	   
    }
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp === null)
    {
        alert (" Your browser does not support AJAX!");
        return;
    }
    // 			if (typeof(OnStateChanged) == 'undefined')
    // 			  {
    // 				  OnStateChanged = 'stateChanged';
    // 			  }
    //            xmlHttp.onreadystatechange = eval(OnStateChanged);
    document.getElementById(DivID).innerHTML = "Please Wait";
    xmlHttp.open("GET", URL, false);
    xmlHttp.send(null);

    if (typeof(OnStateChanged) == 'undefined')
    {
        OnStateChanged = 'stateChanged()';
    }
    eval(OnStateChanged);
    document.documentElement.style.cursor = "auto";

}

//  ---------------------------------------
function showInfo(URL, ID, OnStateChanged)
{
    document.documentElement.style.cursor = "wait";
    DivID = ID;
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp === null)
    {
        alert (" Your browser does not support AJAX!");
        return;
    }
    if (typeof(OnStateChanged) == 'undefined')
    {
        OnStateChanged = 'stateChanged';
    }
    xmlHttp.onreadystatechange = eval(OnStateChanged);
    document.getElementById(DivID).innerHTML = "Please Wait";
    xmlHttp.open("GET", URL, true);
    xmlHttp.send(null);
}

//  ---------------------------------------

function sendAJAXPost(URL, ID, params, OnStateChanged)
{
    DivID = ID;
    document.getElementById(DivID).innerHTML = 'Please wait ...';
    document.documentElement.style.cursor = "wait";
    xmlHttp = GetXmlHttpObject();
    xmlHttp.open("POST", URL, false);

    // Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    // document.documentElement.style.cursor = "auto";

    xmlHttp.send(params);

    if (typeof(OnStateChanged) == 'undefined')
    {
        OnStateChanged = stateChanged();
    }
    xmlHttp.onreadystatechange = OnStateChanged;

}

//  ---------------------------------------

function stateChanged()
{
    if (xmlHttp.readyState == 4)
    {
        // document.getElementById(DivID).innerHTML = xmlHttp.responseText;
        loadHTMLFragment(DivID, xmlHttp.responseText);
        if (SecondURL !== '')
        {
            showInfo(SecondURL, 	SecondDivID);
            SecondDivID = '';
            SecondURL = '';
            document.documentElement.style.cursor = "auto";
        }
    }
}

//  ---------------------------------------

function stateChangedNoJ()
{
    if (xmlHttp.readyState == 4)
    {
        document.getElementById(DivID).innerHTML = xmlHttp.responseText;
        if (SecondURL !== '')
        {
            showInfo(SecondURL, 	SecondDivID);
            SecondDivID = '';
            SecondURL = '';
            document.documentElement.style.cursor = "auto";
        }
    }
}

//  ---------------------------------------

function GetXmlHttpObject()
{
    var xmlHttp = null;

    try
    {
        // Firefox, Opera 8.0 + , Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    return xmlHttp;
}

//  ---------------------------------------


function showDiv(div)
{
    var TopZ = GetTopDivID(div);
    document.getElementById(div).style.display = 'block';
    document.getElementById(div).style.zIndex = TopZ + 1 ;
    if (document.getElementById(div).style.position == "fixed")
    {
        var target = document.getElementsByTagName('BODY')[0];
        target.appendChild(document.getElementById(div));
    }


//   document.getElementById(div).style.zIndex = 9999 ;


}

//  ---------------------------------------

function hideDiv(div)
{
    document.getElementById(div).style.display = 'none';
}

//  ---------------------------------------




function objectExists(obj)
{
    if (obj === null || obj === undefined)
    {
        return false;
    }
    else
    {
        return true;
    }
}

//  ---------------------------------------

function makeWin(url, windowName , params)
{
    if (params == 'X' || params === '' || typeof(params) == 'undefined' )
    {
        params = 'scrollbars=yes,status=no,menubar=1,dependent=yes,resizable=yes,width=700,height=550,screenX=0,screenY=0,alwaysRaised=yes';
    }
    agent = navigator.userAgent;
    win = window.open(url, windowName , params);

    if (agent.indexOf("Mozilla/2") != - 1 && agent.indexOf("Win") == - 1)
    {
        win = window.open(url, windowName , params);
    }
    if ( ! win.opener)
    {
        win.opener = window;
    }
}

//  ---------------------------------------


function reSize(ifrm)
{
    var oBody	 = 	ifrm.document.body;
    var oFrame	 = 	document.all(ifrm);
    try
    {

        oFrame.style.height = oBody.scrollHeight + (oBody.offsetHeight - oBody.clientHeight);
        oFrame.style.width = oBody.scrollWidth + (oBody.offsetWidth - oBody.clientWidth);
    }
    // An error is raised if the IFrame domain != its container's domain
    catch(e)
    {
        window.status = 	'Error: ' + e.number + '; ' + e.description;
    }
}

//  ---------------------------------------

function ShowFrame(fObjID, url, objheight)
{
    var oBody;
    var fObj;
    try
    {
        oBody	 = 	parent.document.body;
        objheight = screen.height - 200;
        objheight = oBody.clientHeight  - 20;
        if ( OpenedFrame != fObjID && OpenedFrame !== '')
        {
            fObj = document.getElementById(OpenedFrame);
            fObj.height = 1;
        }
        OpenedFrame = fObjID;
        fObj = document.getElementById(fObjID);
        // fObj.width = oBody.clientWidth - 20;
        fObj.width = "95%";
        if ( self.frames[fObjID].location.href.indexOf(url) == - 1)
        {
            fObj.height = "98%" ;
            self.frames[fObjID].location.href = url;
        }
        else
        {
            if (fObj.height == objheight)
            {
                fObj.height = 1;

            }
            else
            {
                fObj.height = "98%" ;
            }
        }
    }
    catch(e)
    {
        window.status = 	'Error: ' + e.number + '; ' + e.description;
    }

}

//  ---------------------------------------

function getElementsByTagNames(list, obj)
{
    if ( ! obj) var obj = document;
    var tagNames = list.split(',');
    var resultArray = new Array();
    for (var i = 0; i < tagNames.length; i ++ )
    {
        var tags = obj.getElementsByTagName(tagNames[i]);
        for (var j = 0; j < tags.length; j ++ )
        {
            resultArray.push(tags[j]);
        }
    }

    var testNode = resultArray[0];
    if (testNode=='undefined'){
        var testNode = new Array();
    }
    if (testNode.sourceIndex)
    {
        resultArray.sort(function (a, b)
        {
            return a.sourceIndex - b.sourceIndex;
        }
        )
    }
    else if (testNode.compareDocumentPosition)
    {
        resultArray.sort(function (a, b)
        {
            return 3 - (a.compareDocumentPosition(b) & 6);
        }
        )
    }
    return resultArray;
}

//  ---------------------------------------

function GetParamsInDiv(DivID)
{


    var params = "";
    var formFieldList = getElementsByTagNames('hidden,input,select,textarea,radio', document.getElementById(DivID));
    for (var i = 0; i < formFieldList.length; i ++ )
    {
        if  (formFieldList[i].name != 'undefined' || formFieldList[i].name !== '' )
        {
            if ( formFieldList[i].type == 'checkbox' || formFieldList[i].type == 'radio' )
            {
                if (formFieldList[i].checked)
                {
                    params = params + formFieldList[i].name + '=' + escape(formFieldList[i].value) + '&';
                }
            }
            else
            {
                params = params + formFieldList[i].name + '=' + escape(formFieldList[i].value) + '&';
            }
        }
    }
    return params;
}

//  ---------------------------------------

function SetValueInDiv(DivID, ID, Value)
{
    var formFieldList = getElementsByTagNames('hidden,input,select,textarea', document.getElementById(DivID));
    for (var i = 0; i < formFieldList.length; i ++ )
    {
        if  (formFieldList[i].id == ID)
        {
            formFieldList[i].value = Value;
            return true;
        }
    }
    return false;
}

//  ---------------------------------------

function GetValueInDiv(DivID, ID)
{
    var formFieldList = getElementsByTagNames('hidden,input,select,textarea', document.getElementById(DivID));
    for (var i = 0; i < formFieldList.length; i ++ )
    {
        if  (formFieldList[i].id == ID)
        {
            return formFieldList[i].value ;

        }
    }
    return "";
}

//  ---------------------------------------

function GetTopDivID(div)
{
    var aDivs = document.body.getElementsByTagName("DIV");
    var maxZ = 0;
    var tmpDiv;
    if (aDivs.length > 0)
    {
        for (var i = 0; i < aDivs.length; i ++ )
        {
            tmpDiv = aDivs[i];
            if (tmpDiv.id == div)
            {
                continue;
            }
            if (parseInt(tmpDiv.style.zIndex, 10) > maxZ)
            {
                maxZ = parseInt( tmpDiv.style.zIndex, 10);
            }
        }
    }
    return maxZ	;

}

//  ---------------------------------------

function getParamFromURL( name )
{
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

//  ---------------------------------------

function getParamFromStr( name, tStr )
{
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp( regexS, "i" );
    var results = regex.exec(tStr );
    if( results == null )
    {
        return "";
    }
    else
        return results[1];
}

//  ---------------------------------------

function getMouseXY(e)
{
    if (IE)
    {
        // grab the x - y pos.s if browser is IE
        if (objectExists(document.body)) {
            tempX = event.clientX + document.body.scrollLeft;
            tempY = event.clientY + document.body.scrollTop;
        }
    }
    else
    {
        // grab the x - y pos.s if browser is NS
        tempX = e.pageX;
        tempY = e.pageY;
    }
    if (tempX < 0)
    {
        tempX = 0;
    }
    if (tempY < 0)
    {
        tempY = 0;
    }
    return true;
}

//  ---------------------------------------

function GetSum(xID)
{
    var i = 1;
    var result = 0;
    var eId = xID + i;
    var VarId = document.getElementById(eId);
    while (objectExists( VarId))
    {
        result = result + parseFloat(VarId.value);
        i = i + 1;
        eId = xID + i;
        VarId = document.getElementById(eId);
    }
    if (objectExists( xID))
    {
        document.getElementById(xID).value = Math.round(result);
    }
    return Math.round(result);
}

//  ---------------------------------------
function _close()
{
    if(document.getElementById("_iframe"))
    {
        document.getElementById("_iframe").firstChild.src = "about:blank";
        document.getElementById("_iframe").style.visibility = "hidden";
        document.getElementById("_iframe").style.display = 'none';
    }

    return true;
}

//  ---------------------------------------


function _show(_m)
{
    if(document.getElementById("_iframe"))
    {
        document.getElementById("_iframe").firstChild.src = "/images/" + _m;
        document.getElementById("_iframe").style.visibility = "visible";
        document.getElementById("_iframe").style.display = 'block';
    }

    return false;
}
function dOMgetHTML(el) {
    if (el.nodeType == 3) {       // Handle Text nodes
        return el.nodeValue;
    } else if (el.nodeType == 8) { // Handle Comments correctly
        return '<!--' + el.nodeValue + '-->';
    } else {
        var txt = new Array();
        var i=0;
        txt[txt.length] = '<' + el.tagName;   // Start creating tag
        if (el.attributes)
        {
            for (a=0;a<el.attributes.length;a++) {  // Add Attributes
                if (el.attributes[a].specified) {     // But only user Specified ones
                    txt[txt.length] = ' ' + el.attributes[a].nodeName + '="' + el.attributes[a].nodeValue + '"';
                }
            }
        }
        if (el.childNodes.length > 0)
        //    if (el.childNodes)
        {
            txt[txt.length] = '>';
            while(el.childNodes[i]) {
                txt[txt.length] =  dOMgetHTML(el.childNodes[i]);
                i++;
            }
            txt[txt.length] =  '</' + el.tagName + '>';
        } else {
            txt[txt.length] =  '/>';
        }
    }
    return txt.join('');
}
//  ---------------------------------------

//if ( ! IE) document.captureEvents(Event.MOUSEMOVE)
//document.onmousemove = getMouseXY;

//  ---------------------------------------

function contactFormEx(y) {



    if (y!=ae_ans) {
        alert('Improve your math skills and try again!');
        return -1;
    }


  
    var params = GetParamsInDiv("dContact");
    showInfoSPost("contact.html", "dContact", params);
}

var ae_cb = null;
var ae_ans = 0;
 
// this is a simple function-shortcut
// to avoid using lengthy document.getElementById
function ae$(a) {
    return document.getElementById(a);
}
 
// This is a main ae_prompt function
// it saves function callback 
// and sets up dialog
function ae_prompt(cb, q, a) {
    ae_cb = cb;
    ae$('aep_t').innerHTML = document.domain + ' question:';
    ae$('aep_prompt').innerHTML = q;
    ae$('aep_text').value = a;
    ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = '';
    ae$('aep_text').focus();
    ae$('aep_text').select();
}
 
// This function is called when user presses OK(m=0) or Cancel(m=1) button
// in the dialog. You should not call this function directly.
function ae_clk(m) {
    // hide dialog layers
    ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = 'none';
    if (!m)
        ae_cb(null);  // user pressed cancel, call callback with null
    else
        ae_cb(ae$('aep_text').value); // user pressed OK
}



function formValidate() {	
    var x = Math.floor(Math.random()*11);
    var y = Math.floor(Math.random()*11);
    ae_ans = x + y;
    ae_prompt(contactFormEx,'How much is ' +x +' + ' + y + ' ?','0'  );
}

function populateAds(){
    if (ads.length == 0){
        var JDiv;
        JDiv = ae$('JAd');
        while (JDiv){
            if (JDiv){
                JDiv.style.display='block';
                JDiv.id = 'Jdiv1';
            }
            JDiv = ae$('JAd');
        }

        return
    }
    var arrCounter=0;
    var AdUnits = getElementsByTagNames('li', document);
    for (var i = 0; i < AdUnits.length; i ++ )
    {
        if (AdUnits[i].id == 'AdUnit')
        {
            var updateUnit = AdUnits[i];
            var elementJ = getElementsByTagNames('a,span,div,p,li', updateUnit);

            for (j=0; j<elementJ.length; j++){

                switch ( elementJ[j].id) {
                    case "sRedirect":
                        elementJ[j].href = ads[arrCounter][0];
                        break;
                    case "sTitle":
                        elementJ[j].innerHTML = ads[arrCounter][1];
                        break;
                    case "sDescr":
                        elementJ[j].innerHTML = ads[arrCounter][2];
                        break;
                    case "surl":
                        elementJ[j].innerHTML = ads[arrCounter][3];

                }

            }
            //            AdUnits[i].style.display = 'block';
            AdUnits[i].id = 'AdUnit.'+arrCounter;
            $(updateUnit).show();
            arrCounter = arrCounter+1;
            if (arrCounter>=ads.length){
                arrCounter = 0;
            }
        }
    }

}

function populateAds2()
{
    var AdId;
    var i;
    var j;
    var el = document.getElementsByTagName('div');
    for ( i=0; i<= el.length -1; i++){

        AdId = $(el[i]).attr( "AdId" );
        if (AdId != undefined) {
            for (j=0; j<=ads.length -1; j++)
            {
                if(ads[j][0]==AdId){

                    el[i].innerHTML =ads[j][1];
                    $(el[i]).show();
                    $(el[i]).attr( "AdId","" )
                    runScripts(el[i]);
                    break;
                }
            }
        }


    }


}
function checkAds(prefPr){
    var AdUnits = getElementsByTagNames('li', document);
    var AdsCount =0;
    for (var i = 0; i < AdUnits.length; i ++ )
    {
        if (AdUnits[i].id == 'AdUnit')
        {
            AdsCount = AdsCount+1;
        }
    }
    if (AdsCount>0){
        if (typeof(prefPr) == 'undefined')
        {
            prefPr = '';
        }
        (function(){
            var vsAds = document.createElement('script');
            vsAds.type = 'text/javascript';
            vsAds.async = true;
            vsAds.src = '/ads/vdayAds.js?AdsCount='+AdsCount+ '&prefPr='+ prefPr+ "&dummy=" + new Date().getTime();
            (document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(vsAds);
        })();

    }
}


function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +value+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}


function trim(str) {
    return str.replace(/^\s+|\s+$/g,"");
}
function runScripts(e) {

    if (e.nodeType != 1) return; //if it's not an element node, return

    if (e.tagName.toLowerCase() == 'script') {
        if($(e).attr( "src") != undefined)
        {
        (function(){
            var vsAds = document.createElement('script');
            vsAds.type = 'text/javascript';
            vsAds.async = false;
            vsAds.src = $(e).attr( "src");
            //(document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(vsAds);
            e.appendChild(vsAds);
        })();

        }
        else 
        {
            eval(e.text); //run the script
        }
    }
    else {
        var n = e.firstChild;
        while ( n ) {
            if ( n.nodeType == 1 ) runScripts( n ); //if it's an element node, recurse
            n = n.nextSibling;
        }
    }
}
