/*******************************************************************************
 *
 *      Global Javascript for Shoprogers.com
 *
 *      Place in this file any Javascript functions that can be used
 *      globally by the site. This includes the famous popup() function
 *      and/or any others.
 *
 *      Benefits:
 *       - maintain only a single copy of scripts widely used - this makes debugging
 *         much more manageable.
 *       - take advantage of browser caching of code
 *       - keep client-code files (i.e. asp files) cleaner and free from "noise"
 *
 *      Please observe the following coding conventions when writing functions
 *      to this file:
 *      1)  Functions should be named with a verb, as in "getParameter()" or
 *          "setParameter()" - avoid 'noun' based naming unless writing an
 *          Object Constructor
 *      2)  Functions should always return a value, unless it is changing a value
 *          in a global variable or in the case of the popup() function, performing 
 *          a browser operation. Even in that case, you *can* return something.
 *      3)  Functions should be reusable. Avoid internal use of global variables, and
 *          instead pass the global as a parameter.
 *      4)  Avoid calling 'document.write()' from a function. Instead, have the 
 *          function return the value you wish to 'document.write()', and call
 *          'document.write()' from the client code.
 *              e.g.: document.write( getCookie( "ebizCookieName" ) );
 *      5)  Include documentation that describes what the function does, and
 *          the types/values of its parameters.
 *
 *      December 3, 2002
 *      Richard Fedoriuk, Rogers RSS ASM
 *      rfedoriu@rci.rogers.com
 *
 ******************************************************************************/


/**
 *  srPopup
 *  Opens a new window pointing to 'url', with options 'windowOptions'
 *  param url a String identifying the url to load into the new window
 *  param windowName a String identifying the name of the new window; leave empty to always open a new browser window
 *  param windowOptions a String identifying the options for the new window
 *  returns Window Object of the new window
 */ 
 function srPopup ( url, windowName, windowOptions ) { 
    var newWin = open ( url, windowName, windowOptions );
    return newWin;
 }

 
 /**
 *  popUp
 *  Opens a new window pointing to 'url', with hard-coded options
 *  param url a String identifying the url to load into the new window
 *  returns Window Object of the new window
 */ 
function popUp(url) {
    sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
    self.name = "mainWin"; 
}

function popup_service() {
    clickpop = window.open("rhsi_popup_service.html","popup","toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,top=100,left=100,width=630,height=500");
}

function popup_competition() {
    clickpop = window.open("rhsi_popup_competition.html","popup","toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,top=100,left=100,width=620,height=500");
}

function popup_installation() {
    clickpop = window.open("rhsi_popup_installation.html","popup1","toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,top=100,left=100,width=627,height=490");
}

function popup_availability() {
    clickpop = window.open("/store/cable/CableSearch/availability_search.asp?shopperID=&nav=ahsearch&ProvinceCd=","popup1","toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,top=100,left=100,width=777,height=500");
}

function popup_hispeed_services( url ) {
    clickpop = window.open( "/contentmgmt/store/cable/internetcontent/" + url ,"popup_hispeed_services","toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,top=100,left=100,width=413,height=350");
}


/**
 *  doUnlessCookie
 *  Checks for the presence of a cookie, and executes a function if no cookie exists
 *  sets a cookie to expire in 'expireDays' so that the function doesn't execute again
 *
 *  param cookieName a String identifying the name of the cookie to check for, 
 *        or set if it doesn't exist
 *  param cookieValue a String or Number to set as the value for this cookie
 *  param expiryDays a String or Number to set as the number of days to the 
 *        cookie's expiry date from now
 *  param functionToDo a String representing the function name that should be 
 *        executed if cookie is not present; do not include parentheses in function name
 *  param functionParams an Array containing the parameters to be passed to the 
 *        function named in 'functionToDo'; may contain any legal variable types
 *  Does not return any value unless the function
 *
 *  EXAMPLE:
 *      var myObj = new Object();
 *      myObj.name = "My String";
 *      myObj.value = "Some Value";
 *      var params = new Array( "hello", mystring ); // Array of parameters
 *      doUnlessCookie ( "richard2", "apt 1102", 60, "popup", params );
 */
function doUnlessCookie ( cookieName, cookieValue, expiryDays, functionToDo, functionParams ) {
   
    var returnValue = '';
    
    if ( hasCookie( cookieName ) != true ) {
        setCookie( cookieName, cookieValue, expiryDays );
        
        var f_parameters = '';        
        
        for ( i=0; i<functionParams.length; i++ ) {            
            f_parameters += "functionParams[" + i + "]";            
            if ( i < (functionParams.length-1) ) {
                f_parameters += ", ";
            }
        }
        returnValue = eval ( functionToDo + "(" + f_parameters + ")" );
    }
    
    return returnValue;
}




/**
 *  hasCookie
 *  Indicates whether the cookie with the given name exists
 *  param myCookieName a String that identifies the name of the cookie, follows standard variable naming conventions
 *  returns true if cookie exists, false otherwise
 */
function hasCookie ( myCookieName ) {

    var allcookies = unescape(document.cookie);
    var cookieArray = allcookies.split(';');
    var hasCookie = false;
    
    // parse through all cookies and find out if the one requested exists
    for (i=0; i<cookieArray.length; i++) {        
        var cookie = cookieArray[i].split('=');
		
        var cookiename = cookie[0].replace(/\W/g, '');
        var cookievalue = cookie[1];
        
        if ( cookiename == myCookieName ) {
            hasCookie = true;
            break;
        }
    }     
     
    return hasCookie;
}


