//
// Cross Browsers JS Model
//
var ObjectsType;

if(document.getElementById) ObjectsType="DOM2";
else if(document.all) ObjectsType="IEDOM1"
else if(document.layers) ObjectsType="NCDOM1";

function GetStyleById(IdName){
    switch(ObjectsType){
        case "DOM2":
            if (GetObjectById(IdName)) {
                return document.getElementById(IdName).style;
			}
            break;
        case "IEDOM1":
			if (GetObjectById(IdName)) {
                return document.all[IdName].style;
			}
            break;
        case "NCDOM1":
            return document.layers[IdName];
    }
}

function GetObjectById(IdName, FormName){
    switch(ObjectsType){
        case "DOM2":
            if (FormName) {
                return eval('document.getElementById("'+FormName+'").'+IdName) ;
            } else
                return document.getElementById(IdName);
        case "IEDOM1":
            if (FormName)
                return eval('document.all["'+FormName+'"].'+IdName) ;
            else
                return document.all[IdName];
        case "NCDOM1":
            if (FormName)
                return eval('document.layers["'+FormName+'"].'+IdName) ;
            else
                return document.layers[IdName];
    }
}

function GetObjectByName(IdName, FormName){
    switch(ObjectsType) {
        case "DOM2":
            if (FormName) {
                return eval('document.getElementsByName("'+FormName+'").'+IdName) ;
            } else
                return document.getElementsByName(IdName);
        case "IEDOM1":
            if (FormName)
                return eval('document.all["'+FormName+'"].'+IdName) ;
            else
                return document.all[IdName];
        case "NCDOM1":
            if (FormName)
                return eval('document.layers["'+FormName+'"].'+IdName) ;
            else
                return document.layers[IdName];
    }
}

function MyNewWindow(mypage,myname,w,h,scroll,ret,fullscreen) 
{
	// showModalDialog for IE
 	// MyNewWindow() - centering new window if user's screen resolution > 800x600
 	if (h<=0) { h = 480; }
 	if (w<=0) { w = 640; }
 	var scW = screen.availWidth ? screen.availWidth : screen.width;
	var scH = screen.availHeight ? screen.availHeight : screen.height;
	var winl = (scW-w)/2; 
	var wint = (scH-h)/2;
	
  	if (!screen) {
	    winl = 0; wint = 0;
	}
  	var settings  ='height='+h+',';
      	settings +='width='+w+',';
      	settings +='top='+wint+',';
      	settings +='left='+winl+',';
      	settings +='status=0,';
      	settings +='scrollbars='+scroll+',';		
      	settings +='fullscreen='+fullscreen+',';
      	settings +='resizable=yes';
    win = null;
  	win=window.open(mypage,myname,settings);
    win.opener = window;
  	if( !window.opera ) { win.moveTo(winl,wint); }
  	if (parseInt(navigator.appVersion) >= 4) {win.window.focus();}
  	if (ret) { return win ; }
}

function _showPopup(modal,url,value,properties,winname,alignment) {
    if (modal && !properties) properties = "dialogWidth=230px;dialogHeight=155px;status=0;scroll=0;help=0" ;
    if (modal && typeof window.showModalDialog != 'undefined') {
        var thevalues = value.split(/,/);
        return window.showModalDialog(url, thevalues, properties);
    }

    if (!properties) {
        var w = 640 ;
        var h = 480 ;
        var scW = screen.availWidth ? screen.availWidth : screen.width;
        var scH = screen.availHeight ? screen.availHeight : screen.height;
        var winl = (scW-w)/2;
        var wint = (scH-h)/2;
        properties = 'width='+w+',height='+h+',status=0,scrollbars=1,top='+wint+',left='+winl ;
    }
    if (alignment) {
        switch(alignment) {
            case "center":
                var w = properties.replace(/^.*width=(\d+),.*$/i, '$1') ;
                var h = properties.replace(/^.*height=(\d+),.*$/i, '$1') ;
                var scW = screen.availWidth ? screen.availWidth : screen.width;
                var scH = screen.availHeight ? screen.availHeight : screen.height;
                var winl = (scW-w)/2;
                var wint = (scH-h)/2;
                properties = properties + ',resizable=1,top='+wint+',left='+winl ;
                break;
        }
    }
    var win=window.open(url,winname?'POPUP'+winname:'_BLANK',properties);    
    if (!win) {
        alert('We have detected that you are using popup blocking software. Please turn off popup blocking for this site!');
    }
    //win.resizeTo(w,h);
  	if (parseInt(navigator.appVersion) >= 4) {win.window.focus();}
    else win.focus();
    return win ;
}

