/////////////////////////////////////////////////////////////////////////////////
// BSmart Web Applications (c) all rights reserved.
// BSmartSite.js - Web Site-related functions. 
/////////////////////////////////////////////////////////////////////////////////

// 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="+(screen.height/2-height/2)+",left="+(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();
}
// 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.indexOf("mart/") > (-1)) {curWin.location = "../../htmls/Sitelogin.aspx"}
	else {curWin.location = "Sitelogin.aspx"}	
}
// Confirm Window,exID=productID from service.
function BSmartConfirmWindow(infoID,exID,compID,bsLocation){
  var return_value = window.showModalDialog("../" + bsLocation + "BSmartHTMLS/BSmartConfirmWindow.aspx?infoid=" + infoID + "&extid=" + exID + "&compid=" + compID,"","dialogHeight:220px;dialogWidth:300px;scroll:no;status:no;")
  return return_value;
}
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;}}
// Go2Page function - used by BSmart paging mechanism
function Go2Page(PageNum)
{
	var DocHref = document.location.search;
	DocHrefArr = DocHref.split("&");
	DocHref = "";
	var found = 0;
	var StrTemp = "";
	for(i=0;i<DocHrefArr.length;i++)
	{
		if(String(DocHrefArr[i]).indexOf("BSCRP") != -1)
		{
			found=1;
			StrTemp = String(DocHrefArr[i]).split("=");
			StrTemp[1]=PageNum;
			DocHrefArr[i]=StrTemp[0]+"="+StrTemp[1];
		}
		if(i==0) DocHref += DocHrefArr[i];
		else DocHref += "&"+DocHrefArr[i];
	}
	if(!found)
	{
		if (document.location.href.indexOf("?") == -1) StrTemp = "?";
		else StrTemp = "&";
		DocHref = document.location.href+StrTemp+"BSCRP="+PageNum;
	}
	document.location.href = DocHref;
}
// Search Group
function SearchGroup(SearchID)
{
	var DocHref = document.location.search;
	DocHrefArr = DocHref.split("&");
	DocHref="";
	var found=0;
	var first=0;
	var StrTemp="";
	for(i=0;i<DocHrefArr.length;i++)
	{
		if(String(DocHrefArr[i]).indexOf("BSSEARCH") != -1)
		{
			found=1;
			StrTemp = String(DocHrefArr[i]).split("=");
			StrTemp[1]=SearchID;
			DocHrefArr[i]=StrTemp[0]+"="+StrTemp[1];
		}
		// 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("BSCRP")==-1)
		{
		    if(first==0){DocHref += DocHrefArr[i];first=1}
		    else DocHref += "&"+DocHrefArr[i];
		}
	}
	if(!found)
	{
		if (document.location.href.indexOf("?") == -1) StrTemp = "?";
		else StrTemp = "&";
		DocHref = document.location.href+StrTemp+"BSSEARCH="+SearchID;
	}
	document.location.href = DocHref;
}

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();
    var tmp = xmlhttp.responseText;
    if (tmp.lastIndexOf("<!--*BSmart*-->")!=-1){document.getElementById("tblSearchRes").innerHTML= tmp;}
    else {redirectLoginWindow(window);}
}