/*-------------------------------------------------------------------------------------------------
	Feature Part
-------------------------------------------------------------------------------------------------*/

function doSearchLog(iRandom, sCountry, iCnt, sParam)
{
	var callbackLog = {};
	if(typeof(sParam) == 'undefined') sParam = '';
	var sUrl = dPath + "/"+sCountry+"/job-search-log.php?rnd="+iRandom+"&cnt="+iCnt+"&"+sParam;
	var callbackLog = {};
	//Optional to assign timeout in ms.
	callbackLog.timeout = 8000; 
	//Do request
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callbackLog, null);  
}

function sendFeedBack(sCountry)
{
	var aObj = document.getElementsByName('fb[]');
	var bResult = false;
	var iId = "";
	
	for (var i =0; i<aObj.length; i++)
	{
		if(aObj[i].checked == true) iId = aObj[i].value;		
	}
	if(iId == "") 
	{
		alert("Please select one option.");
		return false;
	}	
	sMsg = document.feedback.fbmsg.value;
	var iRandom = Math.random();
	var sUrl = dPath + "/malaysia/feedback.php?fbid="+iId+"&fbmsg="+sMsg+"&country="+sCountry+"&rnd="+iRandom;
	var callbackFb = {};
	callbackFb.success = updFb;
	callbackFb.timeout = 8000; 
	//Do request
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callbackFb, null);   
}

function updFb()
{
	var oFbCon = document.getElementById('fbCon');
	sHtml = "<div class='enter'></div><div>Thank you for your feedback.</div><div class='enter'></div>";
	if(oFbCon != null) oFbCon.innerHTML = sHtml;	
}

/*-------------------------------------------------------------------------------------------------
	Logic Validate Part
-------------------------------------------------------------------------------------------------*/

function ctrlYoe(bMin)
{
	oMin = getEl('experience-min');
	oMax = getEl('experience-max');
	iMin = Number(oMin.value);
	iMax = Number(oMax.value);
	if((iMax< iMin) && iMax != '-1')
	{
		if(bMin == true) oMax.value = '-1';
		else  oMin.value = '-1';
	}	
}

function ctrlMaxSel(oCtrl, sDiv, sLabel, iMax)
{
	bCheck = checkRequiredCb(oCtrl, '0,' + iMax , false);
	if(bCheck == false)
	{
		setObjVisibility(sDiv, 'true'); 
		setDivMsg(sDiv, 'Please select maximum 5 ' + sLabel + ' only.');	
		setDivClass(sDiv, 'errorReg');
		oCtrl.checked = false;
		updateThis(oCtrl, false, true);
	}
	else
	{
		setDivMsg(sDiv, '');
		setObjVisibility(sDiv,'false'); 
	}
}

/*-------------------------------------------------------------------------------------------------
	Common Part
-------------------------------------------------------------------------------------------------*/
//Get Element
function getEl(sId) 
{ 
	return document.getElementById(sId); 
}

function hideEl(sId) 
{ 
	getEl(sId).style.visibility = 'hidden';
	getEl(sId).style.position = 'absolute';
}

function showEl(sId) 
{ 
	getEl(sId).style.visibility = 'visible';
	getEl(sId).style.position = 'static';
}

function hidediv(sName) {
    if (document.getElementById) { // DOM3 = IE5, NS6
	document.getElementById(sName).style.visibility = 'hidden';
	document.getElementById(sName).style.display = 'none';
    } else {
		if (document.layers) { // Netscape 4
			document.layers[sName].visibility = 'hidden';
			document.layers[sName].display = 'none';
		} else { // IE 4
			document.all[sName].style.visibility = 'hidden';
			document.all[sName].style.display = 'none';
		}
    }
}

function showdiv(sName) {
    if (document.getElementById) { // DOM3 = IE5, NS6
	document.getElementById(sName).style.visibility = 'visible';
	document.getElementById(sName).style.display = 'block';
    } else {
	if (document.layers) { // Netscape 4
	    document.layers[sName].visibility = 'visible';
	    document.layers[sName].display = 'block';
	} else { // IE 4
	    document.all[sName].style.visibility = 'visible';
	    document.all[sName].style.display = 'block';
	}
    }
} 

function disableThis(oCtrl)
{
	oCtrl.disabled=true;
}

function enableThis(oCtrl)
{
	oCtrl.disabled=false;
}

