var media = Array();
var site = Array();

function showcities(state, city){
	var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
      	 document.getElementById('cities').innerHTML = xmlHttp.responseText;
      }
    }
    if(city == "undefined") city = "";
    url = "ajax/aj_get_cities.php?state="+state+"&city="+city;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);	
}

function showMedia(media, cat){
	document.getElementById('xspf').innerHTML = "";
	document.getElementById('gallerywrapper').innerHTML = "";
	document.getElementById('media_content_wrapper').innerHTML = "";
	var exstr = "";	
	var id = "";
	if(media == 'radio' || media == 'document'){
		var type = document.getElementById('mediatype').value;
		if(document.getElementById(type+'_media_associations')){
			var children = document.getElementById(type+'_media_associations').childNodes;
			//alert(document.getElementById(type+'_associations').innerHTML);
			if(children.length){
				for(var i=0; i<children.length; i++){
					if(children[i].childNodes[3])
						exstr += children[i].childNodes[3].value+",";
				}
			}
		}
		id = 'media_content_wrapper';
  	}else{
  		id = 'gallerywrapper';
  	}
	var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
      	 document.getElementById(id).innerHTML = xmlHttp.responseText;
      }
    }
    url = "ajax/aj_get_media.php?type="+media+'&excl='+exstr+'&postcat='+cat;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);	
}

function showContent(cat){
	if(cat==0){
		document.getElementById('media_content_wrapper').innerHTML = "";
		return;
	}
	var exstr = "";	
	var type = document.getElementById('mediatype').value;
	
	if(document.getElementById(type+'_media_associations')){
		var children = document.getElementById(type+'_media_associations').childNodes;
		//alert(document.getElementById(type+'_associations').innerHTML);
		if(children.length){
			for(var i=0; i<children.length; i++){
				if(children[i].childNodes[3])
					exstr += children[i].childNodes[3].value+",";
			}
		}
	}
	var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
      	 document.getElementById('media_content_wrapper').innerHTML = xmlHttp.responseText;
      	 if(type == 'photo'){
      	 	$("#innerwrapper a").fancybox({			
				hideOnContentClick: false,		
				zoomSpeedIn:	400, 
				zoomSpeedOut:	400, 
				overlayShow:	true
			});
      	 }
      }
    }
    url = "ajax/aj_get_media.php?type="+type+"&cat="+cat+"&excl="+exstr;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);	
}

function showTitles(type){
	var exstr = "";
	if(document.getElementById(type+'_associations')){
		var children = document.getElementById(type+'_associations').childNodes;
		//alert(document.getElementById(type+'_associations').innerHTML);
		if(children.length){
			for(var i=0; i<children.length; i++){
				if(children[i].childNodes[3]){
					if(children[i].childNodes[3].value)
						exstr += children[i].childNodes[3].value+",";
				}
			}
		}
	}
	var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
      		var f = type.charAt(0).toUpperCase();
    		var name = f + type.substr(1);
    	 if(type==0) name = "";
      	 document.getElementById('headername').innerHTML = name;
      	 document.getElementById('contentwrapper').innerHTML = xmlHttp.responseText;
      }
    }
    url = "ajax/aj_get_sitecontent.php?type="+type+"&excl="+exstr;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);	
}

