﻿/* JVira
 * Copyright 2009 Multivira Inc. , Babak Arjomandi
 * Ver 1
 */


 // Define Global Variables ///////////////////////////////////////////////////////////////////////////////////////

var startTime;
var mouseX = 0;
var mouseY = 0;
var mouseClickDown = false;
var mouseMoveAction = "";
var LastMousePosX;
var LastMousePosY;

// Init Page // Page Load ///////////////////////////////////////////////////////////////////////////////////////

if (window.attachEvent) {
    window.attachEvent("onload", function() { pageload(); });
}
else {
    window.onload = function() { pageload(); };
}

function pageload() {
    document.onmousemove = getMouseXY;
    document.onmouseup = function () { mouseClickDown = false; mouseMoveAction = ""; };
    //document.onselectstart = function () { return false; }
}



// Mouse Position Control ///////////////////////////////////////////////////////////////////////////////////////

function getMouseXY(e) {
    if (!e) { // grab the x-y pos.s if browser is IE
        mouseX = event.clientX + document.body.scrollLeft;
        mouseY = event.clientY + document.body.scrollTop;
    }
    else {  // grab the x-y pos.s if browser is NS
        mouseX = e.pageX;
        mouseY = e.pageY;
    }
    if (mouseX < 0) { mouseX = 0; }
    if (mouseY < 0) { mouseY = 0; }

    if (mouseMoveAction != "") {
        setScrollCotrolMouseMove(mouseMoveAction);
    }
    return true;
}



// Custom Scrolling ///////////////////////////////////////////////////////////////////////////////////////

function initCustomScroll(divID) {
    if (document.getElementById(divID) != null) {

        document.getElementById(divID).onselectstart = function () { return false; }

        // Create Left Key
        if (document.getElementById(divID + "_leftKey") == null) {
            var newDiv = document.createElement('div');
            newDiv.setAttribute("id", divID + "_leftKey");
            newDiv.setAttribute("onclick", "leftScroll('" + divID + "')");
            document.getElementById(divID).appendChild(newDiv);
        }

        // Create Right Key
        if (document.getElementById(divID + "_rightKey") == null) {
            var newDiv = document.createElement('div');
            newDiv.setAttribute("id", divID + "_rightKey");
            newDiv.setAttribute("onclick", "rightScroll('" + divID + "')");
            document.getElementById(divID).appendChild(newDiv);
        }

        // Create Scroll Slider
        if (document.getElementById(divID + "_scrollSlider") == null) {
            var newDiv = document.createElement('div');
            newDiv.setAttribute("id", divID + "_scrollSlider");
            document.getElementById(divID).appendChild(newDiv);
        }

        // Create Scroll Slider Fill
        if (document.getElementById(divID + "_scrollSliderFill") == null) {
            var newDiv = document.createElement('div');
            newDiv.setAttribute("id", divID + "_scrollSliderFill");
            document.getElementById(divID + "_scrollSlider").appendChild(newDiv);
        }

        // Create Scroll Slider Control
        if (document.getElementById(divID + "_scrollSliderControl") == null) {
            var newDiv = document.createElement('div');
            newDiv.setAttribute("id", divID + "_scrollSliderControl");
            newDiv.setAttribute("onmousedown", "setScrollCotrolMouseDown('" + divID + "')");
            document.getElementById(divID + "_scrollSlider").appendChild(newDiv);
        }

        checkScrollKey(divID);
    }
}

function leftScroll(divID) {
    if (document.getElementById(divID) != null) {
        document.getElementById(divID).scrollLeft += document.getElementById(divID).offsetWidth;
        checkScrollKey(divID);
    }
}

function rightScroll(divID) {
    if (document.getElementById(divID) != null) {
        document.getElementById(divID).scrollLeft += -document.getElementById(divID).offsetWidth;
        checkScrollKey(divID);
    }
}

function checkScrollKey(divID) {
    if (document.getElementById(divID) != null) {

        sliderControl(divID);

        if (document.getElementById(divID).scrollLeft == 0) {
            document.getElementById(divID + "_rightKey").style.visibility = "hidden";
        }
        else {
            document.getElementById(divID + "_rightKey").style.visibility = "visible";
        }

        if (document.getElementById(divID).scrollLeft == document.getElementById(divID).scrollWidth - document.getElementById(divID).offsetWidth) {
            document.getElementById(divID + "_leftKey").style.visibility = "hidden";
        }
        else {
            document.getElementById(divID + "_leftKey").style.visibility = "visible";
        }
    }
}

function sliderControl(divID) {
    if (document.getElementById(divID) != null) {
        if (document.getElementById(divID).scrollWidth > document.getElementById(divID).offsetWidth) {
            document.getElementById(divID + "_scrollSlider").style.visibility = "visible";
            document.getElementById(divID + "_scrollSlider").style.width = document.getElementById(divID).offsetWidth + "px";
            document.getElementById(divID + "_scrollSliderFill").style.width = Math.round((document.getElementById(divID).offsetWidth * document.getElementById(divID).scrollLeft) / document.getElementById(divID).scrollWidth) + "px";
            document.getElementById(divID + "_scrollSliderControl").style.width = Math.round((document.getElementById(divID).offsetWidth * document.getElementById(divID).offsetWidth) / document.getElementById(divID).scrollWidth) + "px";
        }
        else {
            document.getElementById(divID + "_scrollSlider").style.visibility = "hidden";
        }
    }
}