function updateInnerHtml(sCtrl, sHtml)
{
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(sCtrl).innerHTML = sHtml;
	} else {
		if (document.layers) { // Netscape 4
			document.layers[sCtrl].innerHTML = sHtml;
		} else { // IE 4
			document.all[sCtrl].innerHTML = sHtml;
		}
	}
}

/*-------------------------------------------------------------------------------------------------
	Criteria
-------------------------------------------------------------------------------------------------*/
function disableAll(field)
{
	field = document.getElementsByName(field);
    for (i = 0; i < field.length; i++) {
		field[i].disabled = true ;	
    }
}

function checkAll(field) {
	field = document.getElementsByName(field);
    for (i = 0; i < field.length; i++) {
	field[i].checked = true ;
	updateThis(field[i], false, true);
    }
}

function uncheckAll(field) {
	field = document.getElementsByName(field);
    for (i = 0; i < field.length; i++) {
	field[i].checked = false ;
	updateThis(field[i], false, true);
	enableThis(field[i]);
    }
}

//Create this function because we mix name for location and oversea but diff handle on UI
function checkAllLoc(bLoc, bOvs, sCurLoc)
{
	if(typeof(sCurLoc) == 'undefined') sCurLoc = "";
	field = document.getElementsByName('location[]');
    for (i = 0; i < field.length; i++) {
		if (field[i].checked && field[i].disabled) continue;
		iId = field[i].id;
		iId = iId.substring(8);
		sKey = iId.substring(iId.length-4, iId.length); 
		sKey2 = iId.substring(0,3);
		if ( bOvs == 0 && bLoc == 1 && (iId == '90100' || sKey == '0000' || sKey2 == '901')) continue;
		if ( bOvs == 1 && bLoc == 0 && (iId != '90100' && sKey != '0000')) continue;
		if( sCurLoc == iId && bOvs == 1 && bLoc == 0) continue;		
		field[i].checked = true ;		
		updateThis(field[i], false, true);
		toggleCb(field[i]);	
    }	
}

//Create this function because we mix name for location and oversea but diff handle on UI
function uncheckAllLoc(bLoc, bOvs, sCurLoc)
{
	if(typeof(sCurLoc) == 'undefined') sCurLoc = "";
	field = document.getElementsByName('location[]');
    for (i = 0; i < field.length; i++) {
		if (field[i].checked == false && field[i].disabled == false) continue;
		iId = field[i].id;
		iId = iId.substring(8);
		sKey = iId.substring(iId.length-4, iId.length);
		sKey2 = iId.substring(0,3);
		if ( bOvs == 0 && bLoc == 1 && (iId == '90100' || sKey == '0000' || sKey2 == '901')) continue;
		if ( bOvs == 1 && bLoc == 0 && (iId != '90100' && sKey != '0000' && sKey2 != '901')) continue;
		if( sCurLoc == iId && bOvs == 1 && bLoc == 0) continue;		
		field[i].checked = false ;
		updateThis(field[i], false, true);
		enableThis(field[i]);
		toggleCb(field[i]);
		
		//check short list
		sSL = 'Sl' + field[i].id;
		oSl = getEl(sSL);
		if(oSl != null) enableThis(oSl);
    }	
}

function updateThisScan(sName)
{
	var aCtrl = document.getElementsByName(sName);
	for (var x = 0; x < aCtrl.length; x++) updateThis(aCtrl[x], false, true);		
}

function updateRadioSet(what) {	
	var field = what.name;
	field = document.getElementsByName(field);
	for (i = 0; i < field.length; i++) {
	updateThis(field[i], false, true);
    }
}

function updateThisById(sId, sIdMirror)
{
	oCtrl = getEl(sId);
	if(oCtrl == null) return;
	oCtrl2 = getEl(sIdMirror);
	if(oCtrl2 == null) return;
	oCtrl.checked = oCtrl2.checked;
	updateThis(oCtrl);
}

function updateThis(what, bAvoid, bScan) {
    var selectedClassName = "labelSelected";
    var normalClassName = "labelNormal";

    if( what.checked == true ){what.parentNode.className = selectedClassName;}
	else{what.parentNode.className = normalClassName;}
	
	if(typeof(bAvoid) == 'undefined') bAvoid = false;
	updateThisDesc(what, bAvoid);	

	if(typeof(bScan) == 'undefined') bScan = false;
	if(bScan == false) updateThisDetail(what);	

	//check short list
	sSL = 'Sl' + what.id;
	oSl = getEl(sSL);
	if(oSl != null) oSl.checked = what.checked;
}