function addSiteAssociation(id, name, type){
	site[id] = document.getElementById('title'+id);
	if(!document.getElementById(type+'_associations'))
		document.getElementById('site_associations').innerHTML += "<div id='"+type+"_associations' style='padding:3px; margin:3px;'><div style='font-weight:bold; color:#025AA4;'>"+(type.substr(0,1)).toUpperCase()+type.substring(1)+"</div></div>";
  if(!document.getElementById('site'+id)){
		document.getElementById(type+'_associations').innerHTML += "<div id='site"+id+"' style='visibility:hidden; margin-left:10px; margin-bottom:3px;'><div style='float:left;'>"+name+"</div><div style='float:right; cursor:pointer; width:61px; height:15px; background-image:url(\"images/remove_section_item.gif\");' onclick='removeSiteAssociation(\""+id+"\", \""+name+"\", \""+type+"\");' ></div><div style='clear:both;'></div><input type='hidden' id='input"+id+"' name='site["+type+"][]' value='"+id+"' /></div>";
		$('#title'+id).effect("transfer", { to: "#site"+id }, 700);	
		setTimeout("document.getElementById('site"+id+"').style.visibility = ''", 600);
  }
	document.getElementById('contentwrapper').removeChild(document.getElementById('title'+id));
}
function removeSiteAssociation(id, name, type){
	var curval = document.getElementById('sitetype').value;
	if(curval == type && !document.getElementById('title'+id)){
		site[id].style.visibility = 'hidden';
		document.getElementById('contentwrapper').appendChild(site[id]);
		$('#site'+id).effect("transfer", { to: "#title"+id }, 700);	
		setTimeout("document.getElementById('title"+id+"').style.visibility = ''", 600);
	}
	document.getElementById(type+'_associations').removeChild(document.getElementById('site'+id));
	if(document.getElementById(type+'_associations').childNodes.length == 1){
		document.getElementById('site_associations').removeChild(document.getElementById(type+'_associations'));
	}
}

function addMediaAssociation(id, name, type){
  var decodedname = decodeURI(name).replace('+', " ", "g"); 
  var gallery = '';
  media[id] = document.getElementById('mediabox'+id);
    
  if(!document.getElementById(type+'_media_associations'))
	document.getElementById('media_associations').innerHTML += "<div id='"+type+"_media_associations' style='padding:3px; margin:3px;'><div style='font-weight:bold; color:#234802;'>"+(type.substr(0,1)).toUpperCase()+type.substring(1)+"</div></div>";

  if(!document.getElementById('media'+id)){
  	if(document.getElementById('gallery')) gallery = document.getElementById('gallery').value;
	document.getElementById(type+'_media_associations').innerHTML += "<div id='media"+id+"' style='visibility:hidden; margin-left:10px; margin-bottom:3px;'><div style='float:left;'>"+decodedname+"</div><div style='float:right; cursor:pointer; width:61px; height:15px; background-image:url(\"images/remove_media_item.gif\");' onclick='removeMediaAssociation(\""+id+"\", \""+name+"\", \""+type+"\", \""+gallery+"\");'></div><div style='clear:both;'></div><input type='hidden' name='media["+type+"][]' value='"+id+"' /></div>";
	$('#mediabox'+id).effect("transfer", { to: "#media"+id }, 700);
	setTimeout("document.getElementById('media"+id+"').style.visibility = ''", 600);
  }if(document.getElementById('innerwrapper'))
	document.getElementById('innerwrapper').removeChild(document.getElementById('mediabox'+id));
  else document.getElementById('media_content_wrapper').removeChild(document.getElementById('mediabox'+id));
}

function removeMediaAssociation(id, name, type, cat){
	var curval = document.getElementById('mediatype').value;
	if(curval == type ){
		if(document.getElementById('gallery') && document.getElementById('gallery').value == cat || !cat){
			media[id].style.visibility = 'hidden';
			document.getElementById('innerwrapper').appendChild(media[id]);
			$('#media'+id).effect("transfer", { to: "#mediabox"+id }, 700);
			setTimeout("document.getElementById('mediabox"+id+"').style.visibility = ''", 600);
		}
	}
	document.getElementById(type+'_media_associations').removeChild(document.getElementById('media'+id));
	if(document.getElementById(type+'_media_associations').childNodes.length == 1){
		document.getElementById('media_associations').removeChild(document.getElementById(type+'_media_associations'));
	}
}

