﻿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 "www.alliancetitle.com":
            rootURLPath = "/" + siteName + "/";
            break;
        case "alliancetitle.com":
            rootURLPath = "/" + siteName + "/";
            break;            
        case "localhost":
            rootURLPath = "/" + siteName + "/" + folder + "/";
            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 "www.alliancetitle.com":
            rootURLPath = "/";
            rootURLImagePath = rootURLPath;
            break;
        case "alliancetitle.com":
            rootURLPath = "/";
            rootURLImagePath = rootURLPath;
            break;            
        case "localhost":
            rootURLPath = "/" + siteName + "/";
            rootURLImagePath = "/" + siteName + "/Common/";
            break;
    }
    
    return rootURLImagePath;
}


function IsValidateDate(InputDate) 
{
   var Proceed = 0;
   // Create a variable to hold the correct format.
   var CorrectFormat = /[0-1][0-9]\/[0-3][0-9]\/[0-9][0-9][0-9][0-9]/;
   // If the field has value, validate the date.
   if (InputDate)
   {
      // Test to see if the format of the date is correct.
      if (CorrectFormat.test(InputDate))
      {
         // Split out the month, day and year variables.
         var Month = InputDate.substr(0,2);
         var Day = InputDate.substr(3,2);
         var Year = InputDate.substr(6,4);
         // Ensure all the values are greater than 0
         if (Month > 0 && Day > 0 && Year > 0)
         {
            // Find the max day for the month
            // The default max day is 31
            var maxDays = 31;
            // If the month is April, June, September or November the max day is 30
            if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {
               maxDays = 30;
            }
            if (Month == 2){
               if (Year % 4 > 0)
                  maxDays =28;
               else
               if (Year % 100 == 0 && Year % 400 > 0)
                  maxDays = 28;
               else
                  maxDays = 29;
            }
            // Determine if the day entered is less than the max days for that month.
            if (Day <= maxDays) {
               Proceed = 1;
            }
         }
      }
   }
   if (Proceed == 0) {
      return false;
   } else{
      return true;
   }
} //End IsValidateDate

function CenterNewPopupWindow(mypage,myname,w,h,features) 
{
	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 settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += features;
	win = window.open(mypage,myname,settings);
	win.window.focus()
} //End CenterNewPopupWindow

function isValidNumber(evt)
{
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    {   
        if (charCode != 46)
        {
            return false;
        }
    }

    return true;
}  //End isValidNumber

function FormatPhoneNumber(objTextBox)
{
	FormatNumber(objTextBox, '(###) ###-#### ');
}

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 = "";
	}
	
//---------------------------------------------------------------------------------------------------------------------
//----------------------------------------This code can be used to format any number. ---------------------------------
//----------------------------------------Simply change the format to a number format ---------------------------------
//---------------------------------------- you prefer. It will ignore all characters  ---------------------------------
//----------------------------------------  except the #, where it will replace with  ---------------------------------
//----------------------------------------               user input.                  ---------------------------------
//---------------------------------------------------------------------------------------------------------------------

	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;
}  //End FormatNumber

function FormatCurrencyFromNum(inputNumber)
{
    num = inputNumber.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);
}  //End FormatCurrencyFromNum

function FormatCurrencyFromNum(inputNumber, decimalPlaces)
{
    num = inputNumber.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));
    }
    
    if (decimalPlaces == 0)
    {
        return (((sign)?'':'-') + '$' + num);
    }
    else
    {
        return (((sign)?'':'-') + '$' + num + '.' + cents);        
    }
}  //End FormatCurrencyFromNum

function FormatCurrencyFromNum(inputNumber, decimalPlaces, doRound)
{
    num = inputNumber.toString().replace(/\$|\,/g,'');
    num = num.replace(",", "");
    
    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;
    }

    if (doRound == true)
    {
        cents = parseFloat(cents);
        if (cents > 0.49)
        {
            num = parseInt(num) + 1;
        }
    }
    
    num = num.toString();
       
    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));
    }    
    
    if (decimalPlaces == 0)
    {
        return (((sign)?'':'-') + '$' + num);
    }
    else
    {
        return (((sign)?'':'-') + '$' + num + '.' + cents);        
    }
}  //End FormatCurrencyFromNum

function FormatCurrency(textBoxObject)
{
    var num = textBoxObject.value;
    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);
}  //End FormatCurrency

function SimpleSwap(el,which){
  el.src=el.getAttribute(which || "origsrc");
}

function SimpleSwapSetup(){
  var x = document.getElementsByTagName("img");
  for (var i=0;i<x.length;i++){
    var oversrc = x[i].getAttribute("oversrc");
    if (!oversrc) continue;
      
    x[i].oversrc_img = new Image();
    x[i].oversrc_img.src=oversrc;
    x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
    x[i].onmouseout = new Function("SimpleSwap(this, 'origsrc');");
    x[i].setAttribute("origsrc",x[i].src);
  }
}

var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};
window.onload = function(){PreSimpleSwapOnload(); SimpleSwapSetup();}

function GetDaysInMonth(year, month) 
{
    return 32 - new Date(year, month, 32).getDate();
}