/**
 *  getCookie
 *  Gets the value of a cookie
 *  param myCookieName a String identifying the name of a cookie to retrieve the value for
 *  returns a String representing the value of the cookie
 */
function getCookie ( myCookieName ) {

    var allcookies = unescape(document.cookie);
    var cookieArray = allcookies.split(';');
    var cookievalue = '';
    
    // parse through all cookies and find out if the one requested exists
    for (i=0; i<cookieArray.length; i++) {        
        var cookie = cookieArray[i].split('=');
		
        var name = cookie[0].replace(/\W/g, '');
        var value = cookie[1];
        
        if ( name == myCookieName ) {
            cookievalue = value;
            break;
        }
    }     
     
    return cookievalue;
}
   
 
/**
 *  setCookie
 *  Sets a cookie 
 *  param cookieName the name of the cookie, follow standard variable naming conventions
 *  param cookieValue the value of the cookie
 *  param daysToExpire the number of days to expiry - can be more than 1 year (i.e. 365+ days)
 *  does not return any value
 */    
function setCookie ( cookieName, cookieValue, daysToExpire ) {

    var dateArg = daysToExpire * ( 24 * 60 * 60 * 1000 ); // milliseconds
    var expires = new Date( dateArg + (new Date()).getTime() );
    
    document.cookie = (
          cookieName + "=" + escape(cookieValue)
        + "; expires=" + expires.toUTCString()
    );
    
    return document.cookie;
}


   
/**
 *  expireCookie
 *  Expires the cookie by setting expiry date to now; if cookie does not exist, creates it and immediately expires it
 *  param cookieName a String identifying the name of the cookie, follows standard variable naming conventions
 *  does not return any value
 */ 
function expireCookie ( cookieName ) {
    
    var expires = new Date();    
    
    document.cookie = (
          cookieName + "=" + escape("nothing")
        + "; expires=" + expires.toUTCString()
    );
    
    return document.cookie;
}   



/**
 * launch the URL in the parent window then close own window
 * use in pop up window
*/

function popup_CloseAndLaunch(url) {

	window.opener.location.href = url;
	window.close();

}

 /**
 * wepopUp
 *  Opens a new window pointing to 'url', with hard-coded options
 *  param url a String identifying the url to load into the new window
 *  returns Window Object of the new window
 */ 
function wepopUp(url) {
    sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=650,height=450');
    self.name = "mainWin"; 
}




//	PARAM=<%= right(justSID, len(justSID)-11) %>
function LAUNCH( justSID ){
									//	
	location.href = "/store/wireless/products/phones/overview.asp?shopperID=" + justSID + "&features=2&area=1";
	PARAM="shopperID=" + justSID + "&features=2&area=1";
	POPUP=window.open("/store/wireless/products/phones/images/featured_phones.html?"+PARAM+"", "FeaturedPhones", "width=606,height=447,fullscreen=0,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
	POPUP.moveTo(100,100);
	POPUP.focus();
}





// global array containing any/all functions to be run at onload time
// this array is populated with the global function named "add_onload()"
// see documentation for add_onload() function
var onload_functions_array = new Array();

// set the window to execute all onload functions
// *MUST* come after declaration of 'onload_functions_array'
window.onload = onload_functions;


/**
 * void add_onload()
 *
 * Pushes a function-call into a global array (named 'onload_functions_array').
 * This array is traversed by the 'onload_functions' function and all functions 
 * are executed after the page loads.
 * Use this function anywhere in your script, placing the full function-signature
 * as a String for the argument.
 *
 * param (String) function_signature The function signature (included any parameters)
 *                                   to be executed.
 * 
 * example: add_onload( 'my_onload_function( "red", "blue", 123 )' );
 *          This queues a call to 'my_onload_function()' with the above parameters. 
 *          Your function may not require any parameters.
 */
function add_onload( function_signature ) {
	onload_functions_array[ onload_functions_array.length ] = function_signature;
}




/**
 * void onload_functions()
 *
 * Loops through global array 'onload_functions_array' and executes each function
 * which has been added to the array. This function is globally set to be run
 * whenever a page loads. If no functions have been pushed into the array, 
 * then nothing will happen.
 */
function onload_functions() {
	
	for (fn in onload_functions_array) {
		eval( onload_functions_array[fn] );
	}
}

/**
* load Recommendation tool 
*/

function OpenRecoWin(pageNameAndParam,winWidth,winHeight){
     FindOutMoreWindow = window.open(pageNameAndParam,"FindOutMorePoppa","width=" + winWidth + ",height=" + winHeight + ",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0");
}
				
/**
* aug 29/06
* applied by wrapping standing object code with the javascript function
* used to bypass the activex "click to activate" on interactive flash objects
* example:
*			CreateOBJ('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="624" height="196">');
*			CreateOBJ('<param name="movie" value="flashfile.swf" />');
*			CreateOBJ('<param name="quality" value="high" />');
*			CreateOBJ('<embed src="flashfile.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="624" height="196"></embed>');
*			CreateOBJ('</object>');
*/
function CreateOBJ(x) {
	document.write(x);
}
