/* --------------------------------------------------------------------------*/
// Display or hide the specialty select box depending on the search type
// Deprecated
/*---------------------------------------------------------------------------*/
function toggleSearch(objSelect) {
    if (objSelect != null) {
        switch (objSelect.options[objSelect.selectedIndex].value) {
            case 'P':
                toggleDisplay('lbl_bizsector','hide'); 
                toggleDisplay('sel_bizsector','hide');
                toggleDisplay('lbl_specialty','show'); 
                toggleDisplay('sel_specialty','show');
                break;
            default:
                toggleDisplay('lbl_bizsector','show'); 
                toggleDisplay('sel_bizsector','show');
                toggleDisplay('lbl_specialty','hide'); 
                toggleDisplay('sel_specialty','hide');    
        }
    }
    return false;       
}
/* --------------------------------------------------------------------------*/
// Display or hide search tabs
/*---------------------------------------------------------------------------*/
function toggleSearchTab(boxId,bg,objLink) {
    var objBox = document.getElementById(boxId);
    if (objBox != null && bg != null && objLink != null) {
        if (objLink.id == 'tabProvinces') {        		
        		document.getElementById('provincesView').className = 'show';
        		document.getElementById('bizsectorsView').className = 'hide';
        }
        else {
        		document.getElementById('provincesView').className = 'hide';
        		document.getElementById('bizsectorsView').className = 'show';
        }
        objBox.style.background = 'url(/images/tabs/search/' + bg + ') 0 0 no-repeat'; 
    }
    return false;       
}
/* --------------------------------------------------------------------------*/
// Change the visibility of the object
/*---------------------------------------------------------------------------*/
function toggleDisplay(id, inline) {
    var obj = document.getElementById(id);
    if (obj != null) { 
        if (obj.className == 'hide') {
        	if (inline == true)    
            	obj.className = 'showInline';  
        	else
        		obj.className = 'show';  
        }       
        else
            obj.className = 'hide';
    }    
}
/* --------------------------------------------------------------------------*/
// Synch the selectedIndex of the select object with another select box
/*---------------------------------------------------------------------------*/
function syncOptions(objSelect,syncToId,is_min){
    if (syncToId != null) {
        var obj = document.getElementById(syncToId);
        if (objSelect != null && obj != null) {
            if (is_min) {
                if (obj.selectedIndex != (obj.options.length - 1)) {
                    if (parseInt(objSelect.options[objSelect.selectedIndex].value) >= parseInt(obj.options[obj.selectedIndex].value ))
                        obj.selectedIndex = objSelect.selectedIndex;
                }    
            }
            else {
                if (objSelect.selectedIndex != (objSelect.options.length - 1)) {
                    if (parseInt(objSelect.options[objSelect.selectedIndex].value) <= parseInt(obj.options[obj.selectedIndex].value))
                        obj.selectedIndex = objSelect.selectedIndex;
                }
            }
        }               
    }
    return false;  
}
/* --------------------------------------------------------------------------*/
// Set the value of the specified form object
/*---------------------------------------------------------------------------*/
function setValue(objId, value) {
    var obj;
    if (objId != null) {
        obj = document.getElementById(objId);
        obj.value = value;
    }    
    return false; 
}
/* --------------------------------------------------------------------------*/
// Insert options in the specified select box
// arrData = array in options.js
// selectToFill = the id of the select object to fill
// fkey = the filter key to use in the array
// selectToCheck = if specified, filter the data based on the selected value of this object
/*---------------------------------------------------------------------------*/
function setOptions(arrData, selectToFill, fkey, selectToCheck) {
   
    if (arrData == null || selectToFill == null)
        return false;
  
    var value, text, idx, objSelect, objSelectToFill;
    
    if (selectToCheck != null) { 
        objSelect = document.getElementById(selectToCheck);
        fkey = objSelect.options[objSelect.selectedIndex].value;
    }
    
    // if parent is set to all or nil, there is no need to show the options      
    if (fkey != 'all' && fkey != 'nil') {
        idx=1;
        objSelectToFill = document.getElementById(selectToFill);
        // check if helper has been set
        if (objSelectToFill.options.length > 0) {
            if (objSelectToFill.options[0].text.length == 0) {
                objSelectToFill.options[0] = null;
                idx = 0;
            }    
        } 
        else {
            objSelectToFill.disabled =  true;
            return false;    
        }
        for (var i = 0; i < arrData.length; i += 3) {
            if (arrData[i+1] == fkey) {
                objSelectToFill.options[idx] = new Option;
                objSelectToFill.options[idx].text = arrData[i+2];
                objSelectToFill.options[idx].value =arrData[i];     
                idx++;
            }
        }
        objSelectToFill.disabled =  false;
    }
    else
        document.getElementById(selectToFill).disabled=true; 
 
    return false;           
}
/* --------------------------------------------------------------------------*/
// Update the select boxes based on the new option selected in objSelect
/*---------------------------------------------------------------------------*/
function optionSelected(objSelect,module,idx) {  
     var selectProvince, selectRegion, helperType;
     if (module == null) module = 'business';
	 if (idx == null) idx = ''; 	
     switch(objSelect.id) {
        case 'contact_country_id': 
            selectProvince = document.getElementById("contact_province_id");
            // Remove options
            removeOptions(selectProvince);
            helperType = getProvinceHelperType(objSelect.options[objSelect.selectedIndex].value)
            if (helperType != null)
                setSelectHelper( 'contact_province_id', helperType, 'o');
            setOptions(arrProvinces,selectProvince.id, null,objSelect.id)
            break;    
        case module + '_province_id' + idx: 
            selectRegion = document.getElementById(module + "_region_id" + idx);
            helperType = getRegionHelperType(objSelect.options[objSelect.selectedIndex].value);            
            // Remove options
            removeOptions(selectRegion);
            // Setup helper
            setSelectHelper(selectRegion.id, helperType, 'w');
            // Fill options
            setOptions(arrRegions,selectRegion.id, null,objSelect.id)
            break;   
        default: break;
     }
     return false; 
}
/* --------------------------------------------------------------------------*/
// Insert an helper as the first option of the select object
/*---------------------------------------------------------------------------*/
function setSelectHelper(selectId, helperType, formatType) { 
    var objSelect;    
    if (selectId == null)
        return false;
    else   { 
        objSelect = document.getElementById(selectId);
    }
    
    var objLen = objSelect.options.length;
    
    objSelect.options[objLen] = new Option;
    objSelect.options[objLen].text = "";
    objSelect.options[objLen].value = ""; 
    
    for (var k = objSelect.options.length-1; k > 0; k -= 1) {
        objSelect.options[k].text = objSelect.options[k-1].text;
        objSelect.options[k].value = objSelect.options[k-1].value;     
    }
    for (var i = 0; i < arrHelpers.length; i += 4)
    {
        if (arrHelpers[i+1] == helperType && arrHelpers[i+2] == formatType) {
            objSelect.options[0] = new Option;
            objSelect.options[0].text = arrHelpers[i+3];
            objSelect.options[0].value =arrHelpers[i];
            objSelect.selectedIndex = 0;
            return false;
        }
    } 
    return false;    
}
/* --------------------------------------------------------------------------*/
// Remove the first option
/*---------------------------------------------------------------------------*/    
function removeHelper(selectId) {
    if (selectId == null)
        return false;
    else {    
        var objSelect = document.getElementById(selectId);    
        if (objSelect.options.length > 0) {
            objSelect.options[0] = null;
        }
        if (objSelect.options.length == 0)
            objSelect.disabled =  true;        
    }
    return false;
}    
/* --------------------------------------------------------------------------*/
//  Remove options from select object
/*---------------------------------------------------------------------------*/
function removeOptions(objSelect){
    var nb = objSelect.length;
    for (var d = 0; d < nb; d++)
        objSelect.options[objSelect.length-1] = null;
    return false;     
}    
/* --------------------------------------------------------------------------*/
//  Select the option that matches the value
/*---------------------------------------------------------------------------*/
function setSelectedOption(selectId,value){
    var objSelect = document.getElementById(selectId);
    if (objSelect == null)
        return false;
    for (var i=0; i < objSelect.options.length; i++) {
        if (objSelect.options[i].value == value)
            objSelect.options[i].selected = true;                
    }
    return false;    
}
/* --------------------------------------------------------------------------*/
//  check if the option value is a canadian province
/*---------------------------------------------------------------------------*/
function isCanProvince(province_id) {
    var ca_id = getCountryIdByCode('CA');
    for (var record = 0; record < arrProvinces.length; record += 3) {
       if (arrProvinces[record] == province_id && arrProvinces[record+1] == ca_id) return true;
    }
    return false;
}  
/* --------------------------------------------------------------------------*/
// check if the option value is a us state
/*---------------------------------------------------------------------------*/
function isUsState(province_id) {
    var us_id = getCountryIdByCode('US');
    for (var record = 0; record < arrProvinces.length; record += 3) {
        if (arrProvinces[record] == province_id && arrProvinces[record+1] == us_id) return true;
    }
    return false;
}
/* --------------------------------------------------------------------------*/
// check if the option value is a canadian territory
/*---------------------------------------------------------------------------*/
function isTerritory(province_id) {
    for (var record = 0; record < arrTerritoryMap.length; record += 2) {
        if (arrTerritoryMap[record+1] == province_id) return true;
    }
    return false;    
}
/* --------------------------------------------------------------------------*/
// 
/*---------------------------------------------------------------------------*/
function getCountryIdByCode(code) {
    for (var record = 0; record < arrCountryMap.length; record += 2) {
        if (arrCountryMap[record] == code) return arrCountryMap[record+1];
    }
    return false; 
}
/* --------------------------------------------------------------------------*/
// 
/*---------------------------------------------------------------------------*/
function getRegionHelperType(province_id) {    
    if (isTerritory(province_id)) return 't';
    if (isCanProvince(province_id)) return 'p';
    if (isUsState(province_id)) return 's';
    return false; 
}
/* --------------------------------------------------------------------------*/
// 
/*---------------------------------------------------------------------------*/
function getProvinceHelperType(country_id) {    
    if (country_id == getCountryIdByCode('CA'))
        return 'p';
    if (country_id == getCountryIdByCode('US'))
        return 's';
    return null;    
}
/* --------------------------------------------------------------------------*/
//  Fonctions pour la messagerie
/*---------------------------------------------------------------------------*/
function closeMsg(message_id) {
	if (message_id != null) {
		var msg;
        msg = document.getElementById('msgReply' + message_id);
        msg.innerHTML = '';
        toggleDisplay('linkClose' + message_id, true);  
        toggleDisplay('linkReply' + message_id, true);
        toggleDisplay('txtReply' + message_id, true);       
	}
}

