////////////////////////////////////////////////////
////////////////////////////////////////////////////

function ChangeTitle(newTitle)
{
	try{
		window.document.title = newTitle;
		parent.document.title = newTitle;
	}catch(ex){}
}

////////////////////////////////////////////////////
////////////////////////////////////////////////////

function OpenNewWindow(loc)
{
	try{
		window.open(loc,"_blank"); 
	}catch(ex){}
}

////////////////////////////////////////////////////
////////////////////////////////////////////////////

function SetLeftFrame(loc)
{
	try{
		parent.document.all.FrameLeft.src = loc; 
	}catch(ex){}
}

////////////////////////////////////////////////////
////////////////////////////////////////////////////

function SetRightFrame(loc)
{
	try{
		parent.document.all.FrameRight.src = loc; 
	}catch(ex){}
}

////////////////////////////////////////////////////
////////////////////////////////////////////////////

function RefreshLeftFrame(SubmitReason)
{
	try{	
		if(SubmitReason == null || SubmitReason == '')
			SubmitReason = '1';					
		theFrm = parent.parent.frames.item(1);		
		theFrm.document.forms[0].PageBasePostName.value = SubmitReason;
		theFrm.document.forms[0].submit();		
	}catch(ex){}
}

function RefreshLeftFramePesa(SubmitReason)
{   
	try{	
		if(SubmitReason == null || SubmitReason == '')
			SubmitReason = '1';				
		theFrm = parent.parent.frames.item(2);		
		theFrm.document.forms[0].PageBasePostName.value = SubmitReason;
		theFrm.document.forms[0].submit();		
	}catch(ex){}
}

////////////////////////////////////////////////////
////////////////////////////////////////////////////

function RefreshRightFrame(SubmitReason)
{
	try{
		if(SubmitReason == null || SubmitReason == '')
			SubmitReason = '1';
			
		theFrm = parent.parent.frames.item(2);
		theFrm.document.forms[0].PageBasePostName.value = SubmitReason;
		theFrm.document.forms[0].submit(); 
	}catch(ex){}
}

////////////////////////////////////////////////////
////////////////////////////////////////////////////

function GetRightFrameLocation()
{
	try{
		theFrm = parent.parent.frames.item(2);
		return theFrm.document.location + '';
	}catch(ex){}
}

////////////////////////////////////////////////////
////////////////////////////////////////////////////

function GetRightFrameDoc()
{
	try{
		theFrm = parent.parent.frames.item(2);
		return theFrm.document;
	}catch(ex){}
}

////////////////////////////////////////////////////
////////////////////////////////////////////////////

function Phone(txtBox)
{
	try
	{
		if(txtBox.value != '')
		{
			var org = txtBox.value;
			var ex = '';
			if(org.indexOf(' ex') != -1)
			{
				ex = org.substring(org.indexOf(' ex'),org.length);
				org = org.substring(0,org.indexOf(' ex'));
			}
			else if(org.indexOf(' EX') != -1)
			{
				ex = org.substring(org.indexOf(' EX'),org.length);
				org = org.substring(0,org.indexOf(' EX'));
			}
			else if(org.indexOf(' x') != -1)
			{
				ex = org.substring(org.indexOf(' x'),org.length);
				org = org.substring(0,org.indexOf(' x'));
			}
			else if(org.indexOf(' X') != -1)
			{
				ex = org.substring(org.indexOf(' X'),org.length); 
				org = org.substring(0,org.indexOf(' X'));
			}
			else if(org.indexOf(' e') != -1)
			{
				ex = org.substring(org.indexOf(' e'),org.length);
				org = org.substring(0,org.indexOf(' e'));
			}
			else if(org.indexOf(' E') != -1)
			{
				ex = org.substring(org.indexOf(' E'),org.length);
				org = org.substring(0,org.indexOf(' E'));
			}
			
			if(ex != '')
			{
				txtBox.value = ex;
				IntegerValue(txtBox);
				ex = txtBox.value;
			}
			
			if(ex != '')
			{
				txtBox.value = org;
				txtBox.value = txtBox.value + ' x' + ex;
				var fmt = txtBox.value;
				var fmtlen = fmt.length;
				if(fmtlen > 10)
				{
					var eex = fmt.substring(0,fmt.indexOf(' x'));
					var nnumber = phoneformat(eex);
					txtBox.value = nnumber + ' x' + ex;
				}
				else if(fmtlen <= 10)
				{
					var eex = fmt.substring(0,fmt.indexOf(' x'));
					var nnumber = phoneformat(eex);
					txtBox.value = nnumber;
				}
			}
			else
			{
				var fmt = txtBox.value;
				var fmtlen = fmt.length;
				//if(fmtlen <= 10)
				//{
					var nnumber = phoneformat(txtBox.value);
					txtBox.value = nnumber;
				//}	
			}
			
			
		}
	}
	catch(ex)
	{
	}
}
function phoneformat(phonenumber) // this function replaced "phoneformat_old(phonenumber)"
{
try
	{
		// Strip phone number of any non numeric characters	
		phonenumber = stripCharsNotInBag(phonenumber)
		var inumber = phonenumber.length;
		var right;
		var left;
		var mid;
		var newphoneformat;
		switch(inumber)
		{
			case 7:  /*---- Format : ####### ---- Insert dashes - ###-####*/
                    right = phonenumber.substr(3);
                    left = phonenumber.substr(0,3);
                    newphoneformat = left + "-" + right;
                    break
			
			
            case 10: /*---- 'Format : ########## ---- Insert brackets and dashes - (###) ###-####*/
					//Brazil format
					/*
                    right = phonenumber.substr(6);
                    left = phonenumber.substr(0, 2);
                    mid = phonenumber.substr(2, 4);
                    newphoneformat = "" + left + "-" + mid + "-" + right;
                    */
                    
					//North America format
                    right = phonenumber.substr(6);
                    left = phonenumber.substr(0, 3);
                    mid = phonenumber.substr(3, 3);
                    newphoneformat = "(" + left + ") " + mid + "-" + right;
                    break

             default:
					newphoneformat = phonenumber;
		}
		return newphoneformat;	
	}
	catch(ex)
	{

	}
}

