var defexpires = new Date();
var expired = new Date();

defexpires.setTime(defexpires.getTime() + 24 * 60 * 60 * 365 * 1000);
expired.setTime(expired.getTime() - (24 * 60 * 60 * 365));

//
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to return the value of the cookie specified by "name".
//
function GetCookie (name) {
    var end = 0;
    var result = 0;
    var myCookie = " " + document.cookie + ";";
    var searchFor = " " + name + "=";
    var start = myCookie.indexOf(searchFor);
    if (start > -1) {
        start += searchFor.length;
        end = myCookie.indexOf(";", start);
        if (end > -1) result = unescape(myCookie.substring(start, end));
    }
    return result;
}
//
//  Function to create or update a cookie.
//
function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : "/";
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
	
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? ("; expires=" + defexpires.toGMTString()) : ((expires <= 0) ? ("; expires =" + expired.toGMTString()) : ("; expires =" + expires.toGMTString())) ) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}
//
// 	Function to clear cookie
//
function ClearCookie(name) {
	SetCookie(name,'',0);
}


function DeleteCookie(name) {
  document.cookie = name + '=; expires=' + expired.toGMTString() + '; path=/; domain=.ingrammicro.com';
  document.cookie = 'SMTRYNO=; expires=' + expired.toGMTString() + '; path=/; domain=.ingrammicro.com';  
  //document.cookie = "SMCHALLENGE=" + escape("YES") + "; expires=" + expired.toGMTString();
  //SetCookie(SMCHALLENGE,"Yes");
  //SetCookie(SMSESSION,"");
}

function ShowCookieWarning() {
    var date = new Date();
    var value = date.getTime();
	var cname = "CookieTest";
	
    SetCookie(cname, value);

    var realValue = GetCookie(cname);
    ClearCookie(cname);

    if(realValue == 0 || realValue != value)
        this.location = '%dummy%';

}

function searchRedirectProductForm() {
	SetProductCookie();
	document.quicksearchProduct.action = "javascript:quickSearchFormByProduct()";
}
function searchRedirectSKUForm() {
	SetSKUCookie();
	document.quicksearchSKU.action = "javascript:quickSearchFormByNumber()";
}
