

if(document.all && !document.getElementById) 
{
    document.getElementById = function(id) 
    {
         return document.all[id];
    }
}

function getID(evnt)
{
    var id = null;
    if(evnt.target)
    {
        id = evnt.target.id;
    }
    else
    {
        id = evnt.srcElement.id;
    }
    if(!id)
    {   return; }

    return id;
}


function getElementByIdOrName(objID)
{
    var obj = document.getElementById(objID);
    
    if(!obj)
    {
        obj = document.getElementsByName(objID);
        
        if((obj) && (obj.length > 0))
        {
            obj = obj[0];
        }
        else
        {
            obj = null;
        }
    }
    
    return obj;
}
function getDocumentElementByIdOrName(doc,objID)
{
      
    var obj = doc.getElementById(objID);
    
    if(!obj)
    {
              
        obj = doc.getElementsByName(objID);
        if((obj) && (obj.length > 0))
        {
        
            obj = obj[0];
        }
        else
        {
            obj = null;
        }
    }
    return obj;
}
function openWindow(URL,name,wth,hgt,bScrollbars)
{
	bScrollbars = bScrollbars.toLowerCase();
	
	if(bScrollbars == "true")
	{
		window.open(URL,name,"menubar=0,toolbar=0,status=0,width="+wth+",height="+hgt+",left=100,top=50,scrollbars=yes,resizable=no,titlebar=no");
	}
	else
	{
		window.open(URL,name,"menubar=0,toolbar=0,status=0,width="+wth+",height="+hgt+",left=100,top=50,scrollbars=no,resizable=no,titlebar=no");
	}
}

function openMaxWindow(url,name)
{
	var width,height;	
	width = screen.availWidth - 100;
	height = screen.availHeight - 150;
	window.open(url,name,"top=0,left=0,width="+width+",height="+height+",directories=false,location=false,menubar=0,resizable=yes,scrollbars=yes,status=1,toolbar=0");
}

function openMaxWindowMenuBar(url,name)
{
	var width,height;	
	width = screen.availWidth - 100;
	height = screen.availHeight - 150;
	window.open(url,name,"top=0,left=0,width="+width+",height="+height+",directories=false,location=false,menubar=1,resizable=yes,scrollbars=yes,status=1,toolbar=0");
}

function openNormalMaxWindow(url,name)
{
	var width,height;	
	width = screen.availWidth - 100;
	height = screen.availHeight - 200;
	window.open(url,name,"top=0,left=0,width="+width+",height="+height+",directories=false,location=false,menubar=1,resizable=yes,scrollbars=yes,status=1,toolbar=1");
}

function openHelpWindow(url)
{
	var width,height,left;	
	width = 640;
	height = screen.availHeight - 95;
	left = screen.availWidth - width - 10;
	window.open(url,"Help","top=0,left="+left+",width="+width+",height="+height+",directories=false,location=false,menubar=1,resizable=yes,scrollbars=yes,status=1,toolbar=0");
}

function openCustomWindow(url,name,nWidth,nHeight,bFullScreen,bMenu,bResize,bToolbar,bStatus,bScroll)
{
	var width,height;
	if(bFullScreen == true)
	{
		width = screen.availWidth - 10;
		height = screen.availHeight - 70;
	}
	else
	{
		width = nWidth;
		height = nHeight;
	}
	if(bMenu == true)
	{
		bMenu = 1;
	}
	else
	{
		bMenu = 0;
	}
	if(bResize == true)
	{
		bResize = "yes";
	}
	else
	{
		bResize = "no";
	}
	if(bToolbar == true)
	{
		bToolbar = 1;
	}
	else
	{
		bToolbar = 0;
	}
	if(bStatus == true)
	{
		bStatus = 1;
	}
	else
	{
		bStatus = 0;
	}
	if(bScroll == true)
	{
		bScroll = "yes";
	}
	else
	{
		bScroll = "no";
	}
	
	window.open(url,name,"top=0,left=0,width="+width+",height="+height+",directories=false,location=false,menubar="+bMenu+",resizable="+bResize+",scrollbars="+bScroll+",status="+bStatus+",toolbar="+bToolbar);
}

