/*globals*/
var g_LeftColSize = 132;
var g_Gutter = 5;
var g_ContentColSizeIE = 625;//476;
var g_ContentColSize = 605;//456;
var g_MaxSearchFields = 10;
var g_CtlWorkBg = '_ctlWB';
/********/

function toggleLeftCol(show) {
	var left = document.getElementById("left");
	var content = document.getElementById("content");
	if (!content)
	    content = document.getElementById("ctl00_content");
	    
	if (!content)
	    return;
	
	if (!show) {
		left.style.display = 'none';
		if (document.all) {
			content.style.width = g_ContentColSizeIE + g_LeftColSize + g_Gutter;
		}
		else {
			content.style.width = g_ContentColSize + g_LeftColSize + g_Gutter;
		}
	}
	else {
	    left.style.display = 'block';
		if (document.all) {
			content.style.width = g_ContentColSizeIE;
		}
		else {
			content.style.width = g_ContentColSize;
		}
		
	}
}

function toggleRCol(show) {
    var left = document.getElementById("right");
    var content = document.getElementById("content");
    if (!content)
        content = document.getElementById("ctl00_content");

    if (!content)
        return;

    if (!show) {
        left.style.display = 'none';
        if (document.all) {
            content.style.width = g_ContentColSizeIE + g_LeftColSize + g_Gutter;
        }
        else {
            content.style.width = g_ContentColSize + g_LeftColSize + g_Gutter;
        }
    }
    else {
        left.style.display = 'block';
        if (document.all) {
            content.style.width = g_ContentColSizeIE;
        }
        else {
            content.style.width = g_ContentColSize;
        }

    }
}

function toggleSearchBoxes(check) {
    var id = check.id.replace('_tgl', '_w');
    var box = document.getElementById(id);
    
    if (!box) {
        id = check.id.replace('_tgl', '_');
    }
    box = document.getElementById(id);
    
    if (!box)
        return;
        
    var cnt = countCheckBoxes();
    showCount(cnt);
    showWeight();
    
    if (cnt > g_MaxSearchFields) {
        //alert("Maximum of " + g_MaxSearchFields + " allowed!");
        showSearchError("Maximum of " + g_MaxSearchFields + " search fields allowed!");
        check.checked = false;
        var cnt = countCheckBoxes();
        showCount(cnt);
        showWeight();
        window.location.href = "#top";
        return;
    }
    
    showSearchError('');
    
    if (check.checked) {
        box.style.display = 'inline';        
    }
    else 
        box.style.display = 'none';      
        
    var wsBoxId = check.id.replace('_tgl', '_ws');
    box = document.getElementById(wsBoxId);
    if (box) {
        if (check.checked)
            box.style.display = 'inline';
        else
            box.style.display = 'none';
    } 
    
    var tglListId = check.id.replace('_tgl', '_exp');
    box = document.getElementById(tglListId);
    if (box) {
        if (check.checked) {
            
            box.className = 'toggle-list';            
            box.style.display = 'block';
        }
        else
            box.style.display = 'none';
    } 
        
}

function resetWeight() {
    var hw = document.getElementById("hidTotalWeight");
    if (!hw)
        return;
        
    hw.value = 0;
}

function countCheckBoxes() {
    resetWeight();
    
    var cnt = 0;
    var el = document.getElementsByTagName("input");
    if (!el)
        return;
        
    for(i = 0; i < el.length; i++) {
        if (el[i].type.toLowerCase() == 'checkbox' 
            && el[i].name.indexOf('tgl') != -1) {
            
            if (el[i].checked) {
                addWeight(document.getElementById(el[i].id.replace('tgl', 'ws')));
                cnt++;
            }
        }
    }
    return cnt;
}

function addWeight(weightBox) {
    var w = getNumericVal(weightBox.options[weightBox.options.selectedIndex].value);
    
    var hw = document.getElementById("hidTotalWeight");
    var weight = getNumericVal(hw.value);
    
    hw.value = weight + w;
}

function getWeight() {
    return getNumericVal(document.getElementById("hidTotalWeight").value);
}

function onWeight_Change() {
    var cnt = countCheckBoxes();
    showCount(cnt);
    showWeight();
}

function getNumericVal(val) {
    try {
        return parseInt(val);
    }
    catch (e) {
        return 0;
    }
}

function showCount(cnt) {
    var box = document.getElementById("boxCount");
    if (!box)
        return;
        
    box.innerHTML = cnt;
}

function showWeight() {
    var box = document.getElementById("totalWeight");
    var hbox = document.getElementById("hidTotalWeight");
    
    if (!box || !hbox)
        return;
    
    box.innerHTML = hbox.value + "%";
    
    if (getNumericVal(hbox.value) == 100) {
        box.style.color = '#4F6782';
    }
    else
        box.style.color = '#ff0000';
}

