﻿
/*This java script has been built for NewUI.*/
function GetRadWindow()
{
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}

function Close()
{    
    GetRadWindow().close();
}

function ShowRadWindowWithUrl(id,navigateurl)
{  
    if(navigateurl.indexOf("_addFavorites_ItemInformation.aspx")>0)
     {
     navigateurl = GetProperUrl(navigateurl);
     }
     var wnd = ShowRadWindow(id);
     wnd.setUrl(navigateurl);     
     wnd.add_close(OnClientClose);
     return false;
}

function GetProperUrl(properurl)
{
    properurl=properurl.replace("/Control","");
    var urlparam= new Array(4);
    urlparam = properurl.split("_");
    var sku = urlparam[0].toString();
    sku=sku.substring(sku.lastIndexOf("/")+1);
    var striid = urlparam[3].toString();
    striid=striid.substring(striid.indexOf("?iid=")+5);
    properurl = urlparam[0].toString().substring(0,urlparam[0].toString().lastIndexOf("/Controls"));
    properurl = properurl + "ItemInformation.aspx?SKU="+ sku +"&Catalog="+ urlparam[1].toString() +"&event=addFavorites&iid="+ striid;
    return properurl;
}


function ShowRadWindow(id)
{
    var oManager = GetRadWindowManager();    
    wnd =  oManager.open(null,id);
    wnd.set_modal(true);
    wnd.center(); // was "moveTo(200,75);" originally but I believe it was only because the center() didn't work    
    wnd.add_pageLoad(OnClientPageLoad);    
    return wnd;
}

function GetID(sender, eventArgs)
{
    var id = sender.get_id();    
    var idx = id.indexOf("_");   
    if(idx>-1)
    {
        var properid = id.split("_");
        id = properid[1];
    }        
    var pageurl = sender.get_contentFrame().contentWindow.location.toString();      
   if(pageurl.indexOf("CreateAccount.aspx") >-1)
       id = 'createAccount';
   else if(pageurl.indexOf("LostPassword.aspx") >-1)
       id = 'lostPassword';      

    return id;  
}
function reloadParent()
{           
var locationurl = parent.location.href;
if(locationurl.indexOf('ModifyAccount')!= -1)
   {
        var redirecturl;
        if(locationurl.indexOf('caller')!=-1)
        {
            redirecturl = locationurl.substring(locationurl.indexOf('caller=')+7);
            redirecturl = redirecturl.replace(/%252f/g,'/');            
            redirecturl = redirecturl.replace(/%2f/g,'/');                  
            locationurl = "http://" +parent.location.hostname + redirecturl;                       
        }
        else        
        {
            locationurl = locationurl.replace(/ModifyAccount/i,"Search");
        }
         if(locationurl.indexOf('?') == -1)
            locationurl = locationurl.replace('&','?');
   }
        parent.location = locationurl;                        
}

function OnClientClose(sender, eventArgs)
{
     var id = GetID(sender, eventArgs);
    if(id!=null)
   {
       switch(id)
       {
            case 'loginWindow':                  
                reloadParent();
                break;
       }
   }
}
function OnClientPageLoad(sender, eventArgs)
 {
   var id = GetID(sender, eventArgs);                      
   if(id!=null)
   {
       switch(id)
       {
            case 'loginWindow':
                sender.set_title('Enter your username and password to login...');
                break;
            case 'emailWindow':
                sender.set_title('Fill out the form to email your image(s)...');
                break;
            case 'requestinfoWindow':
                sender.set_title('Fill out the form to request information for your image(s)...');
                break;
            case 'createAccount':
                sender.set_title('Fill out the form to create an account...');                
                break;
            case 'lostPassword':
                sender.set_title('Enter your email address to retrieve your password...');                
                break;
            case 'logout':
                sender.set_title('Enter your username and password to login...');
                break;
            case 'deleteAccount':
                sender.set_title('Delete your account...');
                break;
            case 'comparewindow':
                sender.set_title('Comparision results...');
                break;
            case 'PrintWindow':
                sender.set_title('Print your item and cover information...');
                break;
            case 'SearchAllProductsWindow':
                sender.set_title('Select from the dropdowns to search all products...');
                break;
        }
   }
 }

function ShowCompareWindow(id,navigateurl)
{
    if(checkNoItems(2))
    {
       wnd = ShowRadWindow(id); 
       wnd.set_title('Comparision results...');
       submitWithUrl(navigateurl,id);       
    }
    return false;
}
function ShowEmailWindow(id,navigateurl)
{
    if(checkNoItems(1))
    {
        var oManager = GetRadWindow().get_windowManager();        
        var wnd  =oManager.open(null,id);
        wnd.set_modal(true);
        wnd.setActive();
        wnd.center(); // was "moveTo(200,75);" originally but I believe it was only because center() didn't work
        submitWithUrl(navigateurl,id); 
    }
    return false;
}

function PostBack(target,argument)
{    
    __doPostBack(target,argument);
}

function trim(s)
{
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}