function reloadOpener()
{
	window.opener.location = '' + window.opener.location;
}

function reloadLocation(sLoc)
{    
	sLoc = eval(sLoc);
	if(sLoc)
	{
	    sLoc.location = '' + sLoc.location
	}
	
}

function getFileName(file)
{
	var sFileName = file.substring(file.lastIndexOf("\\") + 1,file.lastIndexOf("."));
 	return(sFileName);
}

function getFileNameURL(file)
{
	var sFileName = file.substring(file.lastIndexOf("/") + 1,file.lastIndexOf("."));
 	return(sFileName);
}

function getFileExt(file)
{
 	var sFileName = file.split(".");
 	len = sFileName.length;
 	return(sFileName[len - 1].toLowerCase());
}

function getURLOnly(sVal)
{
	sVal = sVal.toString();
	var sQueryString = sVal.substring(sVal.lastIndexOf("?") + 1);
 	return(replaceAll(sVal,"?" + sQueryString,""));
}

//Replaces all instances of a particular substring with another one
function replaceAll(str,replace,replaceWith)
{
	sb = new String();
	found = false;
	for (var i = 0; i < str.length; i++)
	{
		if (str.charAt(i) == replace.charAt(0))
		{
			found = true;
			for (var j = 0; j < replace.length; j++)
			{
				if (!(str.charAt(i + j) == replace.charAt(j)))
				{
					found = false;
					break;
				}
			}
			if (found)
			{
				sb += replaceWith;
				i = i + (replace.length);
			}
		}
		sb += str.charAt(i);
	}
	return sb;
}

function trim(field)
{
	var chk 
	var inputStr,leftindex,rightindex,trimed
	inputStr = field.value	
	trimed = ""	
	if (inputStr != "" && inputStr != null) 
	{		
		for (var i =(inputStr.length); i > 0  ;i--)
		{
			var oneChar = inputStr.substring(i,i-1)
			if (oneChar != " ") 
			{
				break;
			}
	   	}		
		rightindex = i		
		if (rightindex == 0) 
		{
			field.value = ""
			return field
		}	
		for (var i =0; i< (inputStr.length) ;i++)
		{
			var oneChar = inputStr.substring(i,i+1)
			if (oneChar != " ") 
			{
				break;
			}
	   	}
	   	leftindex = i	   		
		trimed = inputStr.substring(leftindex, rightindex)		
		field.value = trimed		
		return field
	}						
	return field
}

function trimAll(strValue) 
{
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) 
    {
       strValue = strValue.replace(objRegExp,'');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) 
   {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp,'$2');
    }
  return strValue;
}

function setFocus(fld)
{
	try
	{
	     // AQ - Fire-Fox compatibility
		var fldObj = getElementByIdOrName("fld");//document.getElementById(fld);
		if(fldObj)
		{
		  fldObj.focus();
		}
		
	}
	catch(e)
	{
	}
}

function handleSpecialChars(item)
{
	item = trimAll(item);
	item = replaceAll(item,"null","");
	item = replaceAll(item,"_amp_","&");
	item = replaceAll(item,"_eq_","=");
	item = replaceAll(item,"_pc_","%");
	item = replaceAll(item,"_pl_","+");
	
	item = replaceAll(item,"&amp;","&");
	item = replaceAll(item,"&lt;","<");
	item = replaceAll(item,"&gt;",">");
	item = replaceAll(item,"&apos;","'");
	item = replaceAll(item,"&quot;","\"");
	
	return item;
}

function handleInt(sVal)
{

	if(sVal == "")
	{
		return(-1);
	}
	else
	{
		return(parseInt(sVal));
	}
}

function handleCharsForAttb(item)
{
	item = replaceAll(item,"_sp_"," ");
	item = replaceAll(item,"_hf_","-");
	item = replaceAll(item,"_fst_",".");
	item = replaceAll(item,"_gnr_","/");
	item = replaceAll(item,"_coma_",",");
	document.write(item);
}

