﻿jQuery(document).ready(function() {
    jQuery('#btnSearch').click(function() {
        // - get value of searchkeyword
       var keyword = jQuery('#searchKeyword').val();
        var itemPerpage = jQuery('#itemPerpage').val();
        
        keyword = regularSearchTearm(keyword);
        keyword = stringTrim(keyword);

        var itemCount = "";
        var temp = "";
        var itemCountCookie = "";
        //Get item counts
        if (keyword != '' && keyword != 'SEARCH SITE') {
            jQuery.getJSON('/Home/GetDimensionId', function(data) {
                itemCount = data.objectId;        
                
                itemCount = itemCount.replace('-', '/'); 
                temp = itemCount.substring(0, itemCount.indexOf('/') + 1);                   
                try{
                    itemCountCookie = getCookie('ItemCount');
                }
                catch(Error){
                }
                
                if(itemCountCookie != undefined && itemCountCookie != '')
                {   
                    itemCount = temp + "/" + itemCountCookie;
                }
                else if(itemPerpage != undefined && itemPerpage != '')  
                {
                    try{
                        itemCount = temp + "/" + itemPerpage;
                    }
                    catch(Error)
                    {
                    }
                }
                
                itemCount = itemCount.replace("//", "/");
                
                //Redirect to search page.
                window.location = '/Search/List/' + keyword + '/' + itemCount + '/';
            }); 
        }
    });
});

// - Key press handle
function keyhandlers(evt, buttonClick) {
    evt = (evt) ? evt : (window.event) ? event : null;
    try {
        if (evt) {
            var enteredKey = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
            if (enteredKey == 13) {
                jQuery('#' + buttonClick).click();
            }
        }
    }
    catch (e) {}
}

// - Sign up promotion
jQuery(document).ready(function() {
    jQuery('#btnSignUpEmail').click(function() { 
		var email = jQuery('#email').val();
            
		if (email == null || email == '') {
			jQuery('#message').html('Please enter your E-mail address');
			return false;
		}
		if (!echeck(email)) {
			jQuery('#message').html('Invalid E-mail address');
			return false;
		}
		window.location = '/account/NewsletterSignup.aspx?email=' + email;   
	});
});

// Remove space.
function leftTrim(sString)
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    return sString;
}

function rightTrim(sString)
{
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function stringTrim(sString){
    return leftTrim(rightTrim(sString));
}

// Get cookie
function getCookie(name) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) &&
    ( name != document.cookie.substring( 0, name.length ) ) )
    {
    return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}

// Clean special characters.
function regularSearchTearm(keyword) {
    var keyvalue = keyword.replace(/[^a-zA-Z 0-9]+/g, '');

    return keyvalue;
}

// - Link show all
function showMore(section, id, i) {
    //var i = 20;
    if (id == 1) {
        
            objSpan = document.getElementById('spanItem-' + section + '-' + i);
            while (objSpan != null) {
                if (objSpan.style.display == 'none')
                    objSpan.style.display = 'block';
                i++;
                objSpan = document.getElementById('spanItem-' + section + '-' + i);
            }
            document.getElementById('lnkMore' + section).style.display = 'none';
            document.getElementById('lnkLess' + section).style.display = 'inline';
    }
    //i = 10;
    if (id == 2) {
            objSpan = document.getElementById('spanItem-' + section + '-' + i);
            while (objSpan != null) {
                if (objSpan.style.display == 'block')
                    objSpan.style.display = 'none';
                i++;
                objSpan = document.getElementById('spanItem-' + section + '-' + i);
            }
            document.getElementById('lnkMore' + section).style.display = 'inline';
            document.getElementById('lnkLess' + section).style.display = 'none';
    }
}


//Help modules
function echeck(str) { 
    var at = "@"; 
    var dot = "."; 
    var lat = str.indexOf(at); 
    var lstr = str.length; 
    var ldot = str.indexOf(dot); 
    if (str.indexOf(dot + dot) > 0) { return false } 
    if (str.indexOf(at) == -1) { return false } 
    if (str.indexOf(at) == 0 || str.indexOf(at) == lstr) { return false } 
    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) { return false } 
    if (str.indexOf(at, (lat + 1)) != -1) { return false } 
    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) { return false } 
    if (str.indexOf(dot, (lat + 2)) == -1) { return false } if (str.indexOf(" ") != -1) { return false } 
    return true 
} 

//Function for menu
function HideContent(d) {
    if(d.length < 1) { return; }
    document.getElementById(d).style.display = "none";
}

function ShowContent(d) {
    try
    {
        if(d.length < 1) { return; }
        document.getElementById(d).style.display = "block";
        FixIssueIE6();
    }
    catch(e)
    {
        alert('error: ' + e.message);
    }
    
    return false;
}

function ReverseContentDisplay(d) {
    if(d.length < 1) { return; }
    if(document.getElementById(d).style.display == "none") { 
        document.getElementById(d).style.display = "block"; 
        FixIssueIE6();
    }
    else { document.getElementById(d).style.display = "none"; }
}

// Show/Hide menu.
function FixIssueIE6()
{  
    var divTag = document.getElementById("dropdownnav");
    var divContentTag = document.getElementById("dropdowncontent");
    
    divContentTag.style.width = divTag.style.width;
    divContentTag.style.height = divTag.style.height;
    divContentTag.style.left = 0;
    divContentTag.style.top = 0;
    
    var iFrameTag = document.getElementById("selectblocker");
    if(iFrameTag != undefined)
    {
        iFrameTag.style.left = 0;
        iFrameTag.style.top = 0;
        iFrameTag.style.width = 150;
        //iFrameTag.style.height = 292;
		var itemCounts = document.getElementById("departCounts").value;
		if(itemCounts != undefined)
		{
			iFrameTag.style.height = 40 + 21*parseInt(itemCounts);
		}
    }
}

function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

/* add new function to show, hide image */
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}

}

// - ads tag
jQuery(document).ready(function() {
    try{
        jQuery('#ad728x80').append(AAMB1);
        jQuery('#ad160x600').append(AAMB2);
    }
    catch(e)
    {
        alert('error: ' + e.message);
    }
});