function submitSearch() {

    var cnt = countCheckBoxes();
    showCount(cnt);
    showWeight();
    
    if (cnt > g_MaxSearchFields) {
        showSearchError('Exceeded number of allowed search fields!');     
        window.location.href = "#top";   
        return false;    
    }
    
    if (getWeight() != 100) {
        showSearchError('Total Weight must equal 100%!');
        window.location.href = "#top";   
        return false;
    }
    
    //attn: hardcoded
    var container = 'ctl00_mainContent';
    var box = document.getElementById(container + "_txtJobTitle");
    if (box && box.value.length == 0) {
        showSearchError('Please enter Job Title');
        window.location.href = "#top";   
        return false;
    }
    
    box = document.getElementById(container + "_txtLocationPostcode");
    if (box && box.value.length == 0) {
        showSearchError('Please enter Postcode');
        window.location.href = "#top";   
        return false;
    }
    
    box = document.getElementById(container + "_txtCode");
    if (box && box.value.length == 0) {
        showSearchError('Please enter Job Ref #');
        window.location.href = "#top";   
        return false;
    }
    
    return true;
}

function showSearchError(msg) {
    var box = document.getElementById("searchAlertBox");
    box.innerHTML = msg;
    if (msg.length > 0) {
        box.style.display = 'block';
    } 
    else
        box.style.display = 'none';
}

function submitRegistration() {
}

function populateAwareBox(list) {
    var txtBox = document.getElementById(list.id.replace("_lst", "_txt"));
    if (!txtBox)
        return;
        
    if (list.options[list.options.selectedIndex].value != "0")
        txtBox.value = list.options[list.options.selectedIndex].text;
    else 
        txtBox.value = "";
}

function updateABN(box) {
    var id = box.id.substring(0, box.id.length - 1);
    var hid = document.getElementById(id);
    if (!hid)
        return;
        
    hid.value = '';    
    for (i = 1; i < 5; i++) {
        hid.value += document.getElementById(id + i.toString()).value;
    }
    
}

function del(frm) {
	if (confirm("Please confirm 'DELETE' action!"))
		frm.submit();
	else
		return false;
}

function del2(eventTarget) {
	if (confirm("Please confirm 'DELETE' action!")) {
		__doPostBack(eventTarget, '');
	}	
	else {
		return false;	
	}

}

function dropBoxChange(eventTarget) {
	__doPostBack(eventTarget, '');	
}


function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
		
	if (num.indexOf(".") != -1) {
		num = (num * 1000000).toString();
	}
		
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
		
	return ('$' + num);
}

function formatCurrencyBox(box) {
	if (box.value.length != 0)
		box.value = formatCurrency(box.value);
}

function confirmAction(eventTarget, msg) {
	if (confirm(msg)) {
		__doPostBack(eventTarget, '');	
	}	
	else {
		return false;		
	}
}	

//-- handle multi checkbox values --
/*function copyCheckBoxListVals(pfx, id) {
    var clientId = document.getElementById(pfx).value + "_" + id;
    var box = document.getElementById(clientId);
    box.value = '';
    var count = 100 // don't know number of checkboxes, so just iterate through and break on first undefined
    var ch = null;
    for (i = 0; i < count; i++) {
        ch = document.getElementById(clientId + "_" + i.toString());
        if (!ch)
            break;
            
        if (ch.checked) {
            box.value += ch.value;
        }    
    }
    alert(box.value);
}
*/
function copyCheckBoxListVals(ctlId, id) {
    var pfx = document.getElementById(ctlId);
    if (!pfx)
        return;
        
    var box = document.getElementById(pfx.value + "_" + id);
    if (!box)
        return;
        
    box.value = '';
    var count = 100 // don't know number of checkboxes, so just iterate through and break on first undefined
    var ch = null;
    for (i = 0; i < count; i++) {
        ch = document.getElementById(pfx.value + "_ch" + id + "_" + i.toString());
        if (!ch)
            break;
            
        if (ch.checked) {
            box.value += ch.value + ",";
        }    
    }
    box.value = box.value.substring(0, box.value.length - 1); //to remove trailing ,
    
}

function fillPass(boxId, val) {
    var box = document.getElementById(boxId);
    if (box)
        box.value = val;
        
    box = document.getElementById(boxId + '2');    
    if (box)
        box.value = val;
}

function toggleEmployerType(boxName) {
    var box = eval('document.forms[0].' + boxName);
    if (!box)
        return;
     
    var val = "";    
    for(i = 0; i < box.length; i++) {
        if (box[i].checked) {
            val = box[i].value;
            break;
        }
    }
    
    var ind = document.getElementById("rIndustry");
    var cell = document.getElementById("cNumEmployed");
    
    if (val.toLowerCase() == 'recruiter') {
        ind.style.display = 'none';
        cell.innerHTML = "Number of Consultants";
    }
    else {
        ind.style.display = 'block';
        cell.innerHTML = "Number of Employees";
    }
}

function openInfoWnd(url) {
    window.open(url, 'infownd', 'status=1,toolbar=0,width=400,height=450,scrollbar=1,top=10,left=10');
}

function toggleBoxSize(elname, maxsize, minsize) {
    var box = document.getElementById(document.getElementById(g_CtlWorkBg).value + "_" + elname);
    
    var span = document.getElementById(elname + "Div");
    if (span.innerHTML.toLowerCase().indexOf('expand') != -1) {
        box.size = maxsize;
        span.innerHTML = "Collapse List";
    }
    else {
        box.size = minsize;
        span.innerHTML = "Expand List";
    }
}

function setFrmAction(action) {
    document.forms[0].action = action;

}

function gotoSD(action) {
    window.location.href = '/sd.aspx?w=' + action;
}
//--