function _selectall(tableName, state) {
    // applicable to table with rows and cols only, no nested tables
    var tbl = GetObjectById(tableName) ;
    if (!tbl) return ;
    for (i = 0; i < tbl.rows.length; i++) {
        for (j = 0; j < tbl.rows[i].cells.length; j++) {
            for (k =0; k < tbl.rows[i].cells[j].childNodes.length; k++) {
                cb = tbl.rows[i].cells[j].childNodes[k];
                if (!cb || cb.type != "checkbox") continue;
                if (cb.disabled) 
                    cb.checked = false;
                else
                    cb.checked = state ;
            }
        }
    }
}

function xmlGet(IDTag, xmlURL, postAction) {
    var Area = GetObjectById(IDTag);
    //Area.innerHTML = 'Retrieving in progress, please wait...';
    var xmlHttp = XmlHttp.create();
    xmlHttp.open('GET', xmlURL+'&r='+Math.random(), true);
    xmlHttp.onreadystatechange = function()
    {
        if (xmlHttp.readyState == 4) {
            Area.innerHTML = xmlHttp.responseText.replace(/[\r\n]+$/,'') ;
            if (postAction) eval(postAction);
        }
    }
    xmlHttp.send(null);
}

function doOrdering(InputName, type) {
    var oSelect = document.getElementById(InputName);
    var swap;

    if (oSelect.options.length==0) return;
    if (type == 1 && !oSelect.options[0].selected) {
        for (var i = 0; i < oSelect.options.length; i++) {
            if (oSelect.options[i].selected) {
                swap = oSelect.options[i-1];
                if (!swap.selected) oSelect.options[i].swapNode(swap);
            }
        }
    } else if (type == 2 && !oSelect.options[oSelect.options.length-1].selected) {
        for (var i = oSelect.options.length - 1; i >= 0; i--) {
            if (oSelect.options[i].selected) {
                swap = oSelect.options[i+1];
                if (!swap.selected) oSelect.options[i].swapNode(swap);
            }
        }
    }
}

function _setDefault (attr_id, val) {
    try {
        var Inp = GetObjectById('INP['+attr_id+'][]');    
    } catch(e) {
        return false;
    }
    if (!Inp) return false;
    if (val==null || val=="" ) return false;
    smart_val = val.split('|');
    switch (Inp.type) {
    case "text":
    case "textarea":
    case "password":
    case "hidden":
        
        if (val!='') Inp.value = val ;
        if (smart_val[1]!= "") {
            try {
                var smart = GetObjectById(''+attr_id+'');    
            } catch(e) {
                return false;
            }
            if (!smart) return;
            smart.value = smart_val[1];
            Inp.value = smart_val[0]; //overwrite it with the smart value
        }
        
        
        break;
    case "select-one":
        for (var j = 0; j < Inp.options.length; j++) {
            v = GetObjectById('INP['+attr_id+']['+Inp.options[j].value+']');
            if (v!=null && (Inp.options[j].value==val || v.innerHTML.toUpperCase()==val.toUpperCase())) {
                Inp.selectedIndex = j;
                break;
            }
        }
        break;
    case "select-multiple":
        val = ','+val+',';
        for (var j = 0; j < Inp.options.length; j++) {
            v = GetObjectById('INP['+attr_id+']['+Inp.options[j].value+']');
            if (v!=null && (val.match(','+Inp.options[j].value+',') || val.toUpperCase().match(','+v.innerHTML.toUpperCase()+','))) 
                Inp.options[j].selected = true;
            else
                Inp.options[j].selected = false;
        }
        break;
    case "radio":
        var Inp2 = GetObjectByName('INP['+attr_id+'][]');
        for (var j = 0; j < Inp2.length; j++) {
            if (Inp2[j].value==val) { 
                Inp2[j].checked = true;
                break;
            }
        }
        break;
    case "checkbox":
        var Inp2 = GetObjectByName('INP['+attr_id+'][]');    
        val = ','+val+',';
        for (var j = 0; j < Inp2.length; j++) {
            v = GetObjectById('INP['+attr_id+']['+Inp2[j].value+']');
            if (v!=null && (val.match(','+Inp2[j].value+',') || val.toUpperCase().match(','+v.innerHTML.replace("+","\\+").toUpperCase()+','))) 
                Inp2[j].checked = true;
            else
                Inp2[j].checked = false;
        }
        break;
    }
}

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else{
    obj.removeEventListener( type, fn, false );
  } 
}

