// JavaScript Document

// Global open popup window function for website
// pass the URL string, width and height desired for the output.

function openPopUp(URL, W, H) 
{
    // opens a new popup window and displays the passed url, and height & width settings
	var thisWidth = W;
	var thisHeight = H;
	var thisURL = URL;

	// set the min width and height if not passed
	if(typeof thisWidth == "undefined")
	{
		thisWidth = 600;
	}
	if(typeof thisHeight == "undefined")
	{
		thisHeight = 700;
	}
		
	//alert("passed W = "+thisWidth+" passed H = "+thisHeight);	
	var strOptions = "menubar=no,statusbar=yes,resizable=yes,scrollbars=yes,toolbar=no,height="+thisHeight+",width="+thisWidth; 
	var newPopUp = window.open(thisURL, "", strOptions);	
	
    //if popup window is blocked, the newPopUp will be null.
	if (newPopUp != null)
	{
	    newPopUp.focus();
	}
}

function printPopUp()
{
    //if a Pop up window contains Iframe (e.g. oversea payment registration form),
    //window.print() will only print the outside frame not the Iframe content.
    //In order to print the Iframe we need to set focus to Iframe. Since only the
    //oversea payment registration form pop up window contains Iframe, there is no
    //need to retrieve the Iframe through Iframe ID.
    if (window.frames != null && window.frames.length == 1)
    {
        window.frames[0].focus();
        if (navigator.appName.indexOf("Microsoft") != -1)
        {
            window.print();
        }
    }
    else
    {
        window.print();
    } 
}

function openNewWindow(URL)
{
    var thisURL = URL;
    
    if (typeof(thisURL) != 'undefined' && thisURL.length > 0)
    {
        var newWin = window.open(thisURL);
        newWin.focus();
    }
}


// This method provies show-hide functionality for DIVs
// Pass div id to the method and it will show/hide it
// Use "display: none" css on the div to initially hide it
function toggleLayer(whichLayer){	
	var elem, vis;
	elem = GetElement(whichLayer);
	vis = elem.style; 
	
	// if the style.display value is blank we try to figure it out here  
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)    
		vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';  
	vis.display = (vis.display==''||vis.display=='block')?'none':'block';	
}

function toggleOutput(FName, LName, FNameText, LNameText, errorFName, errorLName, inputLayer, outputLayer)
{
	GetElement(errorFName).innerHTML = "";
	GetElement(errorLName).innerHTML = "";
	
    if (IsEmpty(FName))
    {
        GetElement(errorFName).innerHTML = "Please enter your First Name";
        GetElement(FName).focus();
    }										 
    else if (IsEmpty(LName))
    {
        GetElement(errorLName).innerHTML = "Please enter your Last Name";
        GetElement(LName).focus();
    }
	else
	{ 
	    toggleLayer(inputLayer); 
	    toggleLayer(outputLayer);
	    GetElement(FNameText).innerHTML = GetElement(FName).value;
	    GetElement(LNameText).innerHTML = GetElement(LName).value;
	    if(GetElement("header"))  // looks for page header and if id is there, scrolls the page to the top
			GetElement("header").focus();
	} 	
}

function IsEmpty(field)
{
    var isEmpty = false;
    var elem = GetElement(field);
    if(elem.value == "") isEmpty = true;
    
    return isEmpty
}

function GetElement(element)
{
    var elem;
	if(document.getElementById) // standards compliant browsers
		elem = document.getElementById(element);
	else if(document.all) // old IE browsers (6 and below)
		elem = document.all[element];
	else if(document.layers) // old Netscape/Mozilla browsers
		elem = document.layers[element];
    return elem;
}

function ServerTime(dateTimeFormat)
{
    if (window.XMLHttpRequest)
    {   // for IE7+, Firfox, Safari etc.
        xmlhttp = new XMLHttpRequest();
    }
    else
    {   // for IE6-
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if (xmlhttp)
    {
        url = window.location.href;
        url = url.substring(0, url.lastIndexOf("/")) + "/servertime.aspx";
        
        xmlhttp.open("GET",  url, false);
        xmlhttp.send(null);
        xmlString = xmlhttp.responseText;
        
        startIndex = -1;
        endIndex = -1;
        
        switch (dateTimeFormat)
        {
            case '1': // 2010-06-08T18:59:14-01:00
                startIndex = xmlString.indexOf('<ServerDateFormatOne>') + 21;
                endIndex = xmlString.indexOf('</ServerDateFormatOne>');
                break;
            case '2': // 08 June, 2010
                startIndex = xmlString.indexOf('<ServerDateFormatTwo>') + 21;
                endIndex = xmlString.indexOf('</ServerDateFormatTwo>');
                break;
            default: // 2010-06-08T17:59:14Z
                startIndex = xmlString.indexOf('<ServerDate>') + 12;
                endIndex = xmlString.indexOf('</ServerDate>');
                break;
        }
        
        dateTimeText = '';
        
        if (startIndex > 0 && endIndex > startIndex)
        {
            dateTimeText = xmlString.substr(startIndex, endIndex - startIndex);
            
            // '08 June, 2010' trim the first '0' to return '8 June, 2010'
            if (dateTimeFormat == '2' && dateTimeText.indexOf('0') == 0)
            {
                dateTimeText = dateTimeText.substr(1);
            }
        }
        
        return dateTimeText;
    }
    else
    {
        return "Date could not be displayed - Your browser does not support AJAX.";
    }
}

