// cookie functions

var expHrs=1;
var CookieInfoStr='';
thispage = location.href;

var exp = new Date(); 
exp.setTime(exp.getTime() + 3 * 24 * 60 * 60 * 1000); //expire in 3 days
//+ (expHrs*60*60*1000)); //expire in 1 hr  

function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}

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 domain = '.taltech.';
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function showRawCookie () {  
 var Rawstr= (document.cookie);
 return Rawstr;
}

function showCookie () {  
var bigStr = GetAllCookie();
bigStr = StrReplace(bigStr, '; ', '<br>');
return (bigStr);
}

function DelAllCookie(){

var exp = new Date();  
exp.setTime (exp.getTime() - 1);  

KeyArray = new Array();
var cookieStr =  unescape(document.cookie) + '; ';
KeyArray = cookieStr.split('; ');
q=0;

alertmsg=('Empty shopping cart?  ');

Ans=true;

  if (location.href.indexOf('welcome.htm')<0)
    Ans= window.confirm(alertmsg);

  if (Ans==true){

    while(KeyArray[q]!=null){
    KeyArray[q] = KeyArray[q] +'=';
    KeyArray[q] = KeyArray[q].substring(0,KeyArray[q].indexOf('='));
    SetCookie (KeyArray[q],'',exp,'/');
    DeleteCookie (KeyArray[q] );
    q++;
    }

    document.cookie = "; expires=" + exp.toGMTString();
    CartStr ='';

  }

  //alert(location.href.indexOf('welcome.htm'));

  //if (location.href.indexOf('welcome.htm')>0)
	//location.href='cart.htm';
	
}

function SetFormCookie(object){  
  keyStr = object.Key.value;
  valStr = object.Value.value;
  expStr = object.Expires.value;
  expInt =  (expStr+1-1);

  exp = new Date(); 
  exp.setTime(exp.getTime() + (expInt*24*60*60*1000));

  SetCookie (keyStr ,valStr, exp, '/');
  alertmsg=('Set cookie: ' +'\nKey:      '+ keyStr +'\nValue:   '+ valStr +'\nExpires: '+ exp+'\n\nRefresh display now? ');
  Ans= window.confirm(alertmsg);

  if (Ans==true)
	location.href= thispage;
}

function GetAllCookie () {  
var cookieStr = unescape(document.cookie);
var cookieLen = document.cookie.length; 
var retStr='';
thisDoc =CleanUp(location.href);
thisPath=PathOf(thisDoc);

retStr= retStr+ ('Cookie path: '+ PathOf(location.href) + '<br>Cookie length: '+cookieLen+'<br>Cookie variables:<br>'+ cookieStr );
return retStr;
}

function getCookieVal (offset) {  
var endstr = document.cookie.indexOf (";", offset);  
if (endstr == -1)    
endstr = document.cookie.length;  
return unescape(document.cookie.substring(offset, endstr));
}

////////////////////////////////////////////////////////////////////////////////////////////
//   Misc Functions
////////////////////////////////////////////////////////////////////////////////////////////

function toCur(floatstr){
  var tmp= '';
  tmp= tmp + floatstr;
  tmpArray = new Array();
  tmpArray = tmp.split('.')

  var p0 = '';
  var p1 = '';

  (tmpArray[0]==null)? p0='0' :p0=tmpArray[0];
  (tmpArray[1]==null)? p1='00' :p1=tmpArray[1] + '00';
  p1=p1.substring(0,2)

  tmp= '£'+p0 + '.' + p1;
  return tmp;

} 

function StrReplace(sWhere ,sWhat, sWith){
str=sWhere;
while(str.indexOf(sWhat)>-1) {
  str=StrReplaceOnce(str ,sWhat, sWith);
} 

return (str);
}

function StrReplaceOnce(strWhere ,strWhat, strWith){
var strLength=strWhere.length;
str1=strWhere;
var pos1= str1.indexOf(strWhat);
if (pos1>-1){
  str2= str1.substring(0,pos1).concat(strWith);
  str2= str2.concat(str1.substring(pos1+strWhat.length,strLength))
  str1=str2;

}
return str1;
}

function theTime() { 
  var t=new Date();
  tmp=t+' ';
  return tmp.substring(0,19)
}

function gotoURL(Url) { 
  if ((Url!='')&&(Url!=null)){
	if (Url.indexOf('<%')<0) 
	    location.href=Url;
  }
}

function PathOf(strpath){
var ptr;
var path;
var t=strpath.length;
subs='\\';
ptr= strpath.lastIndexOf(subs,t);
if (ptr==-1){
  subs='/';
  ptr= strpath.lastIndexOf(subs,t);
}
path= strpath.substring(0,ptr+1)
return CleanUp(path);
}

function CleanUp(strclean){
var ptr2;
var ret ='';
var t=strclean.length;
substr2='file:///';
ptr2= strclean.indexOf(substr2);
(ptr2>=0) ? ret = strclean.substring(ptr2+substr2.length,t):ret = strclean;
return ret;
}

function isEmail(argvalue) {
  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("@") == -1)
    return false;
  else if (argvalue.indexOf("@") == 0)
    return false;
  else if (argvalue.indexOf("@") == (argvalue.length-1))
    return false;
  arrayString = argvalue.split("@"); 
  if (arrayString[1].indexOf(".") == -1)
    return false;
  else if (arrayString[1].indexOf(".") == 0)
    return false;
  else if (arrayString[1].charAt(arrayString[1].length-1) == ".") {
    return false;
  }
  return true;
}

function nada (){ 
return;
}

function IsIE4(){ return (document.all);}
function IsNN4(){ return (document.layers);}
function IsNN3(){ return (document.images);}


function writeDiv(id_no){
if (IsIE4()){
  if (id_no>0){
    document.write('<a href=\"javascript\:SwapDisplay('+id_no+')\" title=\"Click here to show or hide these details... \">show/hide</a><p>');
    document.write('<div id="idBlurb'+id_no+'" style="DISPLAY\: none">');
  }
  else{
    document.write('</div>');
  }
}
}
