/** SCRIPTS FOR PROJECT PINNACLE **/


function doConfirm(controlIn){
    if (confirm("Are you sure you wish to delete this entry?")){
        eval(controlIn);
        }
    }
    
function doConvert(controlIn){
    if (confirm("Are you sure you wish to convert this customer?")){
        eval(controlIn);
        }
    }
    
    
function PopulateIPBox()
{
	var pos = document.forms[0].MailIPDropList.selectedIndex;
	document.forms[0].MailIPBox.value = document.forms[0].MailIPDropList.options[pos].value;
}

function PopulateCrossBox()
{
	var pos = document.forms[0].CrossDropList.selectedIndex;
	document.forms[0].CrossBox.value = document.forms[0].CrossDropList.options[pos].value;
}

function PopulateInternetBox()
{
	var pos = document.forms[0].InternetDropList.selectedIndex;
	document.forms[0].InternetBox.value = document.forms[0].InternetDropList.options[pos].value;
}

function PopulateWhereFound()
{
	var pos = document.forms[0].WhereDropList.selectedIndex;
	document.forms[0].WhereFound.value = document.forms[0].WhereDropList.options[pos].value;
}

function ZipButtonClick()
{

	if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
	{
		event.cancelBubble = true;
		event.returnValue = false;
			
		document.forms[0].ZipButton.click();
				
	}
	else
	{
		return true;
	}
		
}


function UpdateAliasClick()
{

	if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
	{
		event.cancelBubble = true;
		event.returnValue = false;
			
		document.forms[0].UpdateAliasButton.click();
				
	}
	else
	{
		return true;
	}
		
}


function UpdateEmailButton()
{

	if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
	{
		event.cancelBubble = true;
		event.returnValue = false;
			
		document.forms[0].emailsgrid_UpdateButton.click();
				
	}
	else
	{
		return true;
	}
		
}


function UpdateForwardClick()
{
	if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
	{
		event.cancelBubble = true;
		event.returnValue = false;
			
		document.forms[0].forwardssgrid_AddForwardButton.click();
				
	}
	else
	{
		return true;
	}
		
}

function UpdateAliasForwardClick()
{
	if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
	{
		event.cancelBubble = true;
		event.returnValue = false;
			
		document.forms[0].aliasforwards_AddButton.click();
				
	}
	else
	{
		return true;
	}
		
}



function UpdateCustomerClick()
{

	if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
	{
		event.cancelBubble = true;
		event.returnValue = false;
			
		document.forms[0].UpdateCustomerButton.click();
				
	}
	else
	{
		return true;
	}
		
}


function UpdateContactClick()
{

	if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
	{
		event.cancelBubble = true;
		event.returnValue = false;
			
		document.forms[0].UpdateContactButton.click();
				
	}
	else
	{
		return true;
	}
		
}


function UpdateDomainClick()
{

	if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
	{
		event.cancelBubble = true;
		event.returnValue = false;
			
		document.forms[0].UpdateDomainButton.click();
				
	}
	else
	{
		return true;
	}
		
}


function UpdateHostingClick()
{

	if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
	{
		event.cancelBubble = true;
		event.returnValue = false;
			
		document.forms[0].UpdateHostingButton.click();
				
	}
	else
	{
		return true;
	}
		
}






function SearchButtonClick()
{

	if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
	{
		event.cancelBubble = true;
		event.returnValue = false;
			
		document.forms[0].SearchButton.click();
				
	}
	else
	{
		return true;
	}
		
}


function AddButtonClick()
		{
			if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) 
			{
				event.cancelBubble = true;
				event.returnValue = false;
				
				document.forms[0].AddButton.click();
				
			}
			else
			{
				return true;
			}
		
		}

function PopulateLimit()
{
			
	if ( isNumeric(document.forms[0].AllowedBox.value) && isNumeric(document.forms[0].TotalBox.value) )
	{
		var ans = document.forms[0].TotalBox.value.toInteger() - document.forms[0].AllowedBox.value.toInteger();		
		document.forms[0].LimitLabel.value  = ans.toString();
	}
	
	return true;

}


function confirmDelete()
{
	if ( confirm('Are you sure you want to delete this entry?') )
	{
		return true;
	}

	return false;
	
}

function formatPhone(num)
		{ 
			var _return=false;
			/*
				* 7181238748 to 1(718)123-8748
			*/ 
			
			num= replaceSubstring(num, ".", "");
			num= replaceSubstring(num, "(", "");
			num= replaceSubstring(num, ")", "");
			num= replaceSubstring(num, "-", "");
			num= replaceSubstring(num, " ", "");
			
			if(num.length != 10)
			{ 
				/* 
				* if user did not enter 10 digit phone number then simply print whatever user entered 
				*/ 
				_return=num;
			} 
			else
			{ 
				/* formating phone number here */ 
				_return="(";
				var ini = num.substring(0,3);
				_return+=ini+") ";
				var st = num.substring(3,6);
				_return+=st+"-";
				var end = num.substring(6,10);
				_return+=end;
			}
			return _return; 
		} 
	
	// REPLACE A SUBSTRING WITHIN A STRING
	function replaceSubstring(inputString, fromString, toString) 
	{
		// Goes through the inputString and replaces every occurrence of fromString with toString
		var temp = inputString;
		if (fromString == "") 
		{
			return inputString;
		}
		if (toString.indexOf(fromString) == -1) 
		{ // If the string being replaced is not a part of the replacement string (normal situation)
			while (temp.indexOf(fromString) != -1) 
			{
				var toTheLeft = temp.substring(0, temp.indexOf(fromString));
				var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
				temp = toTheLeft + toString + toTheRight;
			}
		} 
		else 
		{ // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
			var midStrings = new Array("~", "`", "_", "^", "#");
			var midStringLen = 1;
			var midString = "";
			// Find a string that doesn't exist in the inputString to be used
			// as an "inbetween" string
			while (midString == "") 
			{
				for (var i=0; i < midStrings.length; i++) 
				{
					var tempMidString = "";
					for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
					if (fromString.indexOf(tempMidString) == -1) 
					{
						midString = tempMidString;
						i = midStrings.length + 1;
					}
				}
			} // Keep on going until we build an "inbetween" string that doesn't exist
			// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
			while (temp.indexOf(fromString) != -1) 
			{
				var toTheLeft = temp.substring(0, temp.indexOf(fromString));
				var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
				temp = toTheLeft + midString + toTheRight;
			}
			// Next, replace the "inbetween" string with the "toString"
			while (temp.indexOf(midString) != -1) 
			{
				var toTheLeft = temp.substring(0, temp.indexOf(midString));
				var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
				temp = toTheLeft + toString + toTheRight;
			}
		} // Ends the check to see if the string being replaced is part of the replacement string or not
		return temp; // Send the updated string back to the user
	
	} // Ends the "replaceSubstring" function