function viewAssociations(objectId, type){
	if(type == "undefined" || !type) type = "";
	var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
      	 var ScrollTop = document.body.scrollTop;
		 if(!ScrollTop){
			 if (window.pageYOffset)
		        ScrollTop = window.pageYOffset;
		    else
	    	    ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		 }
		 document.getElementById('viewAssociations').style.top = 100+ScrollTop+'px';
      	 document.getElementById('viewAssociations').innerHTML = xmlHttp.responseText;
      	 $(".images a").fancybox({			
				hideOnContentClick: false,		
				zoomSpeedIn:	400, 
				zoomSpeedOut:	400, 
				overlayShow:	true
			});
      }
    }
    url = "ajax/aj_get_associations.php?objectid="+objectId+"&type="+type;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);	
}

function removeAssociation(objsid, medsid, type, id){
	if(!id || id=='undefined') id= medsid;
	if(!type || type=='undefined') type = "";
	if(confirm("Are you sure you want to remove this association?")){
		var xmlHttp = GetXmlHttpObject();
	    xmlHttp.onreadystatechange=function(){
	      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
	      	 document.getElementById('associationwrapper').removeChild(document.getElementById('associationbox'+id));
	      }
	    }
	    url = "ajax/aj_remove_associations.php?objsid="+objsid+"&medsid="+medsid+"&type="+type;
	    xmlHttp.open("GET",url,true);
	    xmlHttp.send(null);	
	}
}

function changeZorder(objsid, medsid, type, change){
	var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
      	 viewAssociations(objsid, type);
      }
    }
    url = "ajax/aj_change_zorder.php?objsid="+objsid+"&medsid="+medsid+"&type="+type+change;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);	
}

function makeFeatured(checked, sid){
	var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
      	 //document.getElementById('albumphoto').innerHTML = "<img src='imageresize.php?imagepath=photos/"+xmlHttp.responseText+"&amp;width=100&amp;height=100' />";
      }
    }
    url = "ajax/aj_set_featured.php?sid="+sid+"&checked="+checked;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function makeTitleImage(sid, galsid){
	var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
      	if(xmlHttp.responseText)
      	 document.getElementById('albumphoto').innerHTML = "<img src='imageresize.php?imagepath=photos/"+xmlHttp.responseText+"&amp;width=100&amp;height=100' />";
      }
    }
    url = "ajax/aj_set_albumimage.php?sid="+sid+"&galsid="+galsid;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

var selectedGal = 0;

function getGallery(sid){
	var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){      	
      	if(xmlHttp.responseText)
      	 document.getElementById('media').innerHTML = xmlHttp.responseText;
      	 document.getElementById('media').style.background = 'url(images/bgsub_newcont.jpg) repeat-x';
      	 	 $("#innerwrapper a").fancybox({			
				hideOnContentClick: false,		
				zoomSpeedIn:	400, 
				zoomSpeedOut:	400, 
				overlayShow:	true
			});
			if(selectedGal != 0){
			   document.getElementById('gal'+selectedGal).style.backgroundColor = '#ffffff';	
			   document.getElementById('gal'+selectedGal).style.color = '#727272';	
			}
			selectedGal = sid;
			document.getElementById('gal'+sid).style.backgroundColor = '#0067b1';	
			document.getElementById('gal'+sid).style.color = '#ffffff';	
      }
    }
    url = "ajax/aj_get_albumimages.php?sid="+sid+"&type=photo";
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function comment(repsid, sid){
	/*var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
      	if(xmlHttp.responseText){
 			positionPopup();
      	  document.getElementById('popup').innerHTML = xmlHttp.responseText;
	      document.getElementById('popup').style.visibility = 'visible';
      	}
      }
    }
    url = "ajax/aj_add_comment.php?repsid="+repsid+"&sid="+sid;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);*/
    
    $("#popupform_backdiv").css({'display':'block'});
	$("#popup_form").css({'display':'block', 'width':'600px', 'margin-left':'-300px', 'height':'350px'});
	$.get("ajax/aj_add_comment.php", { 
		repsid: repsid, sid:sid
		},function(data){
			/*alert(data);*/
			$("#popup_form").html(data);
		});

	$("#popupform_backdiv").click(function(){
		$("#popupform_backdiv").fadeOut('1000');
		$("#popup_form").fadeOut('1000');
	});
	scroll(0,0);
    
}

