/* Configurable attributes - Business */
/* ALL THESE RULES ARE COOKIE BASED.
If the user visits the page secondtime, after clearing the cookies or 
from a different machine, all these rules will not apply*/

// No. of days for 'No Survey Form' if the user responds successfully to the survey 
NoSurveyFormResponded=10000;

// No. of days for 'No Survey Form' if the user is not interested in submitting the survey
NoSurveyFormNotInterested=180;

// No. of days for the last page hit would be remembered.
// i.e. if user visits the once, then for these many days his page visit would be remembered 
RememberHitCount=10000;

// Web URL patterns for enabling the Survey Form
var arrWebPagePatterns=["/web/JP/news/pr/index.html", "/web/JP/news/pr/index_US.html", "/web/JP/solution/", "/web/JP/product/", "/web/JP/ordering/", "/web/JP/support/nav/", "/web/JP/support/", "/web/JP/event/", "/web/JP/partners/index.html", "/web/JP/reseller/program/", "/web/JP/services/index.html", "/japanese/warp/public/3/jp/service/manual_j/", "/web/JP/news/cisco_news_letter/ccb/", "/web/JP/about/", "/web/JP/news/hr/"];
//var arrWebPagePatterns=["/Cisco.com-jp Customer Satisfaction Survey/My Work/"];

// The page-hit configuration for showing the Survey Form, for the above URL patterns
/* The hit-count value position for the each URL pattern should be the same
e.g. 
var arrWebPagePatterns=["/web/JP/solutions/FY2010","/web/JP/solutions"];
var arrShowSurveyPageVisit=[ <<hit-count config for "/web/JP/solutions/FY2010">> , <<hit-count config for "/web/JP/solutions">>]; */
var arrShowSurveyPageVisit=[2, 2, 10, 10, 2, 2, 10, 10, 10, 5, 3, 10, 4, 4, 4];
//var arrShowSurveyPageVisit=[1]; 

// The Survey Form HTML Path
//var surveyFormHTML = "http://www.cisco.com/web/JP/assets/user_survey/form.html?parentURL="+ location.href;
var surveyFormHTML = "/web/JP/assets/user_survey/form.html?parentURL="+ location.href +"&hostName="+location.host;

/* Configurable attributes - Techincal */
divSurveyName="JapanSatisfactionSurvey";
cookieNoSurvey="NoJPSatisfactionSurveyFrm";
cookieNoSurveyResponded="NoJPSatisfactionSurveyFrmResponded";
cookiePrefix="jpSurvey";  

