var frmSearch;
var frmSendTo;
var SearchExists, SendToExists, ShoppingCartExists;
var frmSize, AllowOpt;
var tempImg;
var sGridHtml = "";
var ResourceType = null;//for CallItemGrid only
var sCart;
var frmShoppingCart;

SendToExists = SearchExists = ShoppingCartExists = false;

frmSize = 100;
AllowOpt = 'Select';


//when the search form is called
function CallSearch(ObjectType, ObjectID)
{
    CloseSearch();
    var SearchString = "";

    if ( (ObjectType != "") && (ObjectID != "") ){
        SearchString = "?ObjectType=" + ObjectType + "&ObjectID=" + encodeURIComponent(ObjectID);
    }

    //open the window and send it the price code 
    frmSearch = window.open("wfrmSearch.aspx" + SearchString,"_blank"," location=no, direction=no, menubar=no, titlebar=no, toolbar=no, scrollbars=yes, status=no, resizable=yes, top=100px, left=150px");
    frmSearch.resizeTo(700,520);
    frmSearch.focus();
    SearchExists = true;
}


//when page is unloaded unload the form along with it
function CloseSearch()
{
    if (SearchExists)
        frmSearch.close();
}

function CallShoppingCart()
{
	CloseShoppingCart();
	
	var ExternalSystemID = document.forms[0].ExternalSystemID.value;
    frmShoppingCart = window.open('wfrmShoppingCart' + ExternalSystemID + '.aspx', "_blank", " location=no, direction=no, menubar=no, titlebar=no, toolbar=no, scrollbars=yes, status=no, resizable=yes, top=100px, left=150px");
    frmShoppingCart.resizeTo(590, 400);
    frmShoppingCart.focus();
	ShoppingCartExists = true;
}

function CloseShoppingCart()
{
	if (ShoppingCartExists)
		frmShoppingCart.close();
		
	ShoppingCartExists = false;
}

//set the initial size from schematic form and to display the zoom in/out tools
function IntSize(size)
{
    frmSize = size;
    
    //!!! it's a workaround for some bug that I don't have time to fix :)
    if(!document.getElementById("optZoomSize")) return;
    
    document.getElementById("optZoomSize").value = size;
    document.images["ImgZoomIn"].style.display = "";
    document.images["ImgZoomOut"].style.display = "";
    document.getElementById("optZoomSize").style.display = "";
    document.images["ImgScroll"].style.display = "";
    document.images["ImgSelect"].style.display = "";
    top.fraimage.AllowOption(AllowOpt);
}

//called when the size of the zoom is changed in the combo
function ChangeSize(size, Order)
{
    if(checkSchematicLoaded())
        top.fraimage.ImgZoom( eval(size), Order);
}

function checkSchematicLoaded(){
    try{
        return (top.fraimage.document.images["imgSchematic"] != null);
    }catch(ex){
        return false;
    }
}

//called wen the zoom images are clicked
function CallZoom(Order)
{
    ChangeSize(frmSize, Order);
}

//when the allow zoom button is clicked
function AllowZoom()
{
    //change the color of the button when it is selcted ..and the change the color 
    //of the other buttons to have a pressed and unpressed effect
    AllowOpt = 'Zoom';

    document.images["ImgScroll"].style.backgroundColor = '#d0d1d0';
    document.images["ImgSelect"].style.backgroundColor = '#d0d1d0';

    //to set to the schematic that the zoom feature has to be enabled
    top.fraimage.AllowOption(AllowOpt);
}

// function clear differences clear or select
function ClearAll()
{
/*  var imgScroll = document.images.item("ImgScroll");

    if(imgScroll != null && document.forms[0].ScrollImage != null)
    {
        imgScroll.src = document.forms[0].ScrollImage.value;
        imgScroll.style.backgroundColor = "#FFFFFF";
    }
    
    var imgSelect = document.images.item("ImgSelect");


    if(imgSelect != null && document.forms[0].SelectImage != null)
    {
        imgSelect.src = document.forms[0].SelectImage.value;
        imgSelect.style.backgroundColor = "#FFFFFF";
    }*/
}

//when the allow multi slect button is clicked
function AllowSelect()
{
    AllowOpt = "Select";

    var imgSelect = document.images["ImgSelect"];
    var imgScroll = document.images["ImgScroll"];

    imgSelect.src = imgSelect.attributes.image.value;
    imgScroll.src = imgScroll.attributes.alternatingImage.value;

    //to set to the schematic that the select feature has to be enabled
    if(checkSchematicLoaded())
        top.fraimage.AllowOption(AllowOpt);
}

