/////////////////////////////////////////////////////////////////////////////////
// BSmart Web Applications (c) all rights reserved.
// BSmartSite.js - Web Site-related functions. 
/////////////////////////////////////////////////////////////////////////////////

function bsCheckKeyPress(e){if(event.keyCode==13)return true;return false;}
function bsSetFormIndication(formID){document.getElementById("F10028").value=formID}
function bsValidationUponSubmit(JSFunction)
{
    if(JSFunction!=""){if(!eval(JSFunction+"()"))return false;}
    return true;
}
function bsPostForm(btnID,JSFunction){if (bsValidationUponSubmit(JSFunction)) document.getElementById(btnID).click()}
function bsPostFormKeyPress(btnID,JSFunction)
{    
    if (bsValidationUponSubmit(JSFunction)){document.getElementById(btnID).click();}
    // Return false to stop default Enter-keyPress behaviour (which is submit the form)
    return false;
}
// Open BSmart Data Managment Window.
function openBSmartWindow(containerID, itemID, componentID, dateID){panelDataManagement(containerID, itemID, componentID, dateID)}
// bsGo2Page - used by BSmart paging mechanism
function bsGo2Page(url){document.location.href = url}
// Go2Page - used by BSmart paging mechanism
// Should not be used (backwards compatibility only), use bsGo2Page instead
function Go2Page(PageNum)
{document.location.href = bsUpdateURLParamValue(document.location,"bscrp",PageNum)}
// bsGroupSearch - 
function bsGroupSearch(url){document.location.href=url}
// SearchGroup - 
// Should not be used (backwards compatibility only), use bsGroupSearch instead
function SearchGroup(SearchID)
{
    var DocHref=bsUpdateURLParamValue(document.location,"bssearch",SearchID)
    document.location.href=bsRemoveParamValueFromURL(DocHref,"bscrp")	
}

function bsUpdateURLParamValue(locationObj,paramName,paramValue)
{
    var DocHref = locationObj.search.toLowerCase();
	DocHrefArr = DocHref.split("&");
	DocHref = "";
	var found = 0;
	var StrTemp = "";
	for(i=0;i<DocHrefArr.length;i++)
	{
		if(String(DocHrefArr[i]).indexOf(paramName) != -1)
		{
			found=1;
			StrTemp = String(DocHrefArr[i]).split("=");
			StrTemp[1]=paramValue;
			DocHrefArr[i]=StrTemp[0]+"="+StrTemp[1];
		}
		if(i==0) DocHref += DocHrefArr[i];
		else DocHref += "&"+DocHrefArr[i];
	}
	if(!found)
	{
		if (locationObj.href.indexOf("?") == -1) StrTemp = "?";
		else StrTemp = "&";
		DocHref = locationObj.href+StrTemp+paramName+"="+paramValue;
	}
	return DocHref
}

function bsRemoveParamValueFromURL(url,paramName)
{
    var DocHref = url;
	DocHrefArr = DocHref.split("&");
	DocHref="";	
	var first=0;	
	for(i=0;i<DocHrefArr.length;i++)
	{	
		// Add the parameter but only if it is not bscrp (bscrp
		// represents the paging pageID, thus should be removed
		// in order to retrieve the first page of the new 
		// selected group.
		if(String(DocHrefArr[i]).indexOf(paramName)==-1)
		{
		    if(first==0){DocHref += DocHrefArr[i];first=1}
		    else DocHref += "&"+DocHrefArr[i];
		}
	}
	return DocHref
}