function makePageSafe()
{
    if(isLocalVersion())//If running locally dont make the page safe
    {
        return;
    }//If running locally dont make the page safe


	document.oncontextmenu = new Function("return false");
	setStatusBarAndKeys();
}

function hideStatusBarRecursive()
{ 
	hideStatusBar();
	var oTimeout = window.setTimeout("hideStatusBarRecursive();",150);
} 

function setStatusBarAndKeys()
{ 
	
	if(document.layers)
	{
		document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT | Event.MOUSEUP | Event.MOUSEDOWN | Event.KEYUP | Event.KEYDOWN | Event.CLICK)
	}
	document.onmouseover = hideStatusBar;
	document.onmouseout = hideStatusBar;
	document.onmouseup = hideStatusBar;
	document.onmousedown = hideStatusBar;
	document.onkeyup = hideStatusBar;
	document.onkeydown = hideStatusBarAndKeys;
	document.onclick = hideStatusBar;
	
	hideStatusBarRecursive();
}

function hideStatusBarAndKeys()
{
	disableCtrlKeyCombination("");
	window.status = "";
	return true;
}

function hideStatusBar()
{
	window.status = "";
	return true;
}

function disableCtrlKeyCombination(e)
{
    //list all CTRL + key combinations you want to disable
    var forbiddenKeys = new Array("a","n");
    var key;
    var isCtrl;		
	
    if(window.event)
    {    
        //For IE
        key = window.event.keyCode;
        if(window.event.ctrlKey)
        {
			isCtrl = true;
		}
        else
        {
			isCtrl = false;
		}
    }
    else
    {
        //For Firefox
        key = e.which;
        if(e.ctrlKey)
        {
			isCtrl = true;
        }
        else
        {
			isCtrl = false;
		}
    }

    //if CTRL is pressed check if other key is in forbidenKeys array
    if(isCtrl)
    {
                
        for(i = 0; i < forbiddenKeys.length; i++)
        {
            //case-insensitive comparation
            if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
            {
				window.event.cancelBubble = true;
				window.event.returnValue = false;
				window.event.keyCode = false; 
				return false;
            }
        }
    }
    return true;
}


function formatNumber(p,d) 
{
  var r;
  if(p<0){p=-p;r=formatNumber2(p,d);r="-"+r;}
  else   {r=formatNumber2(p,d);}
  return r;
}
function formatNumber2(pnumber,decimals) 
{
  var strNumber = new String(pnumber);
  var arrParts = strNumber.split('.');
  var intWholePart = parseInt(arrParts[0],10);
  var strResult = '';
  if (isNaN(intWholePart))
    intWholePart = '0';
  if(arrParts.length > 1)
  {
    var decDecimalPart = new String(arrParts[1]);
    var i = 0;
    var intZeroCount = 0;
     while ( i < String(arrParts[1]).length )
     {
       if( parseInt(String(arrParts[1]).charAt(i),10) == 0 )
       {
         intZeroCount += 1;
         i += 1;
       }
       else
         break;
    }
    decDecimalPart = parseInt(decDecimalPart,10)/Math.pow(10,parseInt(decDecimalPart.length-decimals-1)); 
    Math.round(decDecimalPart); 
    decDecimalPart = parseInt(decDecimalPart)/10; 
    decDecimalPart = Math.round(decDecimalPart); 

    //If the number was rounded up from 9 to 10, and it was for 1 'decimal' 
    //then we need to add 1 to the 'intWholePart' and set the decDecimalPart to 0. 

    if(decDecimalPart==Math.pow(10, parseInt(decimals)))
    { 
      intWholePart+=1; 
      decDecimalPart="0"; 
    } 
    var stringOfZeros = new String('');
    i=0;
    if( decDecimalPart > 0 )
    {
      while( i < intZeroCount)
      {
        stringOfZeros += '0';
        i += 1;
      }
    }
    decDecimalPart = String(intWholePart) + "." + stringOfZeros + String(decDecimalPart); 
    var dot = decDecimalPart.indexOf('.');
    if(dot == -1)
    {
      decDecimalPart += '.'; 
      dot = decDecimalPart.indexOf('.'); 
    } 
    var l=parseInt(dot)+parseInt(decimals); 
    while(decDecimalPart.length <= l) 
    {
      decDecimalPart += '0'; 
    }
    strResult = decDecimalPart;
  }
  else
  {
    var dot; 
    var decDecimalPart = new String(intWholePart); 

    decDecimalPart += '.'; 
    dot = decDecimalPart.indexOf('.'); 
    var l=parseInt(dot)+parseInt(decimals); 
    while(decDecimalPart.length <= l) 
    {
      decDecimalPart += '0'; 
    }
    strResult = decDecimalPart;
  }
  return strResult;
}