function updateThisDesc(what, bAvoid)
{
	if(typeof(bAvoid) == 'undefined') bAvoid = false;
	sName = what.name;
	sName = sName.replace("[]","") + 'Sel';
	//hardcode
	if(sName == 'roleSel') sName = 'specializationSel';
	sDesc = trim(what.alt);
	if(sDesc == '') return;
	
	var oDiv = document.getElementById(sName);
	if(oDiv == null) return;

	sLegend = '<b>Your Selection: </b><br/>';
	
	sHtml = oDiv.innerHTML;
	if(typeof(sHtml) == 'undefined' || sHtml == '') {sHtml = ', ';} 
	else{ sHtml = sHtml.substring(27) ; sHtml = ', ' + sHtml + ', ';}	
	sSearch = ', ' + escapeHTML(sDesc) + ', ';
	if( what.checked == true && bAvoid == false && what.disabled == false)
	{		
		bFound = sHtml.search(sSearch);
		if(bFound == -1) sHtml = sHtml + sDesc + ', ';
	}
	else sHtml = sHtml.replace(sSearch, ', ') ;	 
	if(sHtml.length > 4){ sHtml = sHtml.substring(2, (sHtml.length - 2) ); sHtml = sLegend + sHtml;}
	else {sHtml = '';}
	oDiv.innerHTML = sHtml;
}

//This is customize function
function updateThisDetail(oCtrl)
{	
	if(typeof(updateThisExternal) == 'function') updateThisExternal(oCtrl);
}

/*-------------------------------------------------------------------------------------------------
	Toogle Specialization Role
-------------------------------------------------------------------------------------------------*/
function toggleSpeRole(iSpe, bShow)
{
	sKey = 'optSpeRole' + iSpe;
	sLblKey = 'lblTogSpeRole' + iSpe;
	if(typeof(bShow) == 'undefined')
	{
		var oCtrl = getEl(sKey);
		if(oCtrl.style.display == '' || oCtrl.style.display =='none') bShow = true;
		else  bShow = false;
	}	
	if(bShow)  
	{
		getEl(sLblKey).innerHTML = 'Hide Options';
		showdiv(sKey);		
	}
	else
	{
		getEl(sLblKey).innerHTML = 'More Options';
		hidediv(sKey);		
	}
}



/*-------------------------------------------------------------------------------------------------
	Toogle Check Box Tree - location
-------------------------------------------------------------------------------------------------*/
function toggleCbById(sId)
{
	oCtrl = getEl(sId);
	if(oCtrl == null) return;
	toggleCb(oCtrl);
}

/*Old logic: Using class name. For all same elements (same name)*/
function toggleCb(oCtrl)
{
	sName = oCtrl.name;
	bCheck = oCtrl.checked;
	sStart = oCtrl.parentNode.parentNode.className;	
	bStart = false; 
	var oEl = document.getElementsByName(sName);	
	for (x = 0; x < oEl.length; x++) 
	{		
		if(oEl[x].id == oCtrl.id && bStart == false) { bStart = true; continue; }					
		
		if(bStart == false) continue;

		sParantCls = oEl[x].parentNode.parentNode.className;

		if(sParantCls == "") continue;
		if(sParantCls == sStart && bStart == true) break;		
		if(sParantCls < sStart && bStart == true) break;

		oEl[x].checked = bCheck;
		updateThis(oEl[x], true, true);
		if(bCheck) disableThis(oEl[x]);
		else enableThis(oEl[x]);

		//check short list
		sSL = 'Sl' + oEl[x].id;
		oSl = getEl(sSL);
		if(oSl != null) 
		{
			if(bCheck) disableThis(oSl);
			else enableThis(oSl);	
		}
	}			
}

/*New logic: Store parent id in parent element's id*/
function toggleCbSpe(oCtrl)
{
	sName = 'role[]';
	bCheck = oCtrl.checked;
	iSpe = oCtrl.value;
	var oEl = document.getElementsByName(sName);	
	for (x = 0; x < oEl.length; x++) 
	{		
		sParentSpe = oEl[x].parentNode.parentNode.id;
		sParentSpe = sParentSpe.substring(6);
		if(sParentSpe != iSpe) continue;

		oEl[x].checked = bCheck;
		updateThis(oEl[x], true, true);
		if(bCheck) disableThis(oEl[x]);
		else enableThis(oEl[x]);		
	}			
}

function toggleAllSpe(sId)
{
	oCtrl = document.getElementById(sId);
	if(oCtrl != null) toggleCbSpe(oCtrl);
}