// Opens Date-Picker Window
function openDatePicker(fid, defaultDate, basePath)
{   
    var selectedDate
    if (fid!="") selectedDate=document.getElementById(fid).value
    if (selectedDate=="") selectedDate=defaultDate
	var win=window.open(basePath+'/BSmartFreeHtmls/datepicker.aspx?fid='+fid+'&dateid='+selectedDate,'datepick','toolbar=0,statusbar=0,addressbar=0,resizable=0,scrollbars=0,'+getPopupFrame(300,300)); 
	win.focus();				
}
// The function is used to set Pop-Up size (Date-Picker opened by Date fields)
function getPopupFrame(width,height){return "width="+width+",height="+height+",top="+(window.screen.height/2-height/2)+",left="+(window.screen.width/2-width/2)}
function popWin(url){window.open(url);}
function BSmartInfoWindow(infoID, bsLocation)
{	
	var newWin	
	newWin = window.open("../" + bsLocation + "BSmartFreeHTMLS/BSmartInfoWindow.aspx?infoid=" + infoID,"infoWin","toolbar=0,statusbar=0,addressbar=0,width=300,height=200,top=" + (screen.height/2 - 100)  + ",left=" + (screen.width/2 - 150) + ",resizable=0");    
	//Removed (Netscape):
    //newWin=window.showModalDialog("../" + bsLocation + "BSmartHTMLS/BSmartInfoWindow.aspx?infoid=" + infoID,"infoWin","status:no,scroll:no,dialogWidth:300px,dialogHeight:200px");
    newWin.focus();
    // Start build it as an IFrame
    // document.write('<iframe name="BSmartInfoWindow" SRC="http://www.java2s.com" width="300" height="200" style="position:absolute;top:100px;left:100px;">')
}
// Get URL parameter's value
function getUrlParam(name){
	var pname=name + '=';
	var ps=window.location.search;
	if(ps.length > 0)
	{
		var start=ps.indexOf(pname);
		if(start!=-1)
		{
			start+=pname.length;
			var end=ps.indexOf('&',start);
			if(end==-1) end=ps.length;
			return unescape(ps.substring(start,end));
		}
	}
	return null;
}
// Refreshes Parent Windows
function updateLoginWindows(){	
	// Close all managment windows, and redirect parent-most to login.		
	if(opener != undefined && opener != null)
	{
		if(opener.opener != undefined && opener.opener != null)
		{
			try	{redirectLoginWindow(opener.opener);opener.close();close();return;}
			catch (e) {}
		}
		// only if an error occured:		
		try	{redirectLoginWindow(opener);close();}
		catch (e) {}
	}
}

function redirectLoginWindow(curWin){
	var url = curWin.document.location.href		
	if (url.toLowerCase().indexOf("/bsmart/") > (-1)) {curWin.location = "../../Sitelogin"}
	else {curWin.location = "Sitelogin"}	
}

// Confirm Window,exID=productID from service.
function BSmartConfirmWindow(infoID,exID,compID,bsLocation,exParams)
{
  var return_value = window.showModalDialog("../"+bsLocation+"BSmartHTMLS/BSmartConfirmWindow.aspx?infoid="+infoID+"&extid="+exID+"&compid="+compID+exParams,"","dialogHeight:220px;dialogWidth:300px;scroll:no;status:no;")
  return return_value;
}
function BSmartConfirmSubmit(infoID,exID,compID,bsLocation,exParams)
{
	var newWin;
	newWin = window.open("../" + bsLocation + "BSmartHTMLS/BSmartConfirmSubmit.aspx?infoid="+infoID+"&extid="+exID+"&compid="+compID+exParams,"","width=300,height=220,top=" + (screen.height/2 - 100)  + ",left=" + (screen.width/2 - 150) + ",resizable=0")	
    newWin.focus();    
}
function newImage(arg) {if (document.images) {rslt = new Image();rslt.src = arg;return rslt;}}
function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}
var preloadFlag = false;
function preloadImages() {if (document.images) {preloadFlag = true;}}

function BSmartDirectCall(url)
{
    var encUrl=encodeURI(url);
    var xmlhttp='undefined';
    var objInitialized=false;
    try
    {
        try 
        {
            // IE<5 ?
            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
            objInitialized=true;
        } 
        catch (e) 
        {
            try 
            {   
                // IE6 ?
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                objInitialized=true;
            }
            catch (e) {/*isObjInitialized remain false*/}
        }   
        // mozilla & opera     
        if (!objInitialized) {xmlhttp=new XMLHttpRequest();}
    }
    catch(e)
    {
        //Object creation errored 
        alert('Object could not be created. Verify that active scripting and activeX controls are enabled'); 
        return;
    }    
    xmlhttp.open("GET",encUrl,false);
    xmlhttp.send(null);
    return  xmlhttp.responseText;    
}