function positionPopup(){
	$('#back_div').attr('class','back_div');		  
		 $('#popup').attr('class','pop_up_div');
		 document.getElementById('popup').style.visibility = 'hidden';	 
		document.getElementById('back_div').style.height ='100%';
		
	     if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	 	   width = window.innerWidth;
	 	   height = window.innerHeight;
		 } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		    width = document.documentElement.clientWidth;
		    height = document.documentElement.clientHeight;
		 } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
		    width = document.body.clientWidth;
		    height = document.body.clientHeight;
		 }
		 var ScrollTop = document.body.scrollTop;
		 if(!ScrollTop){
			 if (window.pageYOffset)
		        ScrollTop = window.pageYOffset;
		    else
	    	    ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		 }
	     document.getElementById('popup').style.left = width/2 +'px';
	     document.getElementById('popup').style.top = height/2-document.getElementById('popup').offsetHeight/2+ScrollTop - 20+'px';	 
	    // document.getElementById('popup').style.position = 'fixed';     
}

function reportAbuse(sid){
	if(confirm("Are you sure you want to report this comment?")){
		var xmlHttp = GetXmlHttpObject();
	    xmlHttp.onreadystatechange=function(){
	      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
	      	if(xmlHttp.responseText){  
	      		document.getElementById('abuse'+sid).style.display = 'none';  
	      		document.getElementById('abusereported'+sid).innerHTML = 'Abuse Reported';  
	      	}
	      }
	    }
	    url = "ajax/aj_add_comment.php?comsid="+sid+"&abuse=1";
	    xmlHttp.open("GET",url,true);
	    xmlHttp.send(null);
	}
}

function showLogin(page, redirect){
	var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
      	if(xmlHttp.responseText){  
      		if(xmlHttp.responseText == 'redirect'){
      			page = decodeURIComponent(page);
      			window.location.href = page;
      		}else{
	      	  positionPopup(); 
    	  	  var popup = document.getElementById('popup');
      		  popup.innerHTML = xmlHttp.responseText;
      	  	//  popup.innerHTML += '<div onclick=\"remove(\'popup\'); remove(\'back_div\');\" style=\"padding:5px; cursor:pointer; font-weight:bold; font-size:15px\">Close</div>';
	     	  popup.style.visibility = 'visible'; 
      		}
      	}
      }
    }    
    url = "ajax/aj_show_login.php?page="+page+'&red='+redirect;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    
}

