﻿function GetRootURL()
{
    var urlSplit = document.URL.split("//"); // split at protocol
    urlSplit = (urlSplit[1] ? urlSplit[1] : urlSplit[0]).split("/"); 
    
    var server = urlSplit[0];
    var siteName = urlSplit[1];
    var folder = urlSplit[2];
    var rootURLPath;
        
    switch (server)
    {
        case "sto.idaho.gov":
            rootURLPath = "/";
            break;
        case "localhost":
            rootURLPath = "/" + siteName + "/";
            break;
        case "test":
            rootURLPath = "/" + siteName + "/";
            break;
        case "staging":
            rootURLPath = "/" + siteName + "/";
            break;
        case "integration":
            rootURLPath = "/" + siteName + "/";
            break;
        case "webstagingintranet":
            rootURLPath = "/";
            break;              
    }
    
    return rootURLPath;
}

function GetRootImageURL()
{
    var urlSplit = document.URL.split("//"); // split at protocol
    urlSplit = (urlSplit[1] ? urlSplit[1] : urlSplit[0]).split("/"); 

    var server = urlSplit[0];
    var siteName = urlSplit[1];
    var folder = urlSplit[2];

    var rootURLPath;
    var rootURLImagePath;
            
    switch (server)
    {
        case "intranet.sto.idaho.gov":
            rootURLPath = "/";
            rootURLImagePath = rootURLPath;
            break;
        case "localhost":
            rootURLPath = "/" + siteName + "/";
            rootURLImagePath = "/" + siteName + "/Common/";
            break;
        case "test":
            rootURLPath = "/" + siteName + "/";
            rootURLImagePath = rootURLPath;
            break;
        case "staging":
            rootURLPath = "/" + siteName + "/";
            rootURLImagePath = rootURLPath;
            break;
        case "integration":
            rootURLPath = "/" + siteName + "/";
            rootURLImagePath = rootURLPath;
            break;
        case "webstagingintranet":
            rootURLPath = "/";
            rootURLImagePath = rootURLPath;
            break;              
    }
    
    return rootURLImagePath;
}

function PopupWindow(popUpPageURL, popUpPageName, width, height, popUpWindowFeatures) 
{
	if(screen.width)
	{
		var winl = (screen.width-w)/2;
		var wint = (screen.height-h)/2;
	}
	else
	{
		winl = 0;
		wint = 0;
	}

	if (winl < 0) 
	{
	    winl = 0;
    }
    
	if (wint < 0)
	{
	    wint = 0;
    }
				
	var windowSettings = 'height=' + height + ',';
	    windowSettings += 'width=' + width + ',';
	    windowSettings += 'top=' + wint + ',';
	    windowSettings += 'left=' + winl + ',';
	    windowSettings += popUpWindowFeatures;
	    
	win = window.open(popUpPageURL, popUpPageName, windowSettings);
	
	win.window.focus();
}

function FormatPhoneNumber(textBoxObj)
{
	FormatNumber(textBoxObj, "(###) ###-#### ", "");
}

function FormatNumber(num, format, shortformat)
{
	if(format==null)
	{
		// Choose the default format you prefer for the number. 
		//format = "#-(###) ###-#### ";		// Telephone w/ LD Prefix and Area Code
		format = "(###) ###-#### ";			// Telephone w/ Area Code
		//format = "###-###-####";			// Telephone w/ Area Code (dash seperated)
		//format = "###-##-####";			//Social Security Number
	}			
			
	if(shortformat==null)
	{
		// Choose the short format (without area code) you prefer. 
		//If you do not want multiple formats, leave it as "".

		//var shortformat = "###-#### ";
		var shortformat = "";
	}
	
	var validchars = "0123456789";
	var tempstring = "";
	var returnstring = "";
	var extension = "";
	var tempstringpointer = 0;
	var returnstringpointer = 0;
	count = 0;

	// Get the length so we can go through and remove all non-numeric characters
	var length = num.value.length;
		
	// We are only concerned with the format of the phone number - extensions can be left alone.
	if (length > format.length)
	{
		length = format.length;
	}
	
	// scroll through what the user has typed
	for (var x=0; x<length; x++)
	{
		if (validchars.indexOf(num.value.charAt(x))!=-1)
		{
			tempstring = tempstring + num.value.charAt(x);
		}
	}
	
	// We should now have just the #'s - extract the extension if needed
	if (num.value.length > format.length)
	{
		length = format.length;
		extension = num.value.substr(format.length, (num.value.length-format.length));
	}
	
	// if we have fewer characters than our short format, we'll default to the short version.
	for (x=0; x<shortformat.length;x++)
	{
		if (shortformat.substr(x, 1)=="#")
		{
			count++;
		}
	}
	
	if (tempstring.length <= count)
	{
		format = shortformat;
	}
	
	//Loop through the format string and insert the numbers where we find a # sign
	for (x=0; x<format.length;x++)
	{
		if (tempstringpointer <= tempstring.length)
		{
			if (format.substr(x, 1)=="#")
			{
				returnstring = returnstring + tempstring.substr(tempstringpointer, 1);
				tempstringpointer++;
			}
			else
			{
				returnstring = returnstring + format.substr(x, 1);
			}
		}
	}

	// We have gone through the entire format, let's add the extension back on.
	returnstring = returnstring + extension;
	
	//we're done - let's return our value to the field.
	num.value = returnstring;
}