function replyToMsg(message_id) { 
    if (message_id != null) {            
        var msg, subject, form;
        msg = document.getElementById('msgReply' + message_id);
        subject = document.getElementById('msgSubject' + message_id); 
        form = document.getElementById("messageReplyForm");            
        msg.innerHTML = form.innerHTML;
        msg.className = msg.className + ' mt20';
        document.getElementById('message_parent_id').value = message_id;
        document.getElementById('message_subject').value = 'RE: ' + subject.innerHTML;        
        // Disable reply link
        toggleDisplay('linkClose' + message_id, true);
        toggleDisplay('linkReply' + message_id, true);
        toggleDisplay('txtReply' + message_id, true);           
    }    
}

function deleteMsgs(fid,msg_select, msg_no_select) {	
	var f,i,element,checked_found;
	f = document.getElementById(fid)
	checked_found = false;
	if (f != null) {
		for (var i=0; i<f.length; i++) {					
			if (f.elements[i].type == "checkbox") {						
				if (f.elements[i].checked == true) {					
					checked_found = true;
					if(confirm(msg_select)) {						
						f.submit();
						return false;
					}		
				}			
			}			
		}
	}
	if (checked_found == false)
	{
		alert(msg_no_select);
		return false;	
	}
} 
   
/* --------------------------------------------------------------------------*/
// Check if the terms and conditions were accepted
/*---------------------------------------------------------------------------*/
function validateTerms(checkId, msg) {			
	checkObj =  document.getElementById(checkId);		
	if (checkObj != null) {
		if (checkObj.checked)	
			return true;		
	}
	alert(msg);
	return false;
}

/* --------------------------------------------------------------------------*/
// Returns true is it is an array
/*---------------------------------------------------------------------------*/
function isArray(obj)
{
	if (isNaN(obj.length))
		return false;
	else
		return true;
}