function login(x){
	if(x=='1'){
		var email = document.getElementById('email').value;
		var password = document.getElementById('password').value;
		var page= document.getElementById('page').value;
		var xmlHttp = GetXmlHttpObject();
	    xmlHttp.onreadystatechange=function(){
	      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
	      	if(xmlHttp.responseText){  
	      		if(xmlHttp.responseText == "true"){
	      			window.location.href = page;
	      		}else
		      		document.getElementById('login_msg').innerHTML = xmlHttp.responseText;
	      	}
	      }
	    }
	}
	if(x=='2'){
		var email = document.getElementById('email2').value;
		var password = document.getElementById('password2').value;
		var page= document.getElementById('page2').value;
		var xmlHttp = GetXmlHttpObject();
	    xmlHttp.onreadystatechange=function(){
	      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
	      	if(xmlHttp.responseText){  
	      		if(xmlHttp.responseText == "true"){
	      			window.location.href = page;
	      		}else
		      		document.getElementById('login_msg2').innerHTML = xmlHttp.responseText;
	      	}
	      }
	    }
	}
    url = "ajax/aj_login.php?page="+page+"&email="+email+'&pass='+password;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function inviteFriend(type, sid){
	/*var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
      	if(xmlHttp.responseText){  
      		if(xmlHttp.responseText == 'redirect'){
      			page = decodeURIComponent(page);
      			window.location.href = page;
      		}else{
	      	  positionPopup(); 
    	  	  var popup = document.getElementById('popup_form');
      		  popup.innerHTML = xmlHttp.responseText;
      	  	 // popup.innerHTML += '<div onclick=\"remove(\'popup\'); remove(\'back_div\');\" style=\"padding:5px; cursor:pointer; font-weight:bold; font-size:15px\">Close</div>';
	     	  popup.style.visibility = 'visible'; 
      		}
      	}
      }
    }    
    url = "ajax/aj_invite_friend.php?type="+type+'&sid='+sid;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);*/
    
    $("#popupform_backdiv").css({'display':'block'});
	$("#popup_form").css({'display':'block', 'width':'600px', 'margin-left':'-300px', 'height':'350px'});
	$.get("ajax/aj_invite_friend.php", { 
		type: type, sid:sid
		},function(data){
			$("#popup_form").html(data);
		});

	$("#popupform_backdiv").click(function(){
		$("#popupform_backdiv").fadeOut('1000');
		$("#popup_form").fadeOut('1000');
	});
	scroll(0,0);
}

function sendEmail(sid){	
	if(trim(document.getElementById('from_name').value) == ''){
		alert("Please fill out your name."); 
		return 0;
	}
	if(trim(document.getElementById('company_name').value) == ''){
		alert("Please fill out your company name."); 
		return 0;
	}
	var email = document.getElementById('invite_email').value;
	if(check_email(email)){
		
		document.getElementById('send_invite').disabled = true;
		var type= document.getElementById('invite_type').value;
		/*var msg= encodeURIComponent(document.getElementById('invite_message').value);*/
		var msg= document.getElementById('invite_message').value;
		var name= document.getElementById('from_name').value;
		var company_name= document.getElementById('company_name').value;
		 
		/*var xmlHttp = GetXmlHttpObject();
	    xmlHttp.onreadystatechange=function(){
	      if(xmlHttp.readyState==4 || xmlHttp.readySate=="complete"){
	      	if(xmlHttp.responseText){  
	      		var popup = document.getElementById('popup');
	      		popup.innerHTML = '<h2 style="margin:0; color:red">'+xmlHttp.responseText+'</h2>';
	      	//	popup.innerHTML += '<div onclick=\"remove(\'popup\'); remove(\'back_div\');\" style=\"padding:5px; cursor:pointer; font-weight:bold; font-size:15px\">Close</div>';
	      		//setTimeout("remove('popup'); remove('back_div');", 2000);
	      	}
	      }
	    }    
	    if(type=='event_rsvp'){
	    	url = "ajax/aj_event_rsvp.php?&sid="+sid+"&msg="+msg+"&name="+name+'&who='+email;
	    }else{
		    url = "ajax/aj_send_email.php?type="+type+'&sid='+sid+'&msg='+msg+'&name='+name+'&who='+email;
	    }
	    xmlHttp.open("GET",url,true);
	    xmlHttp.send(null);*/
		if(type=='event_rsvp'){
			$.get("ajax/aj_event_rsvp.php", { 
			sid:sid, msg:msg, name: name, company_name: company_name, who:email
			},function(data){
				$("#popup_form").html(data);
			});
		}else{
			$.get("ajax/aj_send_email.php", { 
			type:type, sid:sid, msg:msg, name: name, company_name: company_name, who:email
			},function(data){
				alert(type+' '+email+' '+sid+' '+msg+' '+name+' '+company_name);
				$("#popup_form").html(data);
			});
		}
	}
}

function GetXmlHttpObject(){
  var xmlHttp=null;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
  // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}