function setScrollCotrolMouseDown(divID) {
    LastMousePosX = mouseX;
    LastMousePosY = mouseY;
    mouseClickDown = true;
    mouseMoveAction = divID;
}

function setScrollCotrolMouseMove(divID) {
    if (mouseClickDown == true) {
        var goScrollLeftCtrl = document.getElementById(divID + "_scrollSliderFill").offsetWidth + (mouseX - LastMousePosX);
        if ((goScrollLeftCtrl + document.getElementById(divID + "_scrollSliderControl").offsetWidth) > document.getElementById(divID + "_scrollSlider").offsetWidth) {
            goScrollLeftCtrl = document.getElementById(divID + "_scrollSlider").offsetWidth - document.getElementById(divID + "_scrollSliderControl").offsetWidth;
        }
        if (goScrollLeftCtrl < 0) {
            goScrollLeftCtrl = 0;
        }

        LastMousePosX = mouseX;
        LastMousePosY = mouseY;
        document.getElementById(divID).scrollLeft = Math.round((document.getElementById(divID).scrollWidth * goScrollLeftCtrl) / document.getElementById(divID).offsetWidth);
        checkScrollKey(divID);
    }
}



/////////////////////////////////////////////////////////////////////////////////////////

function openclose(DivName,ImageName)
{
if (document.getElementById(DivName).style.display=='none')
   {
     document.getElementById(DivName).style.display='block';
     document.getElementById(ImageName).src="images/mclose.png";
     document.getElementById(ImageName).alt="Close";
   }
   else
   {
    document.getElementById(DivName).style.display='none';
    document.getElementById(ImageName).src="images/mopen.png";
    document.getElementById(ImageName).alt="Open";
   }
}



function printWindow()
{
   bV = parseInt(navigator.appVersion);
   if (bV >= 4) window.print();
}



function PrintContentByID(ObjID)
{
        docPrint=window.open("","Print");
        docPrint.document.open();
        docPrint.document.write('<html><head><title>Print</title>');
        docPrint.document.write('<link href="Print.css" type="text/css" rel="STYLESHEET">');
        docPrint.document.write('</head><body onLoad="self.print()">');
        docPrint.document.write(document.getElementById(ObjID).innerHTML);  
        docPrint.document.write('</body></html>');
        docPrint.document.close();  
        docPrint.focus();
}




function ConfirmChoice(MyMes,MyPar) 
{ 
   answer = confirm(MyMes)
   if (answer !="0") 
   { 
   location = MyPar 
   } 
}




function GotoURL(url)
{
   window.location.href=url ;
}
	


function MouseRowIn(RowName)
{
RowName.bgColor = "#dddddd" ;
}


function MouseRowOut(RowName)
{
RowName.bgColor = "" ;
}


function ShowDiv(targetId)
{
   document.getElementById(targetId).style.display = 'block';
}



function HideDiv(targetId)
{
   document.getElementById(targetId).style.display = 'none';
}


function domianExtSel(val)
{
   document.getElementById('domainExtVal').innerHTML = val;
   document.getElementById('domainComboBody').style.display = 'none';
   document.getElementById('TextBox29').focus();
}





function EmailContent(email,rname,subject,divid)
{
      var body = "";
      if(document.getElementById(divid))
      {
      body = "Dear " + rname + ", " + document.getElementById(divid).innerHTML;
      }
      window.open("mailto:"+email+"?subject=" + encodeURIComponent(subject) + "&body=" + escape(body),"SendEmail");
}


var EscKeyPress;


function popWinShow(popupDiv)
{
   if (document.getElementById("popWinBG")==null)
      { 
         var popWinBG = document.createElement('div');
         popWinBG.setAttribute("id","popWinBG");
         document.body.appendChild(popWinBG);         
      }
            
   document.getElementById("popWinBG").style.visibility = 'visible';
   
   if (document.getElementById("popWin")==null)
      { 
         var popWin = document.createElement('div');
         popWin.setAttribute("id","popWin");
         document.body.appendChild(popWin);
      }
   
   document.getElementById("popWin").innerHTML = '<span class="IWinClose" onclick="popWinClose();"></span>' +
                                                 document.getElementById(popupDiv).innerHTML;
   document.getElementById("popWin").style.visibility = 'visible';
   EscKeyPress = true;
   document.onkeydown = EscKeyExit;
}

function popWinClose()
{
   if (document.getElementById("popWinBG")!=null)
      { 
         document.getElementById("popWinBG").style.visibility = 'hidden';
      }
         
   if (document.getElementById("popWin")!=null)
      { 
         document.getElementById("popWin").style.visibility = 'hidden';
      }      
}


