﻿  function redirectIfNecessary2() {

   try {
    fullUrl = document.location.href;
	
    splittedByQmArray = fullUrl.split('?');
	
	
	
    // query after ?
    sQuery = splittedByQmArray[1];
    sQuery = sQuery.toLowerCase();
    sQuery = sQuery.replace(/ /g, '');

    // query after s=
    pQuery = sQuery.slice(2,sQuery.length);
	
	//decoding the url to decode the characters encoded during HRU redirection
	pQuery = decodeURIComponent(pQuery);
	
    // e.g. ck-15w&d=compatibility
    regExpForAccComp = /^[a-z0-9-]*&d=compatibility$/;
    regExpForAccCompMatch = pQuery.search(regExpForAccComp);
	
    // e.g. 6500classic,bh-800
    regExpForPhoneAcc = /^[ne]?[0-9]{1,4}[a-z]*[,]{1}[a-z0-9-]*$/;
    regExpForPhoneAccMatch = pQuery.search(regExpForPhoneAcc);
	

    // e.g. e61
    regExpForPhone = /^[nex]?[0-9]{1,4}[a-z]*|n-gage|n-gageqd|booklet3g$/;
    regExpForPhoneMatch = pQuery.search(regExpForPhone);
	
    // special accessories that are otherwise classified to phones with regExpForPhone 
    // e.g. 810carphone or 330AutoNavigation
    regExpForSpecialAcc = /^[0-9]{3}[a-z]+|n800case|^[\D]*616$/;
    regExpForSpecialAccMatch = pQuery.search(regExpForSpecialAcc);
	
	

    if(regExpForAccCompMatch != -1) {
	// remove &d=compatibility from string end
	pCode = pQuery.slice(0,pQuery.length-16);
     	newUrl = '/' + pCode + '/compatibility';
    }
    else if(regExpForPhoneAccMatch != -1) {
    	pCodeArray = pQuery.split(',');
    	pCode = pCodeArray[1];
     	newUrl = '/' + pCode;
    }

    // phone and not special case accesory
    else if(regExpForPhoneMatch != -1 && regExpForSpecialAccMatch == -1) {

    // Use Accessory phone data array that should be on same page 
    phoneCount = phoneProductArray.length; 
    allAccessoriesPage = "all_accessories";
    
	
	
    // Loop through phoneProductArray
    for(i=0; i<phoneCount; i++) 
    {
      phoneName = phoneProductArray[i][0];
      phoneId = phoneProductArray[i][2];
      splittedPhoneName = phoneName.split('Nokia ');
      phoneCode = splittedPhoneName[1];
      phoneCode = phoneCode.replace(/ /g, '');
      phoneCode = phoneCode.toLowerCase();
		
    
      if(phoneCode == pQuery ) {
        showAllAccessoryUrl = '/' + allAccessoriesPage + '?phoneid=' + phoneId;
        break;
      }
    
    }
    
     newUrl = showAllAccessoryUrl;
			

    }
    else {
    	//regExpForAcc = /^[a-z]{1,4}-[0-9]{1,3}[a-z]?|[a-z]+|[a-z]?[0-9]{3}[a-z]+$/;
    	regExpForAcc = /^[a-z0-9-]+$/;
    	regExpForAccMatch = pQuery.search(regExpForAcc);
	if(regExpForAccMatch != -1) {
	  pCode = pQuery.match(regExpForAcc);
     	  newUrl = '/' + pCode;
	}

    }
	
    document.location.href = newUrl;






    } catch(ex) {
     // ignore
    }
  }
 

 window.onload = redirectIfNecessary2;