function GetDaysInCurrentMonth() 
{
    var todayDate = new Date();
    
    return 32 - new Date(todayDate.getFullYear(), todayDate.getMonth(), 32).getDate();
}

function ShowProgressPopup(type)
{
    var popup = $find("ctl00_uxUpdateProgressCntl_uxPopupMdl");
    var lbl = eval(document.getElementById("ctl00_uxUpdateProgressCntl_uxMessageLbl"));
    
    switch (type)
    {
        case "Save":
            lbl.innerHTML = "Saving Data";
            break;
        case "Delete":
            lbl.innerHTML = "Deleting Data";
            break;
        case "Retrieve":
            lbl.innerHTML = "Retrieving Data";
            break;
        case "SaveAndRetrieve":
            lbl.innerHTML = "Saving Changes and Retrieving Data";
            break;            
    }
    
    popup.show();
}

function RoundUp(doubleValue, decimals)
{
    var degree = Math.pow(10, decimals);

    return Math.ceil(doubleValue * degree) / degree;
}

function PadLeft(val, ch, num) {
    var re = new RegExp(".{" + num + "}$");
    var pad = "";
    if (!ch) ch = " ";
    do {
        pad += ch;
    } while (pad.length < num);
    return re.exec(pad + val)[0];
}

function PadRight(val, ch, num) {
    var re = new RegExp("^.{" + num + "}");
    var pad = "";
    if (!ch) ch = " ";
    do {
        pad += ch;
    } while (pad.length < num);
    return re.exec(val + pad)[0];
}

function FormatCurrencyTextBox(textBoxObject, decimalPlaces) {
    if (decimalPlaces == '' || decimalPlaces == undefined) {
        decimalPlaces = 2;
    }

    var num = textBoxObject.value;
    num = num.toString().replace(/\$|\,/g, '');

    if (isNaN(num) || num == '') {
        textBoxObject.value = ''
    }
    else {
        sign = (num == (num = Math.abs(num)));

        var cents = ''

        if (decimalPlaces == 0) {
            cents = '';
        }
        else if (decimalPlaces == 1) {
            num = Math.floor(num * 10 + 0.50000000001);
            cents = num % 10;
            num = Math.floor(num / 10).toString();
            cents = PadLeft(cents, '0', 1);
            cents = PadRight(cents, '0', 1);
        }
        else if (decimalPlaces == 2) {
            num = Math.floor(num * 100 + 0.50000000001);
            cents = num % 100;
            num = Math.floor(num / 100).toString();
            cents = PadLeft(cents, '0', 2);
            cents = PadRight(cents, '0', 2);
        }
        else if (decimalPlaces == 3) {
            num = Math.floor(num * 1000 + 0.50000000001);
            cents = num % 1000;
            num = Math.floor(num / 1000).toString();
            cents = PadLeft(cents, '0', 3);
            cents = PadRight(cents, '0', 3);
        }
        else if (decimalPlaces == 4) {
            num = Math.floor(num * 10000 + 0.50000000001);
            cents = num % 10000;
            num = Math.floor(num / 10000).toString();
            cents = PadLeft(cents, '0', 4);
            cents = PadRight(cents, '0', 4);
        }

        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 FormatNumberTextBox(textBoxObject, decimalPlaces) {
    if (decimalPlaces == '' || decimalPlaces == undefined) {
        decimalPlaces = 2;
    }

    var num = textBoxObject.value;
    num = num.toString().replace(/\$|\,/g, '');

    if (isNaN(num) || num == '') {
        textBoxObject.value = ''
    }
    else {

        sign = (num == (num = Math.abs(num)));

        var cents = ''

        if (decimalPlaces == 0) {
            cents = '';
        }
        else if (decimalPlaces == 1) {
            num = Math.floor(num * 10 + 0.50000000001);
            cents = num % 10;
            num = Math.floor(num / 10).toString();
            cents = PadLeft(cents, '0', 1);
            cents = PadRight(cents, '0', 1);
        }
        else if (decimalPlaces == 2) {
            num = Math.floor(num * 100 + 0.50000000001);
            cents = num % 100;
            num = Math.floor(num / 100).toString();
            cents = PadLeft(cents, '0', 2);
            cents = PadRight(cents, '0', 2);
        }
        else if (decimalPlaces == 3) {
            num = Math.floor(num * 1000 + 0.50000000001);
            cents = num % 1000;
            num = Math.floor(num / 1000).toString();
            cents = PadLeft(cents, '0', 3);
            cents = PadRight(cents, '0', 3);
        }
        else if (decimalPlaces == 4) {
            num = Math.floor(num * 10000 + 0.50000000001);
            cents = num % 10000;
            num = Math.floor(num / 10000).toString();
            cents = PadLeft(cents, '0', 4);
            cents = PadRight(cents, '0', 4);
        }

        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;

    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);
}

function FormatPercentTextBox(textBoxObject) {
    var textBoxText = textBoxObject.value;
    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;
    }
}

function HideShowClick(tagId) {
    var e = document.getElementById(tagId);

    if (e.style.display == 'block')
        e.style.display = 'none';
    else
        e.style.display = 'block';
}