function showhide(id){
    if (document.getElementById){
        if (document.getElementById(id)){
            obj = document.getElementById(id);
            if (obj.style.display == "none"){
                obj.style.display = "";
            } else {
                obj.style.display = "none";
            }
        }
    }
}

function default_move(target_LOV, destination_LOV){
    //select all from the target
    $("#"+target_LOV+" option").attr("selected","selected");
    moving(target_LOV, destination_LOV);
}

function moving(selected_LOV, destination_LOV){
    $('#'+selected_LOV+' option:selected').remove().appendTo('#'+destination_LOV);
}

function exchange(item1, item2) {
    //item1 = item2;
    var text1, value1, text2, value2;
    value1 = $(item1).val();
    text1 = $(item1).text();
    // check if item1 is an selected item
    var selected_Val = $(item1).attr("selected");
    value2 = $(item2).val();
    text2 = $(item2).text();
    
    setStatus(item1, "", text2, value2);
    setStatus(item2, "selected", text1, value1);
}

function setStatus(obj, selected_Val, new_text, new_val){
    $(obj).text(new_text);
    $(obj).val(new_val);
    $(obj).attr("selected", selected_Val);
}

function reOrdering(lov_ID, action){
    var lov = $('#'+lov_ID+' option');
    var lov_Length = $(lov).length;
    // get the selected LOV
    var exchangedOpt;
    var new_seq = 0;
    var item2 = "";
    var exhangeOpt = "";
    var selectedTotal = 0;
    //var selectedOpt =  $("#"+lov_ID+" option["+lov_selectedIndex+"]");
    var curr_Opt = "";
    if (action == "up"){
        for(var i=0; i<lov_Length; i++){
            curr_Opt = $(lov)[i];
            if($(curr_Opt).is(':selected')){
                    new_seq = i - 1;
                    if (new_seq >= 0){
                        exhangeOpt = $(lov)[new_seq];
                        //exchange(exhangeOpt, curr_Opt);
                        exchange(curr_Opt, exhangeOpt);
                    }else{
                        break;
                    } 
            }
        }
    }else if (action == "down"){
        var limit = lov_Length -1;
        for(var i=limit; i>=0; i--){
            curr_Opt = $(lov)[i];
            if($(curr_Opt).is(':selected')){
                    new_seq = i + 1;
                    if (new_seq <= limit){
                        exhangeOpt = $(lov)[new_seq];
                        //exchange(exhangeOpt, curr_Opt);
                        exchange(curr_Opt, exhangeOpt);
                    }else{
                        break;
                    } 
            }
        }
    }
}

function submitTMDialForm(flag , dashboardmode){
    if (flag == 1){
        // we need to reset project id =0 when in dashboard level after user re-click on "submit" button
        // else a wrong query will be populated
        document.getElementById("project_id").value=0;
    }
    if (dashboardmode == 1)  document.REPORT_FORM.detail_camp_id.value='';
    document.filter.submit();
}