function stripCharsNotInBag(s)
{   var i;
    var returnString = "";
    var digits = "0123456789";	// Declaring required variables
	

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (digits.indexOf(c) != -1) returnString += c;

    }
    return returnString;
}
function None(txtBox)
{
	try
	{
		if(txtBox.value != '')
		{
			txtBox.value = Trim(txtBox.value);
		}
	}
	catch(ex)
	{
	}
}

function Email(txtBox)
{
	try
	{
		if(txtBox.value != '')
		{
			txtBox.value = Trim(txtBox.value.toLowerCase());
		}
	}
	catch(ex)
	{
	}
}

function Alphabetic(txtBox){
	
	try
	{
		if(txtBox.value != '')
		{
			txtBox.value = Trim(txtBox.value.toLowerCase());
			txtBox.value = Trim(txtBox.value.charAt(0).toUpperCase());
		}
	}
	catch(ex)
	{
	}
}

function AlphaNumeric(txtBox)
{   	
	
	try
	{
		if(txtBox.value != "" && txtBox.type != "password")
		{
			var INPUT = txtBox.value;
			var strReturn_Value = '';
			var iTemp = INPUT.length;
			if(iTemp==0){
			return"";
		}

			var stri = " i ";
			var strI = " I ";

			while(INPUT.indexOf(stri) > -1){
			INPUT = INPUT.replace(stri,strI);
			}

			while(INPUT.indexOf("  ") > -1){
			INPUT = INPUT.replace("  "," ");
			}

			var UcaseNext = false;
			strReturn_Value += INPUT.charAt(0).toUpperCase();

			for(var iCounter=1;iCounter < iTemp;iCounter++){
			if(UcaseNext == true){
			strReturn_Value += INPUT.charAt(iCounter).toUpperCase();
			}
			else{
			strReturn_Value += INPUT.charAt(iCounter);
			}
			var iChar = INPUT.charCodeAt(iCounter);
			if(iChar == 46 || iChar == 33 || iChar == 63 || iChar == 32){
				
				UcaseNext = true;

			}
			else if(iChar==13 && INPUT.charCodeAt(iCounter+1)==10){
				iCounter+=1;
				//Comment out the next line if you want to stop capitalization on a new line
				UcaseNext = true;
			}
			else if(iChar==44 && INPUT.charCodeAt(iCounter+1)!=32){
			strReturn_Value += (INPUT.substring(iCounter,iCounter)) + " ";
			}
			else{
			UcaseNext = false
			}
			if(iChar == 99 || iChar == 67){
			if(INPUT.charCodeAt(iCounter-1)==77 || INPUT.charCodeAt(iCounter-1)==109){
			UcaseNext = true;
			}
			}


			} 

			txtBox.value = Trim(strReturn_Value);
			
 		
			//txtBox.value = Trim(txtBox.value);
		}
	}
	catch(ex)
	{
	}
}
 
function IntegerValue(txtBox)
{
	try
	{
		if(txtBox.value != '')
		{
			txtBox.value = Trim(txtBox.value);
			strX = txtBox.value;	
			txtBox.value = '';
			var x = 0;
			while(x < strX.length)
			{
				var str = strX.substring(x,x+1) + '';
				if(IsNumericStr(str,true))
					txtBox.value += str; 
				x++;
			}
		}
	}
	catch(ex)
	{
	}
}

