trueWindowOpen = window.open;
window.open = proxyWindowOpen;
function proxyWindowOpen(url, type, attributes){
    //Add logic to check if the URL	being opened i.e. param1 is download, if yes trigger download recording

	var parentId= document.getElementById("pageNodeId")?document.getElementById("pageNodeId").value : "";
	var parentPageType= document.getElementById("pageType")?document.getElementById("pageType").value : "";
	var commandID= "DOWNLOAD" ;
	
	var unreg_dl = dcsCheckDownload(url);

	var reg_dl = url.indexOf("webapp/Download");
	var mod_dl = url.indexOf("download/mod_download.jsp");
    var lic_dl = url.indexOf("download/license.jsp");	

	if (reg_dl != -1 || mod_dl != -1 || lic_dl != -1 ){
		url = url + '&Parent_nodeId='+ parentId +'&Parent_pageType='+parentPageType;
	}

	if(unreg_dl){
		recordDwnldHistory(parentId,parentPageType,url,commandID);		
	}

    //invoke the real window.open
    return trueWindowOpen(url, type, attributes);
}

var e=(navigator.appVersion.indexOf("MSIE")!=-1)?"click":"mousedown";
eventClickBind(e,"downloadClicked");

// added by r1051z on 2/20/2009 for History proeject Start
// captured event if user right clicked and downloaded collateral
eventClickBind("contextmenu","downloadRightClicked");
// added by r1051z on 2/20/2009 for History proeject End

function eventClickBind(event,func){
	 if ((typeof(window[func])=="function")&&document.body){
		 if (document.body.addEventListener){
		 document.body.addEventListener(event, window[func], true);
		   }
		 else if(document.body.attachEvent){
		document.body.attachEvent("on"+event, window[func]);
		   }
	 }
}

// it matches if the link clicked
// have the list of files extensions
function fileTypeMatch(path, typelist){	
	 var type=path.substring(path.lastIndexOf(".")+1,path.length);
	 var types=typelist.split(","); 
	 var lower_type = type.toLowerCase();
	 for (var i=0;i<types.length;i++){
	  if (lower_type==types[i]){	
	     return true;
	   }
	 }
	 return false;
}


function anchorEvt(evt,tag){
	 var e=evt.target||evt.srcElement;
	 while (e.tagName&&(e.tagName!=tag)){
	   e=e.parentElement||e.parentNode;
	 }
	 return e;
}
function dcsCheckDownload(pth){		
  if(pth.indexOf("/")!=-1){	
	  var link_path = new Array();
      link_path = pth.split('/'); 
      if(link_path[1].toUpperCase().indexOf("FILES")!=-1){		
	  return true;					         
	}	
 }
 return false;
}

//This function checks that the hostname of the link
//matches the list of freescale hostnames 
function IsOnsite(hostname){
	 var doms="www.freescale.com";
	 var doms="@@ONSITEDOMAINS@@";
	 var aDoms=doms.split(',');
	 for (var i=0;i<aDoms.length;i++){ 
	   if (aDoms[i].indexOf(hostname)!=-1){
	     return 1;
	    }
	 }
	 return 1;           //change it ro return 0 
}

function recordWebpageHistory(pageId, pageType,commandID){	
 Request.sendPOST('/webapp/history/recordHistory.sp','assetID='+pageId+'&assetType='+pageType+'&commandID='+commandID, checkSuccessHistory);
 }
 function checkSuccessHistory(){	
 }
/** Add start by B17090 on 14-Oct-2009 For CCT68841  **/
 function recordVideoHistory(video_url,commandID){	
  Request.sendPOST('/webapp/history/recordHistory.sp','assetID='+video_url +'&assetType=video&commandID='+commandID, checkSuccessHistory);
 }
/** Add END by B17090 on 14-Oct-2009 For CCT68841  **/
 function recordDwnldHistory(parentId,parentPageType,download_url,commandID,historyPopup){
    Request.sendPOST('/webapp/history/recordHistory.sp','downloadLink='+download_url +'&parentID='+parentId +'&parentType='+parentPageType+'&commandID='+commandID+'&assetType=download'+'&fromHistoryPopUp='+historyPopup, checkSuccessHistory);
 }

