
//function to toggle between grid view and list view on the search results page
function toggleView( viewType )
{
    gridViewDiv = document.getElementById( "gridView" );
    listViewDiv = document.getElementById( "listView" );

    if (viewType=="gridView")
    {
        gridViewDiv.style.display = "";
        listView.style.display = "none";
        document.getElementById("viewTypeSelector").innerHTML="Grid <a href=\"Javascript:toggleView('listView')\">List</a>"
    }
    else
    {
        gridViewDiv.style.display = "none";
        listViewDiv.style.display = "";
        document.getElementById("viewTypeSelector").innerHTML="<a href=\"Javascript:toggleView('gridView')\">Grid</a> List"
    }
    document.cookie = "viewType=" + viewType +"; path=/";
}


/*If a product image is not found the display the BR image not found*/
var errorImage=new Image();
errorImage.src='images/imagenot.gif'
function imageError(image)
{
    image.src=errorImage.src;
}

/*
function sets the image size of all the images with the given class name to max 120 X 120.
It also replaces the merchant's image not found to BR image not found.
*/

function setImageSize(imageClassName){
    var images=document.getElementsByClassName(imageClassName);
    for (i=0; i < images.length; i++) {

        if(images[i].src.indexOf('http://www.officedepot.com/pictures/sk/md/no_image.jpg')!=-1
                || images[i].src.indexOf('images/CatID_1.gif')!=-1
                || images[i].src.indexOf('http://shop.com.edgesuite.net/ccimg.catalogcity.com/210000/213000/213049/Products/17777959.jpg')!=-1
                || images[i].src.indexOf('http://shop.com.edgesuite.net/ccimg.catalogcity.com/140000/144400/144467/Products/10161695.jpg')!=-1
                || images[i].src.indexOf('http://shop.com.edgesuite.net/ccimg.shop.com/220000/228700/228719/Products/26807886.jpg')!=-1
                || images[i].src.indexOf('http://shop.com.edgesuite.net/ccimg.catalogcity.com/140000/144400/144467/Products/10156563.jpg')!=-1
                )
        {
            images[i].src=errorImage.src;
        }
        if(images[i].width>120)
        {   images[i].height=images[i].height*(120/images[i].width);
            images[i].width=120;
        }
        if(images[i].height>120)

        {   images[i].width=images[i].width*(120/images[i].height);
            images[i].height=120;
        }
        images[i].style.visibility = 'visible';
    }
    var body = document.getElementsByTagName('body')[0];
    body.style.display = 'none';
    body.style.display = 'block';

}
/*
function sets the image size of all the images with the given class name to max 220 X 220.
*/
function setImageSize220(imageClassName){
    var images=document.getElementsByClassName(imageClassName);
    for (i=0; i < images.length; i++) {
        if(images[i].width>220)
        {
            images[i].height=images[i].height*(220/images[i].width);
            images[i].width=220;
        }
        if(images[i].height>220)
        {
            images[i].width=images[i].width*(220/images[i].height);
            images[i].height=220;
        }
        images[i].style.visibility = 'visible';
    }
    var body = document.getElementsByTagName('body')[0];
    body.style.display = 'none';
    body.style.display = 'block';

}

/*Function to show or hide the info div on the empty grid space, if present, on thesearch results page */
function showHideInfo(infoDivId)
{
    var body = document.getElementsByTagName('body')[0];
    body.style.display = 'none';
    body.style.display = 'block';
    var infoDiv=parent.document.getElementById(infoDivId);
    if(infoDiv!=null)
    {
        infoDiv.style.display = 'block';
        if(infoDiv.offsetLeft==0)
        {
            infoDiv.style.display = 'none';
        }
    }
}

/*used to check for enter key press in signin form.
checkSearchEnter and checkEnter should be optimized to one function*/
function checkEnter(e){ //e is event object passed from function invocation
    var characterCode //literal character code will be stored in this variable
    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e
        characterCode = e.which //character code is contained in NN4's which property
    }
    else{
        e = event
        characterCode = e.keyCode //character code is contained in IE's keyCode property
    }
    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
        document.signinForm.submit() //submit the form
        return false
    }
    else{
        return true
    }
}
/*Returns false if enter key is pressed
 Used to find out enter key press in search box*/
function checkSearchEnter(e){ //e is event object passed from function invocation
    var characterCode //literal character code will be stored in this variable
    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e
        characterCode = e.which //character code is contained in NN4's which property
    }
    else{
        e = event
        characterCode = e.keyCode //character code is contained in IE's keyCode property
    }
    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
        return false
    }
    else{
        return true
    }

}


function callVisitUpdate(url)
{

        uri='/updateVisit.jsp?prePage='+url;

        // branch for native XMLHttpRequest object
        if (window.XMLHttpRequest) 
        {
            req = new XMLHttpRequest();
            req.open('GET', uri, true);
            req.onreadystatechange = function () 
            {
                if (req.readyState == 4) 
                {
                    if(req.status == 200)
                        dump(req.responseText);
                    else
                        dump("Error loading page\n");
                }
            };
            req.send(null);
            // branch for IE/Windows ActiveX version
        } else if (window.ActiveXObject) {

            req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req) {
                
                
                req.open('GET', uri, true);

                req.onreadystatechange = function () {
                    if (req.readyState == 4) {
                        if(req.status == 200)
                            dump(req.responseText);
                        else
                            dump("Error loading page\n");
                    }
                };


                req.send(null);
            }
        }
    
}


function loadXMLDoc(url)
{

    if (document.myForm.last.value==    '' && document.myForm.first.value=='' && document.myForm.email.value=='' && document.myForm.birthMonth.value=='' && document.myForm.birthYear.value=='' && document.myForm.zipcode.value=='' && document.myForm.password.value=='' && document.myForm.again.value=='' && !document.myForm.agree.checked) {
        //do nothing            
    }
    else {

        // branch for native XMLHttpRequest object
        if (window.XMLHttpRequest) 
        {
            req = new XMLHttpRequest();
            req.open('GET', '/visit.jsp', true);
            req.onreadystatechange = function () 
            {
                if (req.readyState == 4) 
                {
                    if(req.status == 200)
                        dump(req.responseText);
                    else
                        dump("Error loading page\n");
                }
            };
            req.send(null);
            // branch for IE/Windows ActiveX version
        } else if (window.ActiveXObject) {

            req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req) {
                req.open('GET', '/visit.jsp', true);

                req.onreadystatechange = function () {
                    if (req.readyState == 4) {
                        if(req.status == 200)
                            dump(req.responseText);
                        else
                            dump("Error loading page\n");
                    }
                };


                req.send(null);
            }
        }

    }
}

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            numbers.result.value = req.responseText;
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}


///Added code for intercept Alert By Vishal
    
    var internal_clicked = false;	 
    function processLinks(){ //set internal_clicked on all links
        var links = document.getElementsByTagName("A");
        for (var i=0; i < links.length; i++){
            if(!links[i].onclick) links[i].onclick = function(){
                internal_clicked = true;
            }
        }
        //set internal_clicked on all form submits
        var forms = document.getElementsByTagName("FORM");
        for (var i=0; i < forms.length; i++){
            if(!forms[i].onsubmit) forms[i].onsubmit = function(){
                internal_clicked = true;
            }
        }        
        
        
    }
    /*function showpopup(){ 
    if(!internal_clicked){
            window.open("interceptalert.jsp", "offer_popup", "width=600, height=250, resizable=no, scrollbars=no, toolbar=no, location=no, directories=no, status=no, menubar=no, copyhistory=yes");
            //location.href="interceptalert.jsp";
            //location.target="_BLANK"
    }
    }*/