function _getType(id,key)
{
  var type = '';
  if((8917 == id))
  { /*normal cap*/
    type = 'cap'
  }else if((8979 == id))
  { /*custom cap*/
    type = 'cap_custom'
  }else if((8918 == id))
  { /*normal bag*/
    type = 'bag'
  }else if((8980 == id))
  { /*custom bag*/
    type = 'bag_custom'
  }else if((2681 == id)||(2683 == id)||(8218 == id)||(8216 == id)||(8212 == id)||(8210 == id))
  {/*classic, classic child, city, city child, university, university child*/
    type = 'hs_short';
  }else if((3075 == id)||(8214 == id)||(8208 == id))
  {/*classic, city, university*/
    type = 'hs_short_custom';
  }else if((2682 == id)||(8217 == id)||(8211 == id))
  {/*classic, city, university, */
    type = 'hs_long';
  }else if((3076 == id)||(8215 == id)||(8209 == id))
  {/*classic, city, university*/
    type = 'hs_long_custom';
  }
  return type;
}

function _getCustomData(id,key)
{
  var type = 'undefined'
  type = _getType(id,key);
  if(('cap' == type || 'bag' == type || 'hs_short' == type || 'hs_long' == type) && checkShirt(id,key))
  {
    return collectShirtCustomData(type,id,key);
  }else if(('cap_custom' == type || 'bag_custom' == type || 'hs_short_custom' == type || 'hs_long_custom' == type) && checkCustomShirt(id,key))
  {
    return collectShirtCustomData(type,id,key);
  }else
  {
    return false;
  }
  return true;
}

function checkShirt(id,key)
{
  if(8210 == id || 8216 == id || 2683 == id)
  {
    if(!isRaglanShirt())
    {
      alert('Für Kinder gibt es keine unifarbigen shirts.');
      $('shirttype').focus();
      return false;
    }
  }
  if($F('kanton') == '0')
  {
    alert(message_kanton);
	  $('kanton').focus();
    return false;
  }
  if($F('location') == '0')
  {
    alert(message_ort);
	  $('location').focus();
    return false;
  }
  return true;
}
function checkCustomShirt(id,key)
{
  if ($F('customtext1_'+key).length == 0)
  {
    alert("Bitte wählen Sie ein Schriftzug aus.");
	  $('customtext1_'+key).focus();
    return false;
  }
  return true;
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);

   return output;
}
function getBase64Param(type,id,key)
{
  var jahr 		    = getJahr();
  var ort1		    = getOrt(type,id,1);
  var ort2		    = getOrt(type,id,2);
  var design      = getDesign();
  var object      = getHSObject(type,id,key);
  var farbe       = getFarbe();
  var kanton      = getKanton(type);
  var area        = getArea();
  var basestring  = 'format=swf'+'&ort='+ort1+'&ort2='+ort2+'&jahr='+jahr+'&area='+area+'&kanton='+kanton+'&farbe='+farbe+'&design='+design+'&objekt='+object+'&land=CH';
  return encode64(basestring);
}
function getFlashVar(type,id,key)
{
  var gJahr		= getJahr();
  var gFarbe	= getFarbe();
  var gObjekt	= getHSObject(type,id,key);
  var gArea		= getArea();
  var flashstring = 'gJahr='+gJahr+'&gFarbe='+gFarbe+'&gObjekt='+gObjekt+'&gArea='+gArea;
  return flashstring;
}
function getJahr()
{
  if(is_activ('year'))
  {
    return $F('year');
  }else
  {
    return '';
  }
}
function getOrt(type,id,textid)
{
  switch(type)
  {
	case 'hs_short_custom':
	case 'hs_long_custom':
	if(1 == getDesign()) /*university*/
	{
	  return cleanChar($F('customtext'+textid+'_'+id));
	}else
    {
	  if(2 == textid)
	  {
	    return '';
	  }
	  return 'SpecialShirt';
	}
	break;
	case 'hs_short':
	case 'hs_long':
	default:
	if(1 == textid)
	{
	  var tmp = $F('location').split('|');
      return cleanChar(tmp[2]);
	}else
	{
	  return '';
	}
	break;
	case 'cap':
	case 'bag':
	if(1 == textid)
	{
	  var tmp = $F('location').split('|');
      return cleanChar(tmp[2]);
	}else
	{
	  return '';
	}
	break;
	case 'cap_custom':
	case 'bag_custom':
	if(2 == textid)
  {
    return '';
  }
	return 'SpecialShirt';
	break;
  }
}
function cleanChar(str)
{
  str = str.replace('ü','Ã¼');
  str = str.replace('ä','Ã¤');
  str = str.replace('ö','Ã¶');
  str = str.replace('Ü','Ã?');
  str = str.replace('à','Ã ');
  str = str.replace('é','Ã©');
  str = str.replace('è','Ã¨');
  str = str.replace('ô','Ã´');
  str = str.replace('û','Ã»');
  str = str.replace('ê','Ãª');
  str = str.replace('â','Ã¢');
  return str;
}
function isRaglanShirt()
{
  if('uni' == $F('shirttype'))
  {
    return false;
  }else
  {
    return true;
  }
}
function getHSObject(type,id,key)
{
  switch(type)
  {
    case 'hs_short':
  	case 'hs_short_custom':
  	if('Female' == $F('shirtsex_'+key))
  	{
  	  if(isRaglanShirt())
  	  {
  	    return 2;
  	  }else
  	  {
  	    return 4; /*uni*/
  	  }
  	}else
  	{
  	  if(isRaglanShirt())
  	  {
  	    return 1;
  	  }else
  	  {
  	    return 3; /*uni*/
  	  }
  	}
  	break;

  	case 'hs_long':
  	case 'hs_long_custom':
  	if('Female' == $F('shirtsex_'+key))
  	{
  	  if(isRaglanShirt())
  	  {
  	    return 9;
  	  }else
  	  {
  	    return 11; /*uni*/
  	  }
  	}else
  	{
  	  if(isRaglanShirt())
  	  {
  	    return 8;
  	  }else
  	  {
  	    return 10; /*uni*/
  	  }
  	}
  	break;
  	case 'cap':
  	case 'cap_custom':
  	return 5;
  	break;
  	case 'bag':
  	case 'bag_custom':
  	return 6;
  	break;
  	case 'sticker':
  	return 7;
  	break;
  }
}
function getFarbe()
{
  if(is_activ('color'))
  {
    switch($F('color'))
	{
	  case 'Gelb':
	  return '0xffcc00';
	  break;
	  case 'Gold':
	  return '0xae9935';
	  break;
	  case 'Weiss':
	  default:
	  return '0xffffff';
	  break;
	}
  }else
  {
    return '0xffffff';
  }
}
function getKanton(type)
{
  switch(type)
  {
	case 'hs_short_custom':
	case 'hs_long_custom':
	case 'cap_custom':
	case 'bag_custom':
	return 'special_default';
	break;
	case 'cap':
	case 'bag':
	case 'hs_short':
	case 'hs_long':
	default:
	var tmp = $F('location').split('|');
    return tmp[0];
	break;
  }
}
function getArea()
{
  if(is_activ('area'))
  {
    switch($F('area'))
	{
	  case 'CAPITAL':
	  return 1;
	  break;
	  case 'DOWNTOWN':
	  return 3;
	  break;
	  case 'EASTSIDE':
	  return 4;
	  break;
	  case 'NORTH':
	  return 5;
	  break;
	  case 'SOUTH':
	  return 6;
	  break;
	  case 'WESTSIDE':
	  return 7;
	  break;
	  case 'CITY':
	  default:
	  return 2;
	  break;
	}
  }else
  {
    return '';
  }
}