function JPFY10_checkAndShowJapanSurveyForm() {
    window.onload = function() {};

var m = document.getElementsByTagName("META");
    var title;
    var metaName;
    for(var i in m){
      metaName = m[i].name;
      if(metaName){
        metaName = metaName.toLowerCase();
        if(metaName == "title"){
          title = m[i].content;
          title = title.toLowerCase();
          if(title == "not found")
            return;
        }
      }
    }

    JPFY10_setCISCOJapanPageHitCookies();    
    if(JPFY10_checkRulesForSurveyForm())
      setTimeout("JPFY10_createJapanSurveyForm()", 3000);
}                         
function JPFY10_checkRulesForSurveyForm() {
    var showForm = true;
    showForm = showForm && JPFY10_checkCookieRules();   
    showForm = showForm && JPFY10_checkPageURLPattern();    
    return showForm;
}
function JPFY10_createJapanSurveyForm() {
    //create a div element for the popup
    var surveyDiv = document.createElement("div");
    surveyDiv.id = divSurveyName;   
        
    //The zIndex should be higher enough so that the div is on top of all the other 
    //components defined in the page
    surveyDiv.style.zIndex     = '2000';    
    
    //The popup contents are defined here    
    surveyDiv.innerHTML = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td><img src=\"/web/JP/assets/user_survey/top.gif\" name=\"closeImage\" alt=\"\" usemap=\"#top_Map\"></td></tr><tr><td style=\"background-image: url(/web/JP/assets/user_survey/content_bg.gif)\"><iframe align=\"center\" id='srvFormFrame' frameborder=\"0\" scrolling=\"no\" src='"+surveyFormHTML+"'></iframe></td></tr><tr><td><img src=\"/web/JP/assets/user_survey/buttom.gif\" name=\"bottomImage\" alt=\"\"></td></tr></table><map name=\"top_Map\"><area shape=\"rect\" alt=\"???\" coords=\"555,10,609,31\" href=\"javascript:JPFY10_setJapanSurveyCookieDeclined()\"></map>";      
    
    var windowTop = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
    var divTop = windowTop ? (windowTop+25) : 25;
    surveyDiv.style.top = divTop+ 'px';
    
    surveyDiv.style.visibility = 'visible';
    
    //Add the div element to the html body
    document.body.appendChild(surveyDiv);
    
   	 //The below code is for dynamically centering the div position
     // call once to center the div
    /*JPFY10_OnWindowResize();	
    if (window.attachEvent)
		  window.attachEvent('onresize', JPFY10_OnWindowResize);
    else if (window.addEventListener)
		  window.addEventListener('resize', JPFY10_OnWindowResize, false);
    else
		  window.onresize = JPFY10_OnWindowResize;	
    // we won't bother with using javascript in CSS to take care
    //   keeping the window centered
    if (document.all)
		  document.documentElement.onscroll = JPFY10_OnWindowResize;*/       
}
function JPFY10_setJapanSurveyCookieDeclined() {
    JPFY10_createCookie(cookieNoSurvey, 1, NoSurveyFormNotInterested);
    JPFY10_hideJapanSurveyForm();
}         
function JPFY10_setJapanSurveyCookieResponded() {
    JPFY10_createCookie(cookieNoSurveyResponded, 1, NoSurveyFormResponded);
}
function JPFY10_hideJapanSurveyForm() {
    JPFY10_getElement(divSurveyName).style.visibility = 'hidden';
    JPFY10_getElement("srvFormFrame").style.visibility = 'hidden';
    
    //The below code is for dynamically centering the div position
    /*if (window.detachEvent)
		  window.detachEvent('onresize', JPFY10_OnWindowResize);
    else if (window.removeEventListener)
		  window.removeEventListener('resize', JPFY10_OnWindowResize, false);
    else
		  window.onresize = null;*/
}
function JPFY10_checkPageURLPattern() {
  var strUrl = String(decodeURI(location.href));
  var longPatternLength = 0;
  var thisURLPatternLength = 0;
  for(var i in arrWebPagePatterns){
    if(strUrl.indexOf(arrWebPagePatterns[i])>0) {
      thisURLPatternLength = arrWebPagePatterns[i].length;
      if(thisURLPatternLength>longPatternLength)
        longPatternLength = thisURLPatternLength;
      //alert(readCookie(arrWebPagePatterns[i]));
      if(JPFY10_readCookie(arrWebPagePatterns[i])>=arrShowSurveyPageVisit[i]) {
        if(longPatternLength == thisURLPatternLength)
          return true;
      }
    }  
  }
	return false;
}
function JPFY10_checkCookieRules() {
  if(JPFY10_isCookieEnabled()) {
    if(!JPFY10_readCookie(cookieNoSurvey) && !JPFY10_readCookie(cookieNoSurveyResponded))
      return true;
  }
	return false;
}
function JPFY10_setCISCOJapanPageHitCookies() {
  var strUrl = String(decodeURI(location.href));
  for(var i in arrWebPagePatterns){
    if(strUrl.indexOf(arrWebPagePatterns[i])>0) {
      //alert(readCookie(arrWebPagePatterns[i]));
      var pageHitCount = parseInt(JPFY10_readCookie(arrWebPagePatterns[i]));
      if(JPFY10_readCookie(arrWebPagePatterns[i]) == null)
        pageHitCount=0;
      JPFY10_createCookie(arrWebPagePatterns[i],(pageHitCount+1),RememberHitCount);
    }      
  }
}
/* utiltiy functions */
function JPFY10_getElement(id) {
	return document.getElementById(id);
}
function JPFY10_GetWindowWidth() {
	var width =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
		0;      		
	return width;
}           
function JPFY10_GetWindowHeight() {
    var height =
		document.documentElement && document.documentElement.clientHeight ||
		document.body && document.body.clientHeight ||
  		document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
  		0;  		
  	return height;
}
function JPFY10_OnWindowResize() {
	var left = 0;
	var top = 0;
	var div = JPFY10_getElement(divSurveyName);
	
	div.style.left = Math.max((left + (JPFY10_GetWindowWidth() - div.offsetWidth) / 2), 0) + 'px';
	div.style.top = Math.max((top + (JPFY10_GetWindowHeight() - div.offsetHeight) / 2), 0) + 'px';
}
function JPFY10_createCookie(name,value,days) {
  name = cookiePrefix+name;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/; domain=cisco.com";
}
function JPFY10_readCookie(name) {
  name = cookiePrefix+name;
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function JPFY10_eraseCookie(name) {
	JPFY10_createCookie(name,"",-1);
}                    
function JPFY10_isCookieEnabled() {
  JPFY10_createCookie("DummyCookieForTest","Nothing",1)
 /* check for a cookie */
  if (document.cookie == "") {
    return false;  
  } else {
    JPFY10_eraseCookie("DummyCookieForTest");
    return true;
  }
}

function JPFY10_addPageLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}   

// Function call that triggers the biz rules/the popup. Triggered on the page load if the js file is included.
JPFY10_addPageLoadEvent(JPFY10_checkAndShowJapanSurveyForm);
