function showError(r){if((r.responseText!="")&&($('ajax_result'))){var re=new RegExp(/<body>([\s\S]*)<\/body>/m);var nobody=re.exec(r.responseText);$('ajax_result').set('html',nobody[1]);$('ajax_result').setStyle('display','block');}else{alert("Error: "+ r.status+" - "+ r.statusText);}}
function updateField(fieldName,r){var field=$(fieldName);if(!field){return;}
field.set('html',r.responseText);}
function saveTinyMCE(){try{tinyMCE.triggerSave();}
catch(e){}}
function CheckIfError(r){var result=r.documentElement;var error=result.getElementsByTagName('error')[0].firstChild.data;var error_string=result.getElementsByTagName('error_string')[0].firstChild.data;if(error==1){if(error_string!=""){alert("Error: "+ error_string);}
return true;}
return false;}
function setRating(rating,media_key){new Request({url:"ajax_com.php?action=mediarating",method:'post',onSuccess:function(e){updateField('media_rating',this.xhr);},onFailure:function(e){showError(this.xhr)},data:'rating='+ encodeURIComponent(rating)+'&mediakey='+ encodeURIComponent(media_key)}).send();}
function setFavorite(media_key){new Request({url:"ajax_com.php?action=favourite",method:'post',onFailure:function(e){showError(this.xhr)},onComplete:function(e){showFavouriteResult(this.xhr.responseXML)},data:'&mediakey='+ encodeURIComponent(media_key)}).send();}
function showFavouriteResult(r){if(CheckIfError(r))return;var result=r.documentElement;var image=result.getElementsByTagName('image')[0].firstChild.data;var image_alt=result.getElementsByTagName('image_alt')[0].firstChild.data;var message=result.getElementsByTagName('message')[0].firstChild.data;$('favorite_button').style.height=result.getElementsByTagName('height')[0].firstChild.data+"px";$('favorite_button').style.width=result.getElementsByTagName('width')[0].firstChild.data+"px";$('favorite_button').className='button';$('favorite_button').src=image;$('favorite_button').alt=$('favorite_button').title=image_alt;}
function checkMediaForm(){saveTinyMCE();var form_value=$('comment_text_id').value;$('comment_text_id').value='';if(form_value==""){$('comment_text_id').focus();new Fx.Style('comment_text_id','background-color').start('#FF0000','#ffffff');}else{new Request({url:"ajax_com.php?action=comments",method:'post',onFailure:function(e){showError(this.xhr)},onSuccess:function(e){updateField('comments_div',this.xhr);},data:'comment='+ encodeURIComponent(form_value)+'&mediakey='+ encodeURIComponent($('comment_mediakey_id').value)}).send()};return false;}
function CheckUploadForm(){saveTinyMCE();$('upload_media_title_error').setStyle('display','none');$('upload_file_error').setStyle('display','none');$('upload_file_type_error').setStyle('display','none');$('upload_media_description_error').setStyle('display','none');$('upload_media_category_error').setStyle('display','none');$('errors').setStyle('display','none');if($('upload_media_title_id').value=='')$('upload_media_title_error').setStyle('display','block');if($('upfile_0_id').value=='')$('upload_file_error').setStyle('display','block');if($('upload_media_description_id').value=='')$('upload_media_description_error').setStyle('display','block');$('upload_media_category_error').setStyle('display','block');$$('input[id^=upload_media_category]').each(function(checkbox){if(checkbox.checked==true)$('upload_media_category_error').setStyle('display','none');});if(($('upload_media_title_error').getStyle('display')=="block")||($('upload_file_error').getStyle('display')=="block")||($('upload_media_description_error').getStyle('display')=="block")||($('upload_media_category_error').getStyle('display')=="block")||($('upload_file_type_error').getStyle('display')=="block")){$('errors').setStyle('display','block');return false;}else{$$('input[id^=upload_media_type]').each(function(radio){if(radio.checked==true)$('filetype_id').value=radio.value;});new Request({url:'ajax_com.php?action=checktype&umt='+$('filetype_id').value,method:'post',onFailure:function(e){showError(this.xhr)},onComplete:function(e){if(this.xhr.responseXML.documentElement.getElementsByTagName('filetype')[0].firstChild.data!="ok"){$('upload_file_type_error').setStyle('display','block');$('errors').setStyle('display','block');return false;}else{uploadFiles();};},data:'ext='+encodeURIComponent($('upfile_0_id').value)}).send();};}
function CheckUploadForm2(){saveTinyMCE();$('upload_media_title_error').setStyle('display','none');$('upload_file_error').setStyle('display','none');$('upload_file_type_error').setStyle('display','none');$('upload_media_description_error').setStyle('display','none');$('upload_media_category_error').setStyle('display','none');$('errors').setStyle('display','none');if($('upload_media_title_id').value=='')$('upload_media_title_error').setStyle('display','block');if($('upfile_0_id').value=='')$('upload_file_error').setStyle('display','block');if($('upload_media_description_id').value=='')$('upload_media_description_error').setStyle('display','block');$('upload_media_category_error').setStyle('display','block');$$('input[id^=upload_media_category]').each(function(checkbox){if(checkbox.checked==true)$('upload_media_category_error').setStyle('display','none');});if(($('upload_media_title_error').getStyle('display')=="block")||($('upload_file_error').getStyle('display')=="block")||($('upload_media_description_error').getStyle('display')=="block")||($('upload_media_category_error').getStyle('display')=="block")||($('upload_file_type_error').getStyle('display')=="block")){$('errors').setStyle('display','block');return false;}else{$$('input[id^=upload_media_type]').each(function(radio){if(radio.checked==true)$('filetype_id').value=radio.value;});new Request({url:'ajax_com.php?action=checktype&umt='+$('filetype_id').value,method:'post',onFailure:function(e){showError(this.xhr)},onComplete:function(e){if(this.xhr.responseXML.documentElement.getElementsByTagName('filetype')[0].firstChild.data!="ok"){$('upload_file_type_error').setStyle('display','block');$('errors').setStyle('display','block');return false;}else{$('form_upload').submit();};},data:'ext='+encodeURIComponent($('upfile_0_id').value)}).send();};}
function ChangeForm(mediatype){new Request({url:"ajax_com.php?action=mediatype&type="+encodeURIComponent(mediatype),method:'get',onSuccess:function(e){updateField('media_type',this.xhr);},onFailure:function(e){showError(this.xhr)}}).send();}
function checkGuestBook(){saveTinyMCE();$('activity_errors').setStyle('display','none');if(($('guestbook_text_id').value=='')&&($('guestbook_media_id').value=='0')){$('activity_errors').setStyle('display','block');}
if($('activity_errors').getStyle('display')=='none'){new Request({url:'ajax_com.php?action=guestbook_insert&uid='+ encodeURIComponent($('user_id_id').value),method:'post',onSuccess:function(e){updateField('user_activity',this.xhr);},onFailure:function(e){showError(this.xhr)},data:'guestbook_media_id='+ encodeURIComponent($('guestbook_media_id').value)+'&guestbook_text='+ encodeURIComponent($('guestbook_text_id').value)}).send();}}
function showActivity(field,params){new Request({url:'ajax_com.php?action=activity',method:'post',onSuccess:function(e){showActivityResult(field,this.xhr);},onFailure:function(e){showError(this.xhr)},data:'params='+ encodeURIComponent(params)}).send();}
function showActivityResult(fieldName,r){var field=$(fieldName);if(!field){return;}
if(field.getParent()){field=field.getParent();}
field.set('html',r.responseText);}
function showGuestBook(offset){$('errors').setStyle('display','none');var myAjax=new Request({url:"ajax_com.php?action=guestbook&uid="+encodeURIComponent($('user_id_id').value)+"&offset="+encodeURIComponent(offset),method:'get',onSuccess:function(e){updateField('user_guestbook',this.xhr);},onFailure:function(e){showError(this.xhr)}}).send();return false;}
function delete_guestbook(gid){new Request({url:'ajax_com.php?action=gb_delete&uid='+encodeURIComponent($('user_id_id').value)+'&offset='+encodeURIComponent($('offset_id').value),method:'post',onSuccess:function(e){updateField('user_guestbook',this.xhr);},onFailure:function(e){showError(this.xhr)},data:'gid='+encodeURIComponent(gid)}).send();return false;}
function checkChatForm(){$('errors').setStyle('display','none');$('errors2').setStyle('display','none');if($('user_id_id').value==''){$('errors2').setStyle('display','block');}else{if($('chat_text_id').value=='')$('errors').setStyle('display','block');};if(($('errors').getStyle('display')!="block")&&($('errors2').getStyle('display')!="block")){var myAjax=new Request({url:"ajax_com.php?action=chat&uid="+encodeURIComponent($('user_id_id').value),method:'post',onFailure:function(e){showError(this.xhr)},onSuccess:function(e){updateField('chatbox',this.xhr);},data:'txt='+encodeURIComponent($('chat_text_id').value)}).send();};}
function refreshChat(){if((!$('errors')||$('errors').getStyle('display')!="block")&&(!$('errors2')||$('errors2').getStyle('display')!="block")&&(!$('chat_text_id')||$('chat_text_id').value=='')){var myAjax=new Request({url:"ajax_com.php?action=chat",method:'post',onSuccess:function(e){updateField('chatbox',this.xhr);},data:''}).send();};}
function showUserFriends(id,show){new Request({url:'ajax_com.php?action=friends&id='+encodeURIComponent(id)+'&show='+encodeURIComponent(show),method:'get',onFailure:function(e){showError(this.xhr)},onComplete:function(e){$('friends_main_'+id).set('html',this.xhr.responseXML.documentElement.getElementsByTagName('friends')[0].firstChild.data);var show=this.xhr.responseXML.documentElement.getElementsByTagName('friends_show')[0].firstChild.data;$('friends_'+id).removeEvents('click');$('friends_'+id).addEvent('click',function(e){e=new Event(e).stop();showUserFriends(id,show);});}}).send();}
function add_nick(nick){if($('message_id').value.indexOf(nick+':')!=0)$('message_id').value=nick+': '+ $('message_id').value;}
function make_text(id,startTag,endTag){var txtarea=document.getElementById(id);txtarea.focus();startTag='<'+startTag+'>';if(endTag)endTag='</'+endTag+'>';var scrtop=txtarea.scrollTop;var cursorPos=getCursor(txtarea);var txt_pre=txtarea.value.substring(0,cursorPos.start);var txt_sel=txtarea.value.substring(cursorPos.start,cursorPos.end);var txt_aft=txtarea.value.substring(cursorPos.end);if(cursorPos.start==cursorPos.end){var nuCursorPos=cursorPos.start+ startTag.length;}else{var nuCursorPos=String(txt_pre+ startTag+ txt_sel+ endTag).length;}
txtarea.value=txt_pre+ startTag+ txt_sel+ endTag+ txt_aft;setCursor(txtarea,nuCursorPos,nuCursorPos);if(scrtop)txtarea.scrollTop=scrtop;return false;}
function getCursor(input){var result={start:0,end:0};if(input.setSelectionRange){result.start=input.selectionStart;result.end=input.selectionEnd;}else if(!document.selection){return false;}else if(document.selection&&document.selection.createRange){var range=document.selection.createRange();var stored_range=range.duplicate();stored_range.moveToElementText(input);stored_range.setEndPoint('EndToEnd',range);result.start=stored_range.text.length- range.text.length;result.end=result.start+ range.text.length;}
return result;}
function setCursor(txtarea,start,end){if(txtarea.createTextRange){var range=txtarea.createTextRange();range.move("character",start);range.select();}else if(txtarea.selectionStart){txtarea.setSelectionRange(start,end);}}
function insert_image(id,message){var src=prompt(message,'http://');if(src){make_text(id,'img src="'+ src+'" alt="" /','');}}
function insert_link(id,message){var href=prompt(message,'http://');if(href){make_text(id,'a href="'+ href+'" target="_blank"','a');}}
function checkBlogForm(){saveTinyMCE();$('errors_div').setStyle('display','none');try{title=$('title_id').value;if((!$('title_id').value)||((!$('message_id').value)&&($('blog_media_id_id').value==0))){$('message_id').focus();$('errors_div').setStyle('display','block');}else{$('blogform').submit();}}
catch(e){if((!$('message_id').value)&&($('blog_media_id_id').value==0)){$('message_id').focus();$('errors_div').setStyle('display','block');}else{$('blogform').submit();}}
return false;}
function edit_text(type,id){new Request({url:'ajax_com.php?action=language&id=blog&id2='+encodeURIComponent(id)+'&type='+encodeURIComponent(type),method:'get',onFailure:function(e){showError(this.xhr)},onComplete:function(e){try{var message_text=this.xhr.responseXML.documentElement.getElementsByTagName('blog_innerhtml')[0].firstChild.data;}catch(ex){var message_text='';};$(type+'_'+id).set('html','<textarea id="txt_'+type+'_'+id+'" name="txt_'+type+'_'+id+'" class="blog_'+type+'_text">'+message_text+'</textarea>');$(type+'_'+id+'_admin').set('html','[ <a onclick="save_text(\''+type+'\','+id+');" style="cursor: pointer;">'+this.xhr.responseXML.documentElement.getElementsByTagName('blog_save')[0].firstChild.data+'</a> | <a onclick="set_back(\''+type+'\','+id+');" style="cursor: pointer;">'+this.xhr.responseXML.documentElement.getElementsByTagName('blog_cancel')[0].firstChild.data+'</a> ]');}}).send();}
function delete_text(type,id){new Request({url:'ajax_com.php?action=language&id=blog',method:'get',onFailure:function(e){showError(this.xhr)},onComplete:function(e){if(confirm(this.xhr.responseXML.documentElement.getElementsByTagName('blog_delete_sure')[0].firstChild.data)){new Request({url:'ajax_com.php?action=blog_delete',method:'post',onFailure:function(e){showError(this.xhr)},onComplete:function(e){document.location.reload();},data:'type='+encodeURIComponent(type)+'&id='+encodeURIComponent(id)}).send();};}}).send();}
function set_back(type,id){new Request({url:'ajax_com.php?action=blog_setback',method:'post',onFailure:function(e){showError(this.xhr)},onComplete:function(e){$(type+'_'+id).set('html',this.xhr.responseXML.documentElement.getElementsByTagName('text')[0].firstChild.data);$(type+'_'+id+'_admin').set('html','[ <a href="javascript:edit_text(\''+type+'\','+id+');">'+this.xhr.responseXML.documentElement.getElementsByTagName('edit')[0].firstChild.data+'</a> | <a href="javascript:delete_text(\''+type+'\','+id+');">'+this.xhr.responseXML.documentElement.getElementsByTagName('delete')[0].firstChild.data+'</a> ]');},data:'type='+encodeURIComponent(type)+'&id='+encodeURIComponent(id)}).send();}
function save_text(type,id){new Request({url:'ajax_com.php?action=blog_save',method:'post',onFailure:function(e){showError(this.xhr)},onComplete:function(e){$(type+'_'+id).set('html',this.xhr.responseXML.documentElement.getElementsByTagName('text')[0].firstChild.data);$(type+'_'+id+'_admin').set('html','[ <a href="javascript:edit_text(\''+type+'\','+id+');">'+this.xhr.responseXML.documentElement.getElementsByTagName('edit')[0].firstChild.data+'</a> | <a href="javascript:delete_text(\''+type+'\','+id+');">'+this.xhr.responseXML.documentElement.getElementsByTagName('delete')[0].firstChild.data+'</a> ]');},data:'type='+encodeURIComponent(type)+'&id='+encodeURIComponent(id)+'&text='+encodeURIComponent($('txt_'+type+'_'+id).value)}).send();}
function pm_mark_all(value){if(typeof value=='undefined'){value=true;}
$(document).getElements('input[type=checkbox]').setProperty('checked',value);}
function show_referer(offset){new Request({url:'ajax_com.php?action=referer&offset='+offset,method:'post',onSuccess:function(e){updateField('referer',this.xhr);},onFailure:function(e){showError(this.xhr)},data:'mkey='+encodeURIComponent($('media_key').value)}).send();}
function initMap(mode){if(!GBrowserIsCompatible()){return;}
if(!$('map_canvas')||!$('location_longitude')||!$('location_latitude')||!$('location_zoom')){return;}
var field=$('map_canvas');field.map=new GMap2(field);field.map.setMapType(G_HYBRID_MAP);field.geocoder=new GClientGeocoder();var center=new GLatLng(parseFloat($('location_latitude').value),parseFloat($('location_longitude').value));field.map.setCenter(center,parseInt($('location_zoom').value));field.map.addControl(new GMapTypeControl());field.map.addControl(new GLargeMapControl());field.map.enableScrollWheelZoom();if(mode=='edit'){field.marker=new GMarker(center,{draggable:true,autoPan:true});GEvent.addListener(field.marker,'dragend',dragMapMarker);GEvent.addListener(field.map,'click',clickMap);field.map.addOverlay(field.marker);GEvent.addListener(field.map,'zoomend',zoomMap);}
else{field.marker=new GMarker(center);field.map.addOverlay(field.marker);}
GEvent.addListener(field.marker,'click',clickMarker);if(mode!='edit'){clickMarker(center);}}
function clickMap(overlay,loc,overlayLoc){if(!overlay){$('map_canvas').marker.setLatLng(loc);dragMapMarker(loc);}}
function dragMapMarker(loc){$('location_longitude').value=loc.lng();$('location_latitude').value=loc.lat();$('map_canvas').geocoder.getLocations(loc,showAddress);}
function zoomMap(oldLevel,newLevel){$('location_zoom').value=newLevel;}
function showAddress(response){if(!response||response.Status.code!=200){$('location_str').value='';return;}
$('location_str').value=response.Placemark[0].address;}
function clickMarker(loc){if(!$('location_str').value){return;}
var locationParts=$('location_str').value.split(', ');$('map_canvas').map.openInfoWindowHtml(loc,'<div style="color:#000;">'+ locationParts.join('<br />')+'</span>');}
function findMapCoordinates(){if(!$('location_str').value){return;}
$('map_canvas').geocoder.getLocations($('location_str').value,setLocation);}
function setLocation(response){if(!response||response.Status.code!=200){return;}
$('location_str').value=response.Placemark[0].address;var loc=new GLatLng(response.Placemark[0].Point.coordinates[1],response.Placemark[0].Point.coordinates[0]);$('map_canvas').map.setZoom(8+response.Placemark[0].AddressDetails.Accuracy);$('map_canvas').marker.setLatLng(loc);$('map_canvas').map.setCenter(loc);$('location_longitude').value=response.Placemark[0].Point.coordinates[0];$('location_latitude').value=response.Placemark[0].Point.coordinates[1];clickMarker(loc);}
function copyAddressData(){var parts=new Array();var addressFields=new Array('street','zip','city','country');for(i=0;i<addressFields.length;i++){field=addressFields[i];if(!$(field+'_id')||!$(field+'_id').value){continue;}
parts[parts.length]=$(field+'_id').value;}
$('location_str').value=(parts.length>1)?parts.join(', '):'';findMapCoordinates();}
function centerMap(){if(!$('map_canvas').marker){return;}
$('map_canvas').map.setCenter($('map_canvas').marker.getLatLng());}
function inputHover(elemID,elemSrc){if((Browser.Engine.name=='trident')&&(Browser.Engine.version<5)){return;}
if(!$(elemID)){return;}
$(elemID).setProperty('src',elemSrc);}
function calcTagCloudWidth(element){if(!element){element=$('flashcloud');if(!element){return false;}}
var newSize=element.getSize();return newSize.x;}
function updateTagCloudWidth(){var newWidth=calcTagCloudWidth();if(!newWidth){return;}
try{var currentSize=$('tagcloud').getSize();}
catch(e){var currentSize=0;}
if(newWidth!=currentSize.x){try{$('flashcloud').getChildren().setStyle('width',newWidth+'px');}
catch(e){}}}
function send2friend(key){if(!$('send2friend_email_id')||!$('send2friend_message_id')){return;}
new Request({url:'ajax_com.php?action=send2friend',method:'post',onFailure:function(e){showError(this.xhr)},onComplete:function(e){updateShareBox(this.xhr)},data:'key='+ encodeURIComponent(key)+'&email='+ encodeURIComponent($('send2friend_email_id').value)+'&message='+ encodeURIComponent($('send2friend_message_id').value)}).send();}
function updateShareBox(transport){if(!transport.responseXML||!transport.responseXML.documentElement){return;}
var docElement=transport.responseXML.documentElement;var action=docElement.getElementsByTagName('action')[0].firstChild.data;var message=docElement.getElementsByTagName('message')[0].firstChild.data;var error=parseInt(docElement.getElementsByTagName('error')[0].firstChild.data);switch(action){case'send2friend':$('sharebox_status').set('html',message);$('sharebox_status').setStyle('display','block');if(!error){$('send2friend_email_id').value='';$('send2friend_message_id').value='';}
window.setTimeout('hideShareBoxStatus()',3000);break;}}
function hideShareBoxStatus(){$('sharebox_status').set('html','');$('sharebox_status').setStyle('display','none');}
