// JavaScript Document
var totalFeatures = 1;
var featuredIdx = Math.floor( Math.random() * 5) + 1;
var featuredTimer;

$(document).ready(function(){
//Start JQuery Code
    if(document.getElementById('ddwtoday')) {
     totalFeatures = $("#ddwtoday ul li").length;
     var temp = $('#ddwtoday ul a').get(parseInt(featuredIdx-1)).id.split("_");
	 var pageID = temp[1];
	 var idx = temp[2];
	 getFeaturedContent(pageID,idx);
	 featuredTimer=setInterval("rotateFeaturedContent()",5000);
	}
	
	//Search popup
	$("#nav_utility li.form a").toggle(function(){
			$("#header form").fadeIn(500);								
	 },function(){
			$("#header form").fadeOut(500);											
	});	

	//Zebra Tables (content)
	if (!($("table").attr("class")) && !($("table").attr("id"))) {
		$("table tr:even").addClass("even");
		$("table td:last-child").css("border-right", "none");
		$("table th:last-child").css("border-right", "none");
		$("table").css("border-left", "none");
	}

	//Button hover
	$(".submit").hover(		
		function() {			
			off_img = $(this).attr("src");			
			over_img = off_img.replace("off/","on/");																			
			$(this).attr("src", over_img);	
			return false;
		},		
		function() {		
			$(this).attr("src", off_img);
			return false;		
		}		
	);
	
	//Primary Nav hover
	if ($("#primarynav li").not(".selected")) {	
		$("#primarynav li a img").hover(	
			function() {			
				off_img = $(this).attr("src");			
				over_img = off_img.replace("off/","on/");																			
				$(this).attr("src", over_img);		
				return false;
			},		
			function() {		
				$(this).attr("src", off_img);
				return false;		
			}
		);	
	}
	
	$("#searchHelpLink").toggle(		
		function() {			
			$("#searchHelpContainer").fadeIn('fast');
		},		
		function() {		
			$("#searchHelpContainer").fadeOut('fast');
		}		
	);
	
	$("#advancedSearchLink").toggle(		
		function() {			
			$("#advancedSearchOptionsContainer").fadeIn('fast');
		},		
		function() {		
			$("#advancedSearchOptionsContainer").fadeOut('fast');
		}		
	);
	
	if(document.getElementById('emailSignUpForm')) {
	 $("#emailSignUpForm").submit(	
			function() {			
				if(emailSignUpIsValid()) {
				 submitForm(document.getElementById('emailSignUpForm'),'/actions/_postEmailSignUp.cfm','completeEmailSignUpPost');
				 return false;
				}
			}
		);	
	}
	
	
	
	
	//DDW Today Feature

	//Initialize
	//$('#featured').load("features/feature_1.html");

	//Click Function
	$("#ddwtoday li a").click(function(){	
		var temp = $(this).attr('id').split("_");
		var pageID = temp[1];
		var idx = temp[2];
		getFeaturedContent(pageID,idx);
		
		
		///actions/_getFeaturedContent.cfm?pageID=#page_id#
									   
		/*$("#ddwtoday li").removeClass();
		if(document.getElementById('today1')) {
		 $("#today1").attr("src", $("#today1").attr("src").replace("on/","off/"));
		}
		if(document.getElementById('today2')) {
		 $("#today2").attr("src", $("#today2").attr("src").replace("on/","off/"));
		}
		if(document.getElementById('today3')) {
		 $("#today3").attr("src", $("#today3").attr("src").replace("on/","off/"));
		}
		if(document.getElementById('today4')) {
		 $("#today4").attr("src", $("#today4").attr("src").replace("on/","off/"));
		}		
		if(document.getElementById('today5')) {
		 $("#today5").attr("src", $("#today5").attr("src").replace("on/","off/"));
		}
		$(this).parent().addClass("selected");
		$(this).children().attr("src", $(this).children().attr("src").replace("off/","on/"));				
		$("#featured").load($(this).attr("href"));*/
		return false;
	});		
	
	$("#printlink a").click(function(){
	   width=670;
       height=500;								  
	   print_win=window.open('/frame-templates/print_template.cfm','PrintWMSPage','width='+width+',height='+height+', top='+((screen.availHeight/2)-(height/2))+',left='+((screen.availWidth/2)-(width/2))+',toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
       print_win.focus();					   
	   return false;
	});								   
	
	//Print function
	$("body.print a#print").click(function(){
		window.print();
		return false;	
	});	

//End JQuery Code
});

function emailPage() {
	submitForm(document.getElementById('emailPageForm'),'/wmsemail.cfm','completeEmailPagePost');
}

function completeEmailPagePost() {
 TB_remove();	
 alert("Email has been sent.");
}

function completeEmailSignUpPost() {
 document.getElementById('emailAddress').value="";	 
 var confirmationWin =  getPopUpMessage("Thank you for signing up for the DDW email updates.  This confirmation window will automatically close in 10 seconds or you can close it now by clicking the 'X' in the top right hand corner.");   
 document.getElementsByTagName('body').item(0).appendChild(confirmationWin);  
 confirmationWin.style.left = findPosX(document.getElementById('emailAddress'))-240+'px';
 confirmationWin.style.top= findPosY(document.getElementById('emailAddress'))+'px';
 window.setTimeout("closeConfirmation()",10000);
}

function emailSignUpIsValid() {
 if(isValidEmail(document.getElementById('emailAddress'))) {
  return true; 
 }
 else {
   alert("Please enter a valid email address.");
   return false;
 }
}

function print_page() {  
 width=537;
 height=500;
 print_win=window.open('/frame-templates/print_template.cfm','PrintWMSPage','width='+width+',height='+height+', top='+((screen.availHeight/2)-(height/2))+',left='+((screen.availWidth/2)-(width/2))+',toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
 print_win.focus();  	  
}	

function closeConfirmation() {
 if(document.getElementById('statuswindow')) {
  document.getElementsByTagName('body').item(0).removeChild(statuswindow,true); 
 }
}

function getPopUpMessage(msg) {
 var oDiv = document.createElement("div");
 oDiv.setAttribute("id","statuswindow"); 
 var oDiv2 = document.createElement("div");
 oDiv2.setAttribute("id","header"); 
 var oHdr = document.createElement("h2");
 oHdr.innerHTML="Sign up confirmation"; 
 var oLink = document.createElement("a"); 
 oLink.setAttribute("id","close_window");
 oLink.setAttribute("href","javascript: onclick=closeConfirmation();"); 
 var oImg = document.createElement("img");
 oImg.setAttribute("src","/frame-images/ddw_org/buttons/off/close_email.gif");
 oImg.setAttribute("alt","Close window"); 
 oImg.className='submit';
 oLink.appendChild(oImg); 
 oDiv2.appendChild(oHdr);
 oDiv2.appendChild(oLink);
 oDiv.appendChild(oDiv2);
 oDiv.innerHTML+=msg;	
 return oDiv;
}


function findPosX(obj) {
 var curleft = 0;
 if(obj.offsetParent)
  while(1) {
   curleft += obj.offsetLeft;
   if(!obj.offsetParent)
    break;
   obj = obj.offsetParent;
  }
 else if(obj.x)
  curleft += obj.x;
  return curleft;
}
  
function findPosY(obj) {
 var curtop = 0;
 if(obj.offsetParent)
  while(1) {
   curtop += obj.offsetTop;
   if(!obj.offsetParent)
    break;
   obj = obj.offsetParent;
  }
  else if(obj.y)
   curtop += obj.y;
   return curtop;
 }
 
 
 function isValidEmail(inputObj) {
 if(inputObj.value.indexOf('@') == -1 || inputObj.value.indexOf('.') == -1) {
  return false;
 }
 else {
  var tmp1 = inputObj.value.split("@");
  var tmp2 = tmp1[1].split(".");   
  if(tmp1[0].length < 1 || tmp2[0].length < 2 || tmp2[1].length < 2) {
   return false;
  }   
 }
 return true;
}

function saveToOutlook(eventID) { 
 var urlString = 'http://www.ddw.org/actions/_saveToOutlook.cfm?eventID='+eventID;
 window.open(urlString); 
}

function getFeaturedContent(pageID,idx) {
 for(var x = 1; x <= totalFeatures; x++) {	
  $("#today"+x).attr("src", $("#today"+x).attr("src").replace("on/","off/"));	 
 }
 $('#featured').load('/actions/_getFeaturedContent.cfm?pageID='+pageID);	 
 $("#today"+idx).attr("src", $("#today"+idx).attr("src").replace("off/","on/"));
 featuredIdx = idx;
}


function rotateFeaturedContent() {
 if(featuredIdx < totalFeatures) {
  featuredIdx=featuredIdx*1+1; 
 }
 else {
  featuredIdx=1; 
 } 
 var temp = $('#ddwtoday ul a').get(parseInt(featuredIdx-1)).id.split("_");
 var pageID = temp[1];
 var idx = temp[2];
 getFeaturedContent(pageID,idx);
}

// External Vendor Search Function
function RunSearch()
        {
               var     keyword = document.getElementById("SearchBox");
               if(keyword != null && keyword.value.length == 0)
               {
                       alert("Please enter the keyword.");
                       keyword.focus();
               }
               else
               {
			   window.open("http://myddw2010.bdmetrics.com/searchresults.aspx?configid=1&keyword=" + encodeURIComponent(keyword.value));
                       //window.location ="http://myddw2010.bdmetrics.com/searchresults.aspx?configid=1&keyword=" + encodeURIComponent(keyword.value);

               }
        }

        function clickButton(e){
               var bt = document.getElementById('SearchButton'); 
               if (typeof bt == 'object'){ 
                              if(navigator.appName.indexOf('Netscape')>(-1)){ 
                                      if (e.keyCode == 13){ 
                                                     bt.click(); 
                                                     return false; 
                                      } 
                              } 
                               if (navigator.appName.indexOf('Microsoft Internet Explorer')>(-1)){ 
                                      if (event.keyCode == 13){ 
                                                     bt.click(); 
                                                     return false; 
                                      } 
                              } 
               } 
        }
// End External Vendor Search Function		