﻿function init()
{
    setupLayout();
    enableTabs();
    //setupRollovers();  
    setupctl00_ctl00_cols23Content_pageContent_formPanel(); 
}

function setupLayout()
{
    var left = document.getElementById('ctl00_ctl00_left');
    var middle = document.getElementById('ctl00_ctl00_cols23Content_middle');
    
    if (left && middle)
    {
        if (left.offsetHeight < middle.offsetHeight)
        {
            left.style.height = parseInt(middle.offsetHeight) + 20 + "px";
        }
        else
        {
            middle.style.height = parseInt(left.offsetHeight) - 20 + "px";
        }
    }
}

function setupRollovers()
{
    var imgs = document.getElementsByTagName("IMG");
    
    for (var i = 0; i < imgs.length; i++)
    {
        var img = imgs[i];

        // set current button on
        
        if (img.parentNode && img.parentNode.href && (unescape(img.parentNode.href) == unescape(location.href)))
        {
            img.src = img.src.replace('-off', '-on');
        }
        else if (img.src.match(/-off-/))
        {        
            img.onmouseover = rollOn;
            img.onmouseout = rollOff;
        }
        
    }
}


function rollOn()
{
    this.src = this.src.replace(/-off/, '-on');
}

function rollOff()
{
    this.src = this.src.replace(/-on/, '-off');
}

function updateLabel(destId, textbox)
{
    var val = textbox.value;
    
    val = val.replace(/[^a-zA-Z0-9\-]/g, '').toLowerCase();
 
    textbox.value = val;   
    document.getElementById(destId).innerHTML = val;
}

function clearSearchBox(box)
{
    // clear on focus
    if (box.value.length > 0 && box.value.substr(0, 1) == '[')   
    {
        box.value = '';
    }
        
    box.style.color = '';
    
}

function addTag(tag, id)
{
    var o = document.getElementById(id);
    
    if (o)
    {
        if (o.value.length > 0)
            o.value += ' ';
          
        if (tag.indexOf(' ') >= 0)  
            o.value += '"' + tag + '"';
        else
            o.value += tag;
    }
}

function setDate(obj)
{
    // if dateTo is before dateFrom, make it an hour later
    var day1 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateFrom_day');
    var month1 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateFrom_month');
    var year1 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateFrom_year');
    var hour1 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateFrom_hour');
    var minute1 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateFrom_minute');
    var amPm1 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateFrom_amPm');
    
    var d81 = new Date(parseInt(year1.options[year1.selectedIndex].text), month1.selectedIndex, day1.selectedIndex + 1, hour1.selectedIndex + (amPm1.selectedIndex == 1 ? 12 : 0), minute1.selectedIndex * 5, 0);

    var day2 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateTo_day');
    
    if (day2)
    {
        var month2 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateTo_month');
        var year2 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateTo_year');
        var hour2 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateTo_hour');
        var minute2 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateTo_minute');
        var amPm2 = document.getElementById('ctl00_ctl00_cols23Content_pageContent_FormView1_DateTo_amPm');
        
        var d82 = new Date(parseInt(year2.options[year2.selectedIndex].text), month2.selectedIndex, day2.selectedIndex + 1, hour2.selectedIndex + (amPm2.selectedIndex == 1 ? 12 : 0), minute2.selectedIndex * 5, 0);

        if (d82 < d81)
        {
            day2.selectedIndex = day1.selectedIndex;
            month2.selectedIndex = month1.selectedIndex;
            year2.selectedIndex = year1.selectedIndex;            
            hour2.selectedIndex = Math.min(11, hour1.selectedIndex + 1);                
            minute2.selectedIndex = minute1.selectedIndex;
            amPm2.selectedIndex = amPm1.selectedIndex;
        }
     }   
}

function formatNumber(n,format){
	if(isNaN(n)) return "";
	var i,c,f,comma,symbol='',sign='',decimals='',integers='';
	var fInt,fDec,nInt,nDec,len=0,cnt=0;
	if(n<0) sign='-';
	n+='';if(sign) n=n.replace('-','');
	format=(format)? format+'':'#,##0.00';
	if(format.indexOf(',')>=0) comma=',';
	if(format.indexOf('$')>=0) symbol='$';
	else if(format.indexOf('%')>=0)	symbol='%';
	s=format.split('.');
	fInt=((s[0]==''||s[0]==null||s[0]=='undefinded')? '':s[0]);
	fInt=fInt.split('').reverse().join('');
	fDec=(s[1]==''||s[1]==null||s[1]=='undefinded')? '':s[1];
	s=n.split('.');
	nInt=((s[0]==''||s[0]==null||s[0]=='undefinded')? '':s[0]);
	nInt=nInt.split('').reverse().join('');;
	nDec=(s[1]==''||s[1]==null||s[1]=='undefinded')? '':s[1];
	if (nInt) len=nInt.length;
	if (fInt.length>len) len=fInt.length;	
	for(i=0;i<len;i++){
		c=nInt.charAt(i);
		f=fInt.charAt(i);
		cnt++;
		if (cnt==4 && comma && (c||f=='0')) integers+=comma;
		if(f=='0' && !c) integers+='0';
		else if(c) integers+=c;
		if (cnt==4) cnt=1;
	}
	if(fDec) len=fDec.length;
	for(i=0;i<len;i++){
		c=nDec.charAt(i);
		f=fDec.charAt(i);
		if(f=='0' && !c) decimals+='0';
		else if((f=='#' || f=='0') && c) decimals+=c;
	}
	f=((integers+'').split('').reverse().join(''))+((decimals)? '.'+decimals:'');
	if(symbol=='%') f+=symbol;
	else f=symbol+f;
	return sign+f;
}