function dcsEvt(evt,tag){
	var e=evt.target||evt.srcElement;
	while (e.tagName&&(e.tagName!=tag)){
		e=e.parentElement||e.parentNode;
	}
	return e;
}

//This function executes onClick of Anchor Tag
// it checks whether download has been clecked or not
function downloadClicked(evt){		
   evt=evt||(window.event||"");
   if (evt&&((typeof(evt.which)!="number")||(evt.which==1))){		
      var e=anchorEvt(evt,"A");	
	 if(e.hostname && IsOnsite(e.hostname)){	
	  //var types="xls,doc,ppt,tgz,rar,tar,pdf,txt,csv,zip,exe";    
      var reg_dl = e.pathname.indexOf("webapp/Download");
	  var faq_click = e.pathname.indexOf("TransformXMLServlet");
	  var unreg_dl = e.pathname.indexOf("webapp/files");
	  var mod_dl = e.pathname.indexOf("download/mod_download.jsp");
      var lic_dl = e.pathname.indexOf("download/license.jsp");	
	  var page_nodeId = document.getElementById("pageNodeId")?document.getElementById("pageNodeId").value : "";
	  var page_typeName= document.getElementById("pageType")?document.getElementById("pageType").value : "";
	  //var commandId = document.getElementById("recrdHistDwnlodComnd")?document.getElementById("recrdHistDwnlodComnd").value : "";
	  var commandId = "DOWNLOAD" ;
	  var pth=e.pathname?((e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname):"/";
	  var qry=e.search?e.search.substring(e.search.indexOf("?")+1,e.search.length):"";
	  var is_unsecure_download = dcsCheckDownload(pth);
	  //var typeMatched = dcsTypeMatch(e.pathname,types);
	     if (is_unsecure_download ||(reg_dl != -1)||(lic_dl != -1) || (mod_dl != -1) ){				 
			   if (reg_dl != -1){			
					 e.search= e.search +'&Parent_nodeId='+ page_nodeId +'&Parent_pageType='+page_typeName;
				   }			   
			   else if (lic_dl != -1){
			         e.search= e.search +'&Parent_nodeId='+ page_nodeId +'&Parent_pageType='+page_typeName;					
				  }
			   else if (mod_dl != -1){
			         e.search= e.search +'&Parent_nodeId='+ page_nodeId +'&Parent_pageType='+page_typeName;					
				  }
			   else if(is_unsecure_download){
						var history_popup = 0;
						refer = anchorEvt(evt ,"DIV");
						if(refer != null || refer != "undefined"){
							if (refer.nodeType == 1){
								if ((String(refer.nodeName)=="DIV") && (String(refer.id) == "DOWNLOAD")){
									while (!history_popup){
										refer = refer.parentNode;
										if (refer.nodeType==1){ //check that the node is a tag, not text (type=3)
											if ((String(refer.nodeName)=="DIV") && (String(refer.id) == "historyajaxbox")){
												history_popup = 1;
												break;
											} // end of div and historyajaxbox check
											else if (String(refer.nodeName)=="BODY") {
												break;	
											} //end of else if
										} // end of nodeType = 1 check
									} // end of while loop*/
								}
							}
						}
						if(history_popup){
							recordDwnldHistory(page_nodeId,page_typeName,pth,commandId,history_popup);
						}else{
							recordDwnldHistory(page_nodeId,page_typeName,pth,commandId);
						}	
				   }				
				}			 
		   }
		}
 }

// added by r1051z on 2/20/2009 for History proeject Start
// captured event if user right clicked and downloaded collateral
//This function executes onRightClick of Anchor Tag
// it checks whether download has been clicked or not by right clicking of mouse
function downloadRightClicked(evt){
	evt=evt||(window.event||"");
	 if (evt){
		 var btn=evt.which||evt.button;
		 if ((btn!=1)||(navigator.userAgent.indexOf("Safari")!=-1)){
			 // this function will return anchor tag if clicked on any other element, will traverse backwards
			 e = anchorEvt(evt,"A");
				 if ((typeof(e.href)!="undefined")&&e.href){
					 if ((typeof(e.protocol)!="undefined")&&e.protocol&&(e.protocol.indexOf("http")!=-1)){
						 if(e.hostname && IsOnsite(e.hostname)){
							  //var types="xls,doc,ppt,tgz,rar,tar,pdf,txt,csv,zip,exe";    
							  var reg_dl = e.pathname.indexOf("webapp/Download");
							  var faq_click = e.pathname.indexOf("TransformXMLServlet");
							  var unreg_dl = e.pathname.indexOf("webapp/files");
							  var mod_dl = e.pathname.indexOf("download/mod_download.jsp");
							  var lic_dl = e.pathname.indexOf("download/license.jsp");	
							  var page_nodeId = document.getElementById("pageNodeId")?document.getElementById("pageNodeId").value : "";
							  var page_typeName= document.getElementById("pageType")?document.getElementById("pageType").value : "";
							  //var commandId = document.getElementById("recrdHistDwnlodComnd")?document.getElementById("recrdHistDwnlodComnd").value : "";
							  var commandId = "DOWNLOAD" ;
							  var pth=e.pathname?((e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname):"/";
							  var qry=e.search?e.search.substring(e.search.indexOf("?")+1,e.search.length):"";
							  var is_unsecure_download = dcsCheckDownload(pth);
							  //var typeMatched = dcsTypeMatch(e.pathname,types);
								 if (is_unsecure_download ||(reg_dl != -1)||(lic_dl != -1) || (mod_dl != -1) ){				 
									   if (reg_dl != -1){			
											 e.search= e.search +'&Parent_nodeId='+ page_nodeId +'&Parent_pageType='+page_typeName;
										   }			   
									   else if (lic_dl != -1){
											 e.search= e.search +'&Parent_nodeId='+ page_nodeId +'&Parent_pageType='+page_typeName;					
										  }
									   else if (mod_dl != -1){
											 e.search= e.search +'&Parent_nodeId='+ page_nodeId +'&Parent_pageType='+page_typeName;					
										  }
									   else if(is_unsecure_download){				     
											var history_popup = 0;
											refer = anchorEvt(evt ,"DIV");
											if(refer != null || refer != "undefined"){
												if (refer.nodeType == 1){
													if ((String(refer.nodeName)=="DIV") && (String(refer.id) == "DOWNLOAD")){
														while (!history_popup){
															refer = refer.parentNode;
															if (refer.nodeType==1){ //check that the node is a tag, not text (type=3)
																if ((String(refer.nodeName)=="DIV") && (String(refer.id) == "historyajaxbox")){
																	history_popup = 1;
																	break;
																} // end of div and historyajaxbox check
																else if (String(refer.nodeName)=="BODY") {
																	break;	
																} //end of else if
															} // end of nodeType = 1 check
														} // end of while loop*/
													}
												}
											}
											if(history_popup){
												recordDwnldHistory(page_nodeId,page_typeName,pth,commandId,history_popup);
											}else{
												recordDwnldHistory(page_nodeId,page_typeName,pth,commandId);
											}				
										 }				
									}	
										
							}
					 }
				 }
		 }			 
	 }
 } // end of function
// added by r1051z on 2/20/2009 for History proeject end


 

 // This function will be called from flash version of mainserp, when datasheet is clicked. 
 function recordDwnldHistoryFromFlash(path){
	var index  = path.toUpperCase().indexOf("/FILES");
	if (index != -1 )
	{
		var linkurl  = path.substring(index);
		var page_nodeId = document.getElementById("pageNodeId")?document.getElementById("pageNodeId").value : "";
		var page_typeName= document.getElementById("pageType")?document.getElementById("pageType").value : "";
		var commandId =  "DOWNLOAD" ;
		recordDwnldHistory(page_nodeId,page_typeName,linkurl,commandId);
	}
 }

 function recordTrngHistory(training_id,page_Type,commandID){
 Request.sendPOST('/webapp/history/recordHistory.sp','assetID='+training_id +'&assetType='+page_Type+'&commandID='+commandID, checkSuccessHistory);
 }