function EscKeyExit(e) {
	var x;
	if (document.all) {
		var evnt = window.event;
		x = evnt.keyCode;
        }
        else {
            x = e.keyCode;
        }

	if (x == 27 && EscKeyPress == true) {
		popWinClose();
		EscKeyPress = false;
        }
}


function randomRefresh() {
    var RandInt = Math.floor(Math.random() * 9999);
    var DataTimeNow = new Date();
    var result = DataTimeNow.getSeconds() + (DataTimeNow.getMinutes() * 60) + (DataTimeNow.getHours() * 3600) + (DataTimeNow.getDate() * 86400);
    return String(result) + String(RandInt);
}



// Ads AND Banners ///////////////////////////////////////////////////////////////////////////////////////
var BannerList = new Array();
var imageIDcurrent = 0 ;

function createBanner(BannerDivID,imageList,TimerLength) {
    BannerList = imageList.split(',');

    if (document.getElementById("bannerImg")==null)
       { 
          var bannerImg = document.createElement('img');
          bannerImg.setAttribute("id","bannerImg");
          document.getElementById(BannerDivID).appendChild(bannerImg);
       }   
       
    if (document.getElementById("BannerCtlKeys")==null)
       { 
          var BannerCtlKeys = document.createElement('div');
          BannerCtlKeys.setAttribute("id","BannerCtlKeys");
          document.getElementById(BannerDivID).appendChild(BannerCtlKeys);         
       }       
       
    var BKeys = ""; 
    var i = 0;    
    for (i = 0 ; i < BannerList.length ; i++)
    {
        var ii = i + 1 ;
        BKeys += '<a href="#" id="BannerKey_'+i+'" class="BannerKey" onclick="AdsShow('+i+');">'+ii+'</a>' ;
    }    
    document.getElementById('BannerCtlKeys').innerHTML = BKeys ;

   AdsShow(imageIDcurrent);
   setInterval("changeAds()", TimerLength);
}


function AdsShow(imageID) {
    imageIDcurrent = imageID;
    document.getElementById("bannerImg").src = BannerList[imageIDcurrent];  
    var i = 0;    
    for (i = 0 ; i < BannerList.length ; i++)
    {
        document.getElementById('BannerKey_'+i).className = 'BannerKey';
    }        
    document.getElementById('BannerKey_'+imageID).className = 'BannerKeyActive';
}


function changeAds() {
    imageIDcurrent += 1 ;
    if (imageIDcurrent >= BannerList.length)
       { imageIDcurrent = 0 ; }  
    AdsShow(imageIDcurrent);
}



// Ajax Call ///////////////////////////////////////////////////////////////////////////////////////

function makeHttpRequest(url, callback_function, return_xml) {
    var http_request, response, i;
    var activex_ids = ['MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];

    if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) { // IE6 and older
        for (i = 0; i < activex_ids.length; i++) {
            try {
                http_request = new ActiveXObject(activex_ids[i]);
            }
            catch (e) { }
        }
    }

    if (!http_request) {
        alert('Unfortunately your browser doesn’t support this feature.');
        return false;
    }

    http_request.onreadystatechange = function() {
        if (http_request.readyState !== 4) {
            // not ready yet
            callback_function('loading');
            return;
        }
        if (http_request.status !== 200) {
            // ready, but not OK
            //alert('There was a problem with the request.(Code: ' + http_request.status + ')');
            callback_function('error');
            return;
        }

        if (return_xml) {
            response = http_request.responseXML;
        }
        else {
            response = http_request.responseText;
        }
        callback_function(response);
    };

    var rand = "&ref=" + randomRefresh();
    http_request.open('GET', url + rand, true);
    http_request.send(null);
}



// Internet Speed Test ///////////////////////////////////////////////////////////////////////////////////////

function speedTest() {
    var d = new Date;
    startTime = d.getTime();
    makeHttpRequest('images/speedtest.bmp?', getSpeedTest, false);
}

function getSpeedTest(ajaxContent) {
    switch (ajaxContent) {
        case 'loading':
            break;
        case 'error':
            alert('There is a problem with the request!');
            break;
        default:
            var bytesLoaded = 3000056;
            var d = new Date;
            var time = Math.round((d.getTime() - startTime) / 10) / 100;
            var connSpeed = readablizeBit(Math.round(bytesLoaded / time));
            alert("Speed Test Duration: " + time + " second \n" +
                  "Connection Speed: " + connSpeed);
            break;
    }
}



function readablizeBit(bit) {
    bit = bit * 10;
    var s = new Array('Bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps');
    var e = Math.floor(Math.log(bit) / Math.log(1000));
    return (bit / Math.pow(1000, Math.floor(e))).toFixed(2) + " " + s[e];
}

function readablizeBytes(bytes) {
    var s = new Array('Bytes', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb');
    var e = Math.floor(Math.log(bytes) / Math.log(1024));
    return (bytes / Math.pow(1024, Math.floor(e))).toFixed(2) + " " + s[e];
}