//Checks if popup blocker is installed 
function isPopupBlocker(sPrefix)
{
	var oDate = new Date();
	var winURL = "PopupURL_" + oDate.getDate() + oDate.getTime();
	var winName = "PopupChecker_" + oDate.getDate() + oDate.getTime();
	
	try
	{
		var oWin = window.open(winURL,winName,"width=100,height=50,top=5000,left=5000");
		validateWindow(oWin,sPrefix);
	}
	catch(e)
	{ 
		popupBlockedHandler(sPrefix);
	}
}

function validateWindow(oWin,sPrefix)
{
	try
	{
		if (oWin == null || typeof(oWin) == "undefined") 
		{
			popupBlockedHandler(sPrefix);
		}
		else 
		{
				var oWinName = oWin.name;
				oWin.close();
		}
	} 
	catch(e)
	{ 
		popupBlockedHandler(sPrefix);
	}
}

function popupBlockedHandler(sPrefix)
{
	document.getElementById("hPopupBlockerInstalled").value = "1";	
	document.getElementById("pbWarningSpan").style.display = "";	
}

//Function to check all/none check boxes in a form 
function checkAll(frm,sType,cBox,bVal,sCallback)
{ 
	if(frm == "" || sType == "")
	{	
		return;
	}
	
	frm = eval(frm);
	
	var e;
	for (var count=0; count < frm.elements.length; count++)
	{
		e = frm.elements[count];	
		if (e.type == "checkbox" && e.name.indexOf(sType) != -1)
		{
			if(cBox != "")
			{
				e.checked = cBox.checked;
			}
			else
			{
				e.checked = bVal;
			}
		}	
	}
	if(sCallback != "")
	{
		eval(sCallback);
	}
}

function writeOBJTag(nObjName,sPath,nWidth,nHeight,sCab,sNameParam)
{
	var strOut = "";
	strOut += " <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' ";
	strOut += " codebase='" + sCab + "' ";
	strOut += " width='" + nWidth  + "' height='" + nHeight + "' id='" + nObjName + "' align=''> ";
	strOut += sNameParam;
	strOut += " <param name='quality' value='best'> ";
	strOut += " <param name='menu' value='false'> ";
	strOut += " <param name='bgcolor' value='#FFFFFF'> ";
	strOut += " <embed src='" + sPath + "' quality='best' menu='false' bgcolor='#FFFFFF' ";
	strOut += " width='" + nWidth  + "' height='" + nHeight + "' name='" + nObjName;
	strOut += " type='application/x-shockwave-flash' "
	strOut += " pluginspage='http://www.macromedia.com/go/getflashplayer'></embed> ";
	strOut += " </object> ";
	document.write(strOut);
}