function DecimalValue(txtBox)
{
	try
	{
		if(txtBox.value != '')
		{
			var nodotyet = true;
			txtBox.value = Trim(txtBox.value);
			strX = txtBox.value;	
			txtBox.value = '';
			
			var x = 0;
			while(x < strX.length)
			{
				var str = strX.substring(x,x+1) + '';
				if( IsNumericStr(str,true) || (str == '.' && nodotyet) )
				{
					if(str == '.')
						nodotyet = false;
					txtBox.value += str; 
				}
				x++;
			}
			
			if( txtBox.value.substring(txtBox.value.length-1,txtBox.value.length) == '.' )
				txtBox.value = txtBox.value.substring(0,txtBox.value.length-1);
		}
	}
	catch(ex)
	{
	}
}

function Time(txtBox)
{
	try
	{
		if(txtBox.value != '')
		{
			txtBox.value = Trim(txtBox.value);
		}
	}
	catch(ex)
	{
	}
}

function Currency(txtBox)
{
	try
	{
		if(txtBox.value != '')
		{
			DecimalValue(txtBox);
			txtBox.value = '$' + txtBox.value;
			
			var OrigString = txtBox.value;
			var s = OrigString.indexOf(".");
			if(s == -1)
				OrigString = OrigString + ".00";
			else if(OrigString.length - s == 1)
				OrigString = OrigString + "00";
			else if(OrigString.length - s == 2)
				OrigString = OrigString + "0";
			else if(OrigString.length - s > 2)
				OrigString = OrigString.substring(0, s + 3);
			OrigString = OrigString;
			txtBox.value = OrigString;
		}
	}
	catch(ex)
	{
	}
}

function Percent(txtBox)
{
	try
	{
		if(txtBox.value != '')
		{
			DecimalValue(txtBox);
			txtBox.value = txtBox.value;
			
			var OrigString = txtBox.value;
			var s = OrigString.indexOf(".");
			if(s == -1)
				OrigString = OrigString + ".00";
			else if(OrigString.length - s == 1)
				OrigString = OrigString + "00";
			else if(OrigString.length - s == 2)
				OrigString = OrigString + "0";
			else if(OrigString.length - s > 2)
				OrigString = OrigString.substring(0, s + 3);
			OrigString = OrigString;
			txtBox.value = OrigString + '%';
		}
	}
	catch(ex)
	{
	}
}

function IsNumericStr(strX, sing)
{
	try
	{
		var str = eval(strX) / 1;
		var s = '0 1 2 3 4 5 6 7 8 9';
		if(strX + '' == '' || strX + '' == ' ' || (sing && s.indexOf(strX + '') == -1))
			return false;
		return true;
	}
	catch(ex)
	{
		return false;
	}
}

function Trim(strX)
{
	try
	{
		var from = 0;
		var to = strX.length;
		var x = 0;
		while(x < strX.length)
		{
			var str = strX.substring(x,x+1) + '';
			if(str != "" && str != '' && str != ' ' && str != 'undefined' && str != null)
			{
				from = x;
				break;
			}	
			x++;
		}

		x = strX.length-1;
		while(x > -1)
		{
			var str = strX.substring(x,x+1) + '';
			if(str != "" && str != '' && str != ' ' && str != 'undefined' && str != null)
			{
				to = x+1;
				break; 
			}
			x--;
		}
		strX = strX.substring(from,to);
		return strX;
	}
	catch(ex)
	{
		return strX;
	}
}


///////////////////
		function PostalCodeFormat(TXTPOSTALCODE, Caller)
			{
				try{
					/////////////////////////////
					var TXTPOSTALCODEObj = document.getElementById(TXTPOSTALCODE);
					/////////////////////////////
					
					/////////////////////////////
					var txtpostalcode = TXTPOSTALCODEObj.value;
					var regex= /^[A-Z]\d[A-Z]\d[A-Z]\d$/;
					var regexx = /^[A-Z]\d[A-Z]-\d[A-Z]\d$/;
					var re = new RegExp(regex);
					var ree = new RegExp(regexx);

					if (TXTPOSTALCODEObj.value.toUpperCase().match(re) || TXTPOSTALCODEObj.value.toUpperCase().match(ree))
					{
						if(txtpostalcode.length == 6)
						{
							var fristthree = txtpostalcode.substring(0,3);
							var restoff = txtpostalcode.substring(txtpostalcode.length - 3,txtpostalcode.length);
							var newval = fristthree + " " + restoff;
							TXTPOSTALCODEObj.value = newval.toUpperCase()
						}else if(txtpostalcode.length == 7)
							{
								out = "-"; // replace this
								add = " "; // with this
								temp = txtpostalcode; // temporary holder

								while (temp.indexOf(out)>-1)
								{
								pos= temp.indexOf(out);
								temp = "" + (temp.substring(0, pos) + add + 
								temp.substring((pos + out.length), temp.length));
								}
								TXTPOSTALCODEObj.value = temp.toUpperCase();
							}
							
					}else
					{
					TXTPOSTALCODEObj.value = TXTPOSTALCODEObj.value.toUpperCase();
					}

					/////////////////////////////
				}catch(exx){}
			}