//when the allow select button is clicked
function AllowScroll()
{
    AllowOpt = "Scroll";
    //ClearAll();
    var imgSelect = document.images["ImgSelect"];
    var imgScroll = document.images["ImgScroll"];

    imgScroll.src = imgScroll.attributes.image.value;
    imgSelect.src = imgSelect.attributes.alternatingImage.value;

    //to set to the schematic that the select feature has to be enabled
    if(checkSchematicLoaded())
        top.fraimage.AllowOption(AllowOpt);
}

//calling the disable zoom feature when a pdf / doc are selected since they cannot be zoomed
function DisableZoom()
{           
    //hide all the zooming tools

    document.images["ImgZoomIn"].style.display = "none";
    document.images["ImgZoomOut"].style.display = "none";
    document.forms[0].optZoomSize.style.display = "none";
    //  document.getElementById("ImgZoom").style.display = "none";
    document.images["ImgScroll"].style.display = "none";
    document.images["ImgSelect"].style.display = "none";
}           

function PrintEnable(){
    var img = document.images["ImgPrint"];

    //TODO It's work's only for IE
    //if disabled by web.config
    if(img.attributes.ServerDisabled.value.toUpperCase() == "TRUE"){ 
        PrintDisable();
        return;
    }
    img.src = img.attributes.image.value;
    img.disabled = false;
}

function PrintDisable(){
    var img = document.images["ImgPrint"];
    //TODO It's work's only for IE
    img.src = img.attributes.alternatingImage.value;
    img.disabled = true;
}

function isPressEnter(evnt)
{
    //TODO It's work's only for IE
    if(IE6_up)
        return (window.event.keyCode == 13);
    else if (NS6_up)
    {
        if(evnt.which == 13)
            return true;
        else
            return false;
    }
}

//called from the search form
function ShowLink(LinkRelId, LinkTo_ID, isSerialSearch)
{
    var Tier;//, LName;
    var LID = LinkTo_ID;

    //get the pricing tier and the name of the loading image
    //LName = document.getElementById("txtLName").value;
    
    if (isSerialSearch)
    {
        //var tempStr = "wfrmTree.aspx?LID="+ LID + "&SearchText=" + LinkRelId;
        //top.fratree.location.href = tempStr;
        top.fratree.document.forms[0].hSearchedModelRelID.value = LID;
        top.fratree.document.forms[0].hSearchText.value = LinkRelId;
                
        top.fratree.document.forms[0].hLinkRelId.value = "";
        top.fratree.document.forms[0].hPartID.value = "";
        
    }
    else
    {
        //var tempStr = "wfrmTree.aspx?LinkRelId=" + LinkRelId + ((LinkTo_ID != null) ? ("&PartID=" + LinkTo_ID) : "");
        //top.fratree.location.href = tempStr;
        top.fratree.document.forms[0].hLinkRelId.value = LinkRelId;
        top.fratree.document.forms[0].hPartID.value = ((LinkTo_ID != null) ? (LinkTo_ID) : "");

        top.fratree.document.forms[0].hSearchedModelRelID.value = "";
        top.fratree.document.forms[0].hSearchText.value = "";
    }
    
    top.fratree.document.forms[0].action = "wfrmTree.aspx";
    top.fratree.document.forms[0].submit();
    //window.focus();
}

function Init()
{
    var strSource, PrintVal;
    var isMailPresent = false;
    var isPrintPresent = false;
    AllowSelect();

	sCart = new SoppingCart();
}

function CallSendTo()
{
    if (SendToExists)
        frmSendTo.close();
        
    var features = "location=no, direction=no, menubar=no, titlebar=no, toolbar=no, scrollbars=no, status=no, resizable=no, left=0, top=0, width=617, height=300";
    frmSendTo = window.open("wfrmSendTo.aspx", "",features);
    SendToExists = true;
}

//================================================================================================
function PrintPreview()
{               
    if(document.images["ImgPrint"].disabled == true) return;
    
    if(top.fraimage == null){
        PrintDisable();
        return;     
    }

    if(!checkSchematicLoaded()){ 
        PrintDisable();
        return;
    }

    window.open("PrintPreview.aspx?LinkRelId="+top.fraimage.document.forms[0].hLinkRelId.value+"&ImageURL="+top.fraimage.document.images["imgSchematic"].src,"_blank");
}

//================================================================================================
function LogError(msg)
{
    window.status = msg;
}