function toggleAllLoc(sId)
{
	oCtrl = document.getElementById(sId);
	if(oCtrl != null) toggleCb(oCtrl);
}

/*-------------------------------------------------------------------------------------------------
	Toogle Quick Search
-------------------------------------------------------------------------------------------------*/
function toggleQs(sName)
{
	var sCtrl = sName + "Con";
	var oCon = document.getElementById(sCtrl);
	if(oCon.style.display == '' || oCon.style.display =='none') showdiv(sCtrl);
	else hidediv(sCtrl);
	updateOptSel(sName);
}

function updateOptSel(sName) 
{	
	var sCtrl = sName + "Sel";
	var sHtml = getCheckBoxSet(sName) ;
	updateInnerHtml(sCtrl, sHtml);
}

function getCheckBoxSet(sName)
{
	var aRes = Array();
	if(sName == 'qsSpe') aRes = getCheckBoxLabel(sName, document.getElementsByName('specialization[]'));
	if(sName == 'qsInd') aRes = getCheckBoxLabel(sName, document.getElementsByName('industry[]'));
	if(sName == 'qsLoc') 
	{ 
		var aRes = getCheckBoxLabel(sName, document.getElementsByName('location[]'));
		aRes = getCheckBoxLabel(sName, document.getElementsByName('country[]'), aRes[0], aRes[1], aRes[2]);		
	}	
	if(typeof(aRes[0]) != 'undefined') return aRes[0];
	return "";
}

function getCheckBoxLabel(sName, elN, sHtml, iCount, iAdd)
{
	var limit = 1;
    var additional = 0;
    var count = 0;
	var STR_FILLER = "";
	var STR_NONE = getDefLabelDesc(sName);
    var STR_ALL = "All";  
	if(typeof(sHtml) == 'undefined' || sHtml == STR_NONE) var sHtml = "";
	if(typeof(iCount) != 'undefined') count = count + iCount;
	if(typeof(iAdd) != 'undefined' && iAdd!=0)
	{
		additional = additional + iAdd;
		sHtml = sHtml.replace(" ... " + additional + " more" , "" );
	}
	limit = limit - count;
	if(limit <= 0) limit = 0;
	
	for(i = 0; i < elN.length; i++){
		if ( elN[i].checked == true && limit > 0) 
		{	    			
			var labels = document.getElementsByTagName('label');
			var mylabel = "";
			for (x = 0; x < labels.length; x++) {
				if(labels[x].htmlFor == elN[i].id) {
					mylabel = labels[x].innerHTML;
				}
			}			
			sHtml += STR_FILLER +"" + mylabel + "";
			limit--;
			count++;
			STR_FILLER = ", ";
		} else if (elN[i].checked == true && limit == 0) {
			additional++;
			count++;
		}
    }

	if (count == 0) sHtml = STR_NONE; 
//	else if (count == elN.length && count != 1) { sHtml = STR_ALL; } 
	else if (limit == 0 && additional > 0) {var ENDSTR = " ... " + additional + " more"; sHtml += ENDSTR ; }
	
	var aRes = Array();
	aRes[0] = sHtml;
	aRes[1] = count;
	aRes[2] = additional;
	return aRes;
}

function getDefLabelDesc(sName)
{
	if(sName == 'qsSpe') return "Select specialization";
	if(sName == 'qsLoc') return "Select location";
	if(sName == 'qsInd') return "Select industry";
	return false;
}





/*-------------------------------------------------------------------------------------------------
	Do advance search
-------------------------------------------------------------------------------------------------*/
function doAdvanceSearch(sUrl)
{
	//Advance search possible param
	var sParam = "";
	sParam = sParam + asGetText('key');
	sParam = sParam + asGetCBList('area');
	sParam = sParam + asGetText('option');
	sParam = sParam + asGetCBList('location', 1);
	sParam = sParam + asGetCBList('industry', 1);
	sParam = sParam + asGetCBList('specialization', 1);
	sParam = sParam + asGetCBList('role', 1);
	sParam = sParam + asGetCBList('position', 1);
	sParam = sParam + asGetCBList('job-type', 1);
	sParam = sParam + asGetCBList('qualification', 1);
	sParam = sParam + asGetCBList('field-of-study', 1);
	sParam = sParam + asGetText('nationality');
	sParam = sParam + asGetText('experience-min');
	sParam = sParam + asGetText('experience-max');
	sParam = sParam + asGetText('salary');	
	sParam = sParam + asGetCBList('classified');
	sParam = sParam + asGetCBList('salary-option');
	sParam = sParam + asGetText('salary-currency');
	sParam = sParam + asGetCBList('job-posted', 1);
	sParam = sParam + asGetHidden('campus');
	sParam = sParam + asGetHidden('src');
	
	if(sParam != "") 
	{
		sParam = sParam.substring(1, sParam.length);		
		sUrl = sUrl + '?' + sParam;
	}
	
	document.location.href = sUrl;	
}

