﻿var Constants = new Object();
Constants.OneMB = 1000000;

function GetPageElement(ElementName)
{
    var ReturnElement = "";

    if (document.getElementById)
    {
        ReturnElement = "document.getElementById('" + ElementName + "')";
    }
    else if (document.all)
    {
        ReturnElement = "document.all['" + ElementName + "']";
    }
    return eval(ReturnElement);
}

function SplitString(JoinedList,DelimA,DelimB)
{
	var args = JoinedList.split(DelimA);
	for (var i=0; i<args.length; i++)
	{
		var pair = args[i].split(DelimB);

		temp = unescape(pair[0]).split('+');
		name = temp.join(' ');

		temp = unescape(pair[1]).split('+');
		value = temp.join(' ');

		this[name]=value;
	}
	return this;
}

function IsINT(sText)
{
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;

    var checkText = RemoveCommas(sText);

    if (checkText.length == 0)
        return false;
         
    for (i = 0; i < checkText.length && IsNumber == true; i++) 
    { 
        Char = checkText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function FormatIntControl(val)
{
	var ctlVal;
	var iPeriodPos;
	ctlVal = val;

	if (isNaN(ctlVal)) 
	{
		return ctlVal;
	} else {
		iPeriodPos = ctlVal.indexOf(".");
		if (iPeriodPos>0) 
		{
            ctlVal = ctlVal.substring(0,iPeriodPos);    
		}
		return ctlVal;
	}
}

function IsNumeric(sText)
{
    var ValidChars = "0123456789.-";
    var IsNumber=true;
    var Char;

    var checkText = RemoveCommas(sText);

    if (sText.length == 0)
        return false;
         
    for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function RemoveCommas(sText)
{
    var newText = sText + "";
    return newText.replace(",","");
}

function trim(field)
{
    while (field.charAt(field.length-1) == " "){field = field.substring(0,field.length-1);} 
	while (field.substring(0,1) ==" "){field = field.substring(1,field.length);}
	return field;
}

function Hide(id) {
    var node = GetPageElement(id);
    if (node != null) {
        node.style.display = "none";
    }
}

function Show(id) {
    var node = GetPageElement(id);
    if (node != null) {
        node.style.display = "";
    }
}

function Toggle(id) {
    var node = GetPageElement(id);
    if (node != null) {
        node.style.display = node.style.display == "none" ? "" : "none";
    }
}

function QuickMessageBoard(URL) {
	MessageWin = this.open(URL, "messageWindow", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=700,height=600");
}

function setstatus(str) {
    window.status = str;
    return true;
}

function UTCDate()
{
    var herenow = new Date();
    var utcnow = new Date();

    utcnow.setDate(herenow.getUTCDate());
    utcnow.setMonth(herenow.getUTCMonth());
    utcnow.setFullYear(herenow.getUTCFullYear());
    utcnow.setHours(herenow.getUTCHours());
    utcnow.setMinutes(herenow.getUTCMinutes());
    utcnow.setSeconds(herenow.getUTCSeconds());
    utcnow.setMilliseconds(herenow.getUTCMilliseconds());

    return utcnow;
}

function UTCDateWithOffset(offsetMinutes)
{
    var now = UTCDate();
    now.setMinutes(now.getMinutes() + offsetMinutes);
    return now;
}

function SetDropDownSelection(dropDown, newValue)
{
    for(var i=0; i< dropDown.options.length; i++)
    {
        if(dropDown.options[i].value == newValue)
        {
            dropDown.selectedIndex = i;
            break;
         }
    }
}

function SwitchMenu(obj){
    var el = GetPageElement(obj);
    if (el != null)
    {
        el.className = 'navblueon';
    }
}

function SwitchMenu2(obj){
    var el = GetPageElement(obj);
    if (el != null)
    {
        el.className = 'navblue';
    }
}

function showSubNav(obj){
    var subnav;
    var thisobj = "GetPageElement('" + obj + "Menu')";
    subnav = eval(thisobj);
    if (subnav != null){
        subnav.style.visibility = 'visible';	
    }
}

function hideSubNav(obj){
    var subnav;
    var thisobj = "GetPageElement('" + obj + "Menu')";
    subnav = eval(thisobj);
    if (subnav != null){
        subnav.style.visibility = 'hidden';	
    }
}

function ScrollToTop()
{
    if (window.pageYOffset)
        window.pageYOffset = 0;
    else if (document.body.scrollTop)
        document.body.scrollTop = 0;
}

function IgnoreEnter(e)
{
    var characterCode;

    if(e && e.which)
    {
        characterCode = e.which;
    }
    else
    {
        e = event;
        characterCode = e.keyCode;
    }

    if(characterCode == 13)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function StringCleaner(oldString)
{
    while (oldString.indexOf("%20") > -1)
    {
        oldString = oldString.replace("%20"," ");
    }

    while (oldString.indexOf("%22") > -1)
    {
        oldString = oldString.replace("%22","\"");
    }
                            
    return oldString;
}

function Open_New(str)
{
    OpenWinNew = this.open(str);
}

function swapImage(ctrl,img)
{
    ctrl.src="../../images/SkillTesting/"+img;     
}

// This function validates the file extensions

function IsValidFileExtension(file, extListType) {

        var extAt = file.lastIndexOf(".");
        if (extAt == -1) {
          return false;
        }
        ext = file.substring(extAt,file.length).toLowerCase();
          
        switch (extListType){   
            case "generic":
                        extArray = new Array(".accdb", ".avi", ".bmp", ".doc", ".docx", ".eps", ".gif", ".htm", ".html", ".jpeg", ".jpg", ".mdb", ".mde", ".mov", ".mp3", ".mpe", ".mpeg", ".mpg", ".pdf", ".png", ".pps", ".ppsx", ".ppt", ".pptx", ".psd", ".qxd", ".swf", ".tif", ".tiff", ".txt", ".wav", ".wmv", ".xls", ".xlsx", ".zip");
                        extArray.sort();
                        break;
            case "image":  
                        extArray = new Array(".gif",".jpeg",".jpg",".bmp");
                        extArray.sort();
                        break;
        }     
      //check for valid extension
      for (var i = 0; i < extArray.length; i++) {
          if (extArray[i] == ext) {
              return true;
          }
      }
      alert("Please only upload files that end in types:  " 
                  + (extArray.join("  ")) + "\nPlease select a new "
                  + "file to upload and submit again.");
      return false;
}

  function is_space(filepath){
    var invalid = " \\"; // Invalid character is a space
    var invalid2= "\\ ";
    if (filepath.indexOf(invalid) > -1 || filepath.indexOf(invalid2) > -1){
      alert("DO NOT include spaces in your file's name");
      return false;
    }
    return true;
  }