function setupctl00_ctl00_cols23Content_pageContent_formPanel()
{
    if (document.getElementById('ctl00_ctl00_cols23Content_pageContent_formPanel'))
    {
        var chks = document.getElementById('ctl00_ctl00_cols23Content_pageContent_formPanel').getElementsByTagName('INPUT');
        
        for(var i = 0; i < chks.length; i++)
        {
            var chk = chks[i];
            
            if (chk.type == 'checkbox' || chk.type == 'radio')
            {
                if (chk.id.indexOf('isGift') < 0)
                {
                    chk.onclick = calcCost;
                }
                else
                {
                    // leave as-is
                }
            }
        }
        
        document.getElementById('ctl00_ctl00_cols23Content_pageContent_country').onchange = calcCost;
                                 
        calcCost();
        
        showGiftDiv(document.getElementById('ctl00_ctl00_cols23Content_pageContent_isGift'))   
    }
}

function doReset()
{
    document.getElementById('aspnetForm').reset();
    calcCost();
}

function calcCost()
{
    var totalCost = 0;
    
    var isForeign = (document.getElementById('ctl00_ctl00_cols23Content_pageContent_country').options[document.getElementById('ctl00_ctl00_cols23Content_pageContent_country').selectedIndex].value != 'Australia');
    var backIssueCount = 0;
    
    var chks = document.getElementById('ctl00_ctl00_cols23Content_pageContent_formPanel').getElementsByTagName('INPUT');
    
    for(var i = 0; i < chks.length; i++)
    {
        var chk = chks[i];
        
        if ((chk.type == 'checkbox'  || chk.type == 'radio') && chk.checked && chk.parentNode && chk.parentNode.getAttribute('cost'))
        {            
            totalCost += parseFloat(chk.parentNode.getAttribute('cost'));
            
            if (chk.id.indexOf('_issue') > 0)
                backIssueCount += 1;
        }
    }
    
    if (isForeign)
    {
        if (document.getElementById('ctl00_ctl00_cols23Content_pageContent_year1Sub').checked)
            totalCost += 3 * 20;
            
        if (document.getElementById('ctl00_ctl00_cols23Content_pageContent_year2Sub').checked)
            totalCost += 6 * 20;
            
        if (backIssueCount > 0 && backIssueCount < 3)
            totalCost += 20;    
        else if (backIssueCount >= 3)
            totalCost += 40;
    }
    
    document.getElementById('totalCost').innerHTML = '$' + formatNumber(totalCost + 0.005, "####0.00");
       
    document.getElementById('ctl00_ctl00_cols23Content_pageContent_totalValue').value = totalCost;
}

function showGiftDiv(chk)
{
    if (chk.checked)
        document.getElementById('ctl00_ctl00_cols23Content_pageContent_giftDiv').style.display = 'block';
    else
        document.getElementById('ctl00_ctl00_cols23Content_pageContent_giftDiv').style.display = 'none';
    
}


//-------------------- Free email update

function doSubscribe()
{
    var ok = true;

    if (! document.getElementById('ctl00_ctl00_cols23Content_pageContent_email').value.match(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/))
    {
    	alert('Invalid email address');
	    ok = false;
    }
	
    if (ok)
    {
        var destUrl = "http://eddesk3.itechne.com/newsletter/subscribe.aspx";
        
        var url = "http://www.artreview.com.au/scripts/ajax.aspx";

        var msg = 'Thank you. Subscription submitted.';

        if (document.getElementById('ctl00_ctl00_cols23Content_pageContent_unsubscribe').checked)
        {
            destUrl = destUrl.replace(/subscribe/, 'unsubscribe');
            msg = 'Thank you. Subscription removal submitted.';
        }

        url += '?email=' + escape(document.getElementById('ctl00_ctl00_cols23Content_pageContent_email').value);
        url += '&name=';
        url += '&emailListId=' + 'f49c1ae1-4d01-4a9f-9525-76478e154e87'; 
        url += '&destUrl=' + escape(destUrl);

        //prompt('url', url);

        var ajax = new AjaxDelegate(url, "", null);
        ajax.Fetch();

        alert(msg);
    }
}