function collectShirtCustomData(type,id,key)
{
  var custom = '';

  if('cap_custom' == type || 'bag_custom' == type || 'hs_short_custom' == type || 'hs_long_custom' == type)
  {
    switch(getDesign())
  	{
  	  case 1:/*'University'*/
  	  custom  += 'Ort1: ' + $F('customtext1_'+key) + '\n';
  	  break;
  	  case 2:/*'Classic';*/
   	  custom  += 'Ort1: ' + $F('customtext1_'+key) + '\n';
  	  custom  += 'Ort2: ' + $F('customtext2_'+key) + '\n';
  	  break;
        case 3:/*'City';*/
   	  custom  += 'Ort1: ' + $F('customtext1_'+key) + '\n';
  	  custom  += 'Ort2: ' + $F('customtext2_'+key) + '\n';
  	  break;
  	}
  }else
  {
  	var tmp    = $F('location').split('|');
  	custom  += 'Shirtname: ' + tmp[2] + ' (' + $F('kanton') + ')\n';
  }

  custom += 'Style: '+getHomeshirtType()+'\n';
  if(is_activ('year'))
  {
	  custom += 'Jahr: ' + $F('year') + '\n';
  }

  if(is_activ('area'))
  {
    if(!(('cap_custom' == type || 'bag_custom' == type || 'hs_short_custom' == type || 'hs_long_custom' == type) && 3 == getDesign()))
    {
	    custom += 'Bereich: ' + $F('area') + '\n';
	  }
  }
  if(is_activ('color'))
  {
  	custom += 'Farbe: ' + $F('color') + '\n';
  }
  if('hs_short' == type || 'hs_long' == type || 'hs_short_custom' == type || 'hs_long_custom' == type)
  {
    if(isRaglanShirt())
    {
      custom += 'Shirtart: Raglan' + '\n';
    }else
    {
      custom += 'Shirtart: Uni' + '\n';
    }
    custom += 'Grösse:' + $F('shirtsize_'+key) + '\n';
    custom += 'Geschlecht:' + $F('shirtsex_'+key) + '\n';
  }
  $('shop.cart[' + key + '][custom]').value = custom;
  return true; /*normalfall*/
}
function _openPreview(id,key)
{
  var type = 'undefined'
  type = _getType(id,key);
  if(('cap' == type || 'bag' == type || 'hs_short' == type || 'hs_long' == type) && checkShirt(id,key))
  {
    openPicWin(abscorrect+'de/homeshirtpopup.html?base64params='+getBase64Param(type,id,key)+'&'+getFlashVar(type,id,key),'homeshirtImg',300,250);
  }else if(('cap_custom' == type || 'bag_custom' == type || 'hs_short_custom' == type || 'hs_long_custom' == type) && checkCustomShirt(id,key))
  {
    openPicWin(abscorrect+'de/homeshirtpopup.html?base64params='+getBase64Param(type,id,key)+'&'+getFlashVar(type,id,key),'homeshirtImg',300,250);
  }else
  {
    return false;
  }
}