function asGetText(sKey)
{
	oCtrl = getEl(sKey);
	if(oCtrl != null) 
	{
		sVal = trim(oCtrl.value);
		if(sVal != "") return '&' + sKey + '=' + asUrlEncode(sVal);
	}
	return '';
}

function asGetCBList(sKey, bArray)
{
	sCtrl = sKey;
	if(typeof(bArray) !='undefined' && bArray == 1) sCtrl = sCtrl + '[]';
	oCtrl = document.getElementsByName(sCtrl);
	var sParam = '';
    for (i = 0; i < oCtrl.length; i++) {
		if(oCtrl[i].checked  && oCtrl[i].disabled == false)
		{
			sParam = sParam + oCtrl[i].value + ',';
		}	
    }
	if(sParam != '')
	{
		sParam = sParam.substring(0, sParam.length-1);	
		sParam = '&' + sKey + '=' + asUrlEncode(sParam);
	}
	return sParam;
}

function asGetHidden(sKey)
{
	oCtrl = document.getElementsByName(sKey);
	var sParam = '';
    for (i = 0; i < oCtrl.length; i++) {		
		sParam = sParam + oCtrl[i].value;			
    }	
	if(sParam != '')
	{		
		sParam = '&' + sKey + '=' + sParam;
	}
	return sParam;
}

function asUrlEncode(str)
{
	var ret = str;     
	ret = ret.toString();    
	ret = encodeURIComponent(ret);    
	ret = ret.replace(/%20/g, '+');     
	return ret;
	//return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

/*-------------------------------------------------------------------------------------------------
	Banner Part - external
-------------------------------------------------------------------------------------------------*/
// The id/name for flash banner
leaderboard_id = "homepage_leaderboard";

// Setup expandable leaderboard
function jobstreet_setup_expleaderboard(file, width, height, expwidth, expheight) {
      if(!expwidth) {
            expwidth = width;
      }
      if(!expheight) {
            expheight = height;
      }
      jobstreet_writeFlash(leaderboard_id, file, expwidth, expheight, "transparent", "");
      jobstreet_shrink_leaderboard();
} 

// Call this from flash banner to expand your ad
function jobstreet_expand_leaderboard() {
      jobstreet_getFlash(leaderboard_id).height = 120;
}

// Call this from flash banner to shrink your ad
function jobstreet_shrink_leaderboard() {
      jobstreet_getFlash(leaderboard_id).height = 60;
}

// Function to get flash id/name
function jobstreet_getFlash(id) {
      if(navigator.appName.indexOf("Microsoft") != -1) {
            return window[id];
      }else {
            return document[id];
      }
}

// Function to write the flash
function jobstreet_writeFlash(id, file, width, height, wmode, params) {
      var flashTag = '';
      flashTag += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
      flashTag += 'id="' + id + '" ';
      flashTag += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#" ';
      flashTag += 'width="' + width + '" ';
      flashTag += 'height="' + height + '">';
      flashTag += '<param name="movie" value="' + file + '"/>';
      flashTag += '<param name="wmode" value="' + wmode + '"/>';
      flashTag += '<param name="quality" value="high"/>';
      flashTag += '<param name="flashvars" value="' + params + '"/>';
      flashTag += '<param name="allowscriptaccess" value="always"/>';
      flashTag += '<embed src="' + file + '"';
      flashTag += ' width="' + width + '"';
      flashTag += ' height="' + height + '"';
      flashTag += ' type="application/x-shockwave-flash"';
      flashTag += ' name="' + id + '"';
      flashTag += ' allowscriptaccess="always"';
      flashTag += ' quality="high"';
      flashTag += ' wmode="' + wmode + '" ';
      flashTag += ' flashvars="' + params + '" ';
      flashTag += ' swliveconnect="true" ';
      flashTag += ' pluginspage="http://www.macromedia.com/go/getflashplayer">';
      flashTag += '</embed>';
      flashTag += '</object>';
      document.write(flashTag);
}