function writeOBJTag_NoContext(nObjName,sPath,nWidth,nHeight,sCab,sNameParam, divID)
{
    var isPopup = false;
    if(window.top.opener)
    {
        isPopup = true;
    }
    
    if(!isPopup)
    {
        return;
    }
    
	var strOut = "";
	strOut += " <object type='application/x-oleobject'  classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' ";
	strOut += " codebase='" + sCab + "' ";
	strOut += " width='" + nWidth  + "' height='" + nHeight + "' id='" + nObjName + "' align='center'> ";
	strOut += sNameParam;
	strOut += " <PARAM NAME='_cx' VALUE='5080'/> "
    strOut += " <PARAM NAME='_cy' VALUE='5080'/> "
    strOut += " <PARAM NAME='FlashVars' VALUE=''/> "
    ///strOut += " <PARAM NAME='Movie' VALUE='"+sPath+"'> "
    ///strOut += " <PARAM NAME='Src' VALUE='"+sPath+"'> "
    strOut += " <PARAM NAME='WMode' VALUE='Opaque'/> "
    strOut += " <PARAM NAME='Play' VALUE='-1'/> "
    strOut += " <PARAM NAME='Loop' VALUE='-1'/> "
    strOut += " <PARAM NAME='Quality' VALUE='High'/> "
    strOut += " <PARAM NAME='SAlign' VALUE='LT'/> "
    ///strOut += " <PARAM NAME='Menu' VALUE='0'> "
    strOut += " <PARAM NAME='Base' VALUE=''/> "
    strOut += " <PARAM NAME='AllowScriptAccess' VALUE='always'/> "
    strOut += " <PARAM NAME='Scale' VALUE='NoScale'/> "
    strOut += " <PARAM NAME='DeviceFont' VALUE='0'/> "
    strOut += " <PARAM NAME='EmbedMovie' VALUE='0'/> "
    ///strOut += " <PARAM NAME='BGColor' VALUE='CCCCCC'> "
    strOut += " <PARAM NAME='SWRemote' VALUE=''/> "
    strOut += " <PARAM NAME='MovieData' VALUE=''/> "
    strOut += " <PARAM NAME='SeamlessTabbing' VALUE='1'/> "
    strOut += " <PARAM NAME='Profile' VALUE='0'/> "
    strOut += " <PARAM NAME='ProfileAddress' VALUE=''/> "
    strOut += " <PARAM NAME='ProfilePort' VALUE='0'/> "
    strOut += " <PARAM NAME='AllowNetworking' VALUE='all'/> "
    strOut += " <PARAM NAME='AllowFullScreen' VALUE='false'/> "
	strOut += " <PARAM name='quality' value='best'/> ";
	strOut += " <PARAM name='menu' value='false'/> ";
	strOut += " <PARAM name='bgcolor' value='#FFFFFF'/> ";
	strOut += " <embed clsid='{D27CDB6E-AE6D-11cf-96B8-444553540000}' type='application/x-oleobject' src='" + sPath + "' quality='best' menu='false' bgcolor='#FFFFFF' ";
	strOut += " width='" + nWidth  + "' height='" + nHeight + "' name='" + nObjName+"' ";
	strOut += " type='application/x-shockwave-flash' "
	strOut += " pluginspage='http://www.macromedia.com/go/getflashplayer'></embed> ";
	strOut += " </object> ";
	
	
	
	var containerDiv = document.getElementById(divID);
	
	if(containerDiv) 
	{
	    containerDiv.innerHTML = strOut;
	}
	else
	{
	    alert("Error locating container");
	}
	
}

function closeNestedWindows()
{
	try
	{
		var win = this.window
		do 
		{
			win = win.parent
		} while (win.parent != win)
		win.close();
	}
	catch(err)
	{
	}
}
//Functions added for URLEncoding and decoding
function urlencode(str) {   
  var re;  
  if(typeof encodeURI == 'function') {    
    str = encodeURI(str);    
  } else {
    str = escape(str);
  }  
  return str;
}
function urldecode(str) {
  var re;
  if(typeof decodeURI == 'function') {
    str = decodeURI(str);    
  } else {
    str = unescape(str);
  }
  return str;
}

function isLocalVersion()
{
    var url = document.location.href;
    var isLocalDomain= false;
    var strLocalDomains = "http://192.168,http://127.0.0.1,http://localhost,http://lion,http://elephant,http://casper,http://azhar,http://omer,http://usman,http://aqib,http://jibran";
    var arLocalDomains = strLocalDomains.split(",");

    for(var counter=0; counter < arLocalDomains.length; counter ++)
    {
        if(url.indexOf(arLocalDomains[counter]) >= 0)
        {
            isLocalDomain= true;
            break;
        }
    }
    
    return isLocalDomain;
}
