var DDSPEED = 10;
var DDTIMER = 15;

// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}

function doSearch(keyword){
	
	if(keyword.Length == 0){
		
		alert('Please enter a valid keyword');
		
	}else{
	
		window.location.href = 'advanced_search_result.php?keywords=' + keyword;
		
	}
	
	
}

function mail_sub(){
	
		errcheck = 0;
		errmsg = '';
		
		emailID = document.getElementById('mail_add').value;
		
		
		if (emailID == '' || emailID == 'Enter E-Mail here.'){
		
			errcheck = errcheck + 1; errmsg = errmsg + '\n- Invalid E-Mail Address Entered.';
			
			}else{
		
			if (echeck(emailID)==false){errcheck = errcheck + 1; errmsg = errmsg + '\n- Invalid E-Mail Address Entered.';}		
			
		}	
		
		if(errcheck > 0){
			
			alert(errmsg);	
		
		}else{

            if(confirm('Subscribe ' + emailID + '.  Are you sure?')){

				document.getElementById('subscribe').innerHTML = '<table width="469" border="0" cellspacing="0" cellpadding="0"><tr><td width="45"><img src="images/loading.gif" width="36" height="36" /><br /></td><td width="424" class="main">Subscribing <strong>' + emailID + '</strong> to Newsletter</td></tr></table>';
				AjaxSubscribe(emailID);
			
			}
		}
	
}



function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    
		    return false
		 }

 		 return true					
	}


		function AjaxSubscribe(emailID){



            strUrl = 'newsletter_subscribe.php?mail_add=' + emailID;

            //Invoke Ajax.
			if (window.XMLHttpRequest) {		
				ajaxRequest = new XMLHttpRequest(); //Not IE	
				} else if(window.ActiveXObject) {		
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); //IE	
				} else {		
				//Display your error message here. 		
				//and inform the user they might want to upgrade		
				//their browser.		
				alert ('Your browser does not support the XmlHttpRequest object.  Contact Tech Support.');	
			}
		




				ajaxRequest.onreadystatechange = function(){
					if(ajaxRequest.readyState == 4){
						document.getElementById('subscribe').innerHTML = ajaxRequest.responseText;
					}
				}

				//Set asynchronus call
				ajaxRequest.open('GET',strUrl,true);

				//Make the Call
				ajaxRequest.send(null);

			



        }



		function handleNewsletterSubscribeResponse(){

				if (ajaxRequest.readystate == 4){

					document.location.reload();

				}




		}

