﻿/* 
==================================================================
    Global Javascript Library For Work Opportunities: WouWeb Apps
    v1.1, 6/2006
==================================================================
*/

function outerHTML(obj) {
    var tsd = document.createElement("div");
    var copyOb = obj.cloneNode(true);
    tsd.appendChild(copyOb);
    return tsd.innerHTML
} 

function callFunction(winObj, functionName, args) {

    var func = functionName + '(\'' + args + '\')';
    if (winObj.execScript) {
        winObj.execScript(func, 'javascript');
        return null; 
    }
    winObj.eval(func);
    return null;
}

function swapClass(obj, cls) {
  obj.className = cls;
}

function Test(msg) {
    alert("Msg: " + msg);
}

function confirmItem(msg) {
	return confirm(msg)
}

function inspect(obj) {
    var text = ''
    
    var count = 0
    for (var i in obj) {
        if (i.indexOf("HTML") <=0) {
            if (count%2!=0) {
                text += i + ': ' + obj[i] + "\n";
            }
            else {
                text += i + ': ' + obj[i] + "\t\t";
            }
            count++;
        }
        
    }
  
    return text;
}

//******************Window Functions********************************

function goBack() {
	history.go(-1);
}

function cancelPopup() {
	self.close();
}

function closePopup(forward) {
	if (forward!="") {
		opener.location=forward;
	}
	else {
		opener.location.reload();
	}
	self.close();
}

function closeOpener() {
	opener.close();
}



function openWindow(url, name, attributes, w, h, center) {

	if (attributes.indexOf("status") < 0) {
		attributes += ", status";
	} 
	
	var str = "'" + attributes + ", width=" + w + ", height=" + h;

	if (center=="true") {
		var l = Math.floor((screen.width - w) / 2);
		var t = Math.floor((screen.height - h) / 2);
	    str += ", left=" + l + ", top=" + t
	}
	str += "'"

	var newWindow = window.open(url, name, str);
	newWindow.focus();
}

function showStatus(win, msg) {
	win.status = msg;
}

function hideStatus(win) {
	win.status=win.defaultStatus;
}



//******************Date Utility Functions********************************

function getDate() {
	var now = new Date();
	var month = convertMonth(now.getMonth());
	var year = now.getYear();
	var day = convertDay(now.getDay());
	var date = now.getDate();
	if (year<1900) {
		year=year+1900;
	}
	var timestamp = day +" " + month + " " + date +", " + year
	return timestamp;
}

function convertDay(day) {
	if (day==0) {
		return "Sunday";
	}
	if (day==1) {
		return "Monday";
	}
	if (day==2) {
		return "Tuesday";
	}
	if (day==3) {
		return "Wednesday";
	}
	if (day==4) {
		return "Thursday";
	}
	if (day==5) {
		return "Friday";
	}
	if (day==6) {
		return "Saturday";
	}
	return null;
}

function convertMonth(month) {
	if (month==0) {
		return "January";
	}
	if (month==1) {
		return "February";
	}
	if (month==2) {
		return "March";
	}
	if (month==3) {
		return "April";
	}
	if (month==4) {
		return "May";
	}
	if (month==5) {
		return "June";
	}
	if (month==6) {
		return "July";
	}
	if (month==7) {
		return "August";
	}
	if (month==8) {
		return "September";
	}
	if (month==9) {
		return "October";
	}
	if (month==10) {
		return "November";
	}
	if (month==11) {
		return "December";
	}
	return null;
}

//******************String Utility Functions*******************************

function htmlEncode(inputStr, quote_style) {
    inputStr = inputStr.toString();
    
    // Always encode
    inputStr = inputStr.replace(/&/g, '&amp;');
    inputStr = inputStr.replace(/</g, '&lt;');
    inputStr = inputStr.replace(/>/g, '&gt;');
    
    // Encode depending on quote_style
    if (quote_style == 'ENT_QUOTES') {
        inputStr = inputStr.replace(/"/g, '&quot;');
        inputStr = inputStr.replace(/'/g, '&#039;');
    } else if (quote_style != 'ENT_NOQUOTES') {
        // All other cases (ENT_COMPAT, default, but not ENT_NOQUOTES)
        inputStr = inputStr.replace(/"/g, '&quot;');
    }
    
    return inputStr;
}

function htmlDecode(inputStr, quote_style) {

    inputStr = inputStr.toString();
    
    inputStr = inputStr.replace('/&amp;/g', '&');
    inputStr = inputStr.replace('/&lt;/g', '<');
    inputStr = inputStr.replace('/&gt;/g', '>');
    
    // Encode depending on quote_style
    if (quote_style == 'ENT_QUOTES') {
        inputStr = inputStr.replace('/&quot;/g', '"');
        inputStr = inputStr.replace('/&#039;/g', '\'');
    } else if (quote_style != 'ENT_NOQUOTES') {
        // All other cases (ENT_COMPAT, default, but not ENT_NOQUOTES)
        inputStr = inputStr.replace('/&quot;/g', '"');
    }
    
    return inputStr;
}


//******************Text Utility Functions********************************

function replaceText(el, text) {
  if (el != null) {
    clearText(el);
    var newNode = document.createTextNode(text);
    el.appendChild(newNode);
  }
}

function clearText(el) {
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        el.removeChild(childNode);
      }
    }
  }
}

function getText(el) {
  var text = "";
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        if (childNode.nodeValue != null) {
          text = text + childNode.nodeValue;
        }
      }
    }
  }
  return text;
}

if (!String.prototype.trim) {
    String.prototype.trim = function () {
        return this.replace(/^\s+|\s+$/g, "");
    }
}

if (!String.prototype.ltrim) {
    String.prototype.ltrim = function () {
        return this.replace(/^\s+/, "");
    }
}

if (!String.prototype.rtrim) {
    String.prototype.rtrim = function () {
        return this.replace(/\s+$/, "");
    }
}



//******************Array Utility Functions****************************

if (!Array.prototype.contains){
        Array.prototype.contains = function(obj){
    var len = this.length;
    for (var i = 0; i < len; i++){
      if(this[i]===obj){ return true;}
    }
    return false;
  };
}

if(!Array.prototype.containsAny){
      Array.prototype.containsAny = function(obj){
            if(isArray(obj)){
                  for(var i=0;i<obj.length;i++){
                        if(this.contains(obj[i])){ return true;}
                  }
                  return false;
            }else{throw new TypeError();}
      }
}



//******************AJAX Functions********************************

function createRequest() {
  var request = null;
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = null;
      }
    }
  }

  if (request == null) {
    alert("Error creating request object!");
  } else {
    return request;
  }
}