function CheckAllCheckBoxes(checkBoxObj, checkBoxName)
{
	for (el=0; el<document.forms[0].elements.length; el++) 
	{ 
        var pageObjName = document.forms[0].elements[el].id;
		var indexPosition = pageObjName.indexOf(checkBoxName);
			
		if (indexPosition >= 0)
		{
		    if (checkBoxObj.checked == true)
		        document.forms[0].elements[el].checked = true;
		    else
		        document.forms[0].elements[el].checked = false;
		}
	}
}

function FormatCurrencyTextBox(textBoxObject)
{
    var num = textBoxObject.value;

    if (num != "") {
        num = num.toString().replace(/\$|\,/g, '');

        if (isNaN(num)) {
            num = "0";
        }

        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        cents = num % 100;
        num = Math.floor(num / 100).toString();

        if (cents < 10) {
            cents = "0" + cents;
        }

        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
            num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
        }

        textBoxObject.value = (((sign) ? '' : '-') + '$' + num + '.' + cents);
    }
}

function FormatCurrencyReturnValue(inputValue)
{
    var num = inputValue;

    if (num != "") {
        num = num.toString().replace(/\$|\,/g, '');

        if (isNaN(num)) {
            num = "0";
        }

        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        cents = num % 100;
        num = Math.floor(num / 100).toString();

        if (cents < 10) {
            cents = "0" + cents;
        }

        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
            num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
        }

        return (((sign) ? '' : '-') + '$' + num + '.' + cents);
    }
    else {
        return "";
    }
}

function FormatPercentTextBox(textBoxObject)
{
    var textBoxText = textBoxObject.value;

    if (textBoxText != "") {
        textBoxText = RemovePercentSign(textBoxText);

        if (textBoxText == "") {
            textBoxText = "0";
        }

        var p = textBoxText.indexOf(".");
        if (p < 0) {
            // No decimal present.
            textBoxText += ".0";
            p = textBoxText.indexOf(".");
        }

        if (p == 0) {
            // Decimal at position 0
            textBoxText = "0" + textBoxText;
            p = textBoxText.indexOf(".");
        }

        textBoxText += (p == textBoxText.length - 1) ? "0" : "";

        textBoxObject.value = textBoxText + "%";
    }
}

function RemovePercentSign(inputString)
{
	var p = inputString.indexOf("%");
	
	if(p > -1)
		return inputString.substring(0,p);
	else
		return inputString;
}

function NumericKeyUp(textBoxObj)
{
	var inputStr = textBoxObj.value.toString();
	var strLength = inputStr.length;
	var newStr = "";
	
	for (var i = 0; i < strLength; i++)  
	{
		var oneChar = inputStr.charAt(i);
		
		if (!( isNaN(oneChar) || oneChar == ' ') || oneChar == '.' || oneChar == ',' || oneChar == '-')
		{  
			newStr = String(newStr) + String(textBoxObj.value.substring(i,i+1));
		}
	}
	
	if (newStr != textBoxObj.value)
	{
		textBoxObj.value = newStr;
	}	
}

function CurrencyKeyUp(textBoxObj)
{
	var inputStr = textBoxObj.value.toString();
	var strLength = inputStr.length;
	var newStr = "";
	
	for (var i = 0; i < strLength; i++)  
	{
		var oneChar = inputStr.charAt(i);
		
		if (!( isNaN(oneChar) || oneChar == ' ') || oneChar == '.'  || oneChar == ',' || oneChar == '$' || oneChar == '-')
		{  
			newStr = String(newStr) + String(textBoxObj.value.substring(i,i+1));
		}
	}
	
	if (newStr != textBoxObj.value)
	{
		textBoxObj.value = newStr;
	}	
}

function PhoneNumberKeyUp(textBoxObj)
{
	var inputStr = textBoxObj.value.toString();
	var strLength = inputStr.length;
	var newStr = "";
	
	for (var i = 0; i < strLength; i++)  
	{
		var oneChar = inputStr.charAt(i);
		
		if (!( isNaN(oneChar) || oneChar == ' ') || oneChar == '('  || oneChar == ')' || oneChar == '-')
		{  
			newStr = String(newStr) + String(textBoxObj.value.substring(i,i+1));
		}
	}
	
	if (newStr != textBoxObj.value)
	{
		textBoxObj.value = newStr;
	}	
}

function PercentKeyUp(textBoxObj)
{
	var inputStr = textBoxObj.value.toString();
	var strLength = inputStr.length;
	var newStr = "";
	
	for (var i = 0; i < strLength; i++)  
	{
		var oneChar = inputStr.charAt(i);
		
		if (!( isNaN(oneChar) || oneChar == ' ') || oneChar == '.'  || oneChar == ',' || oneChar == '%' || oneChar == '-')
		{  
			newStr = String(newStr) + String(textBoxObj.value.substring(i,i+1));
		}
	}
	
	if (newStr != textBoxObj.value)
	{
		textBoxObj.value = newStr;
	}	
}