var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function block_see() {
	MM_findObj("fileInput").style.display = (MM_findObj("fileInput").style.display == 'none') ? document.all ? "block" : "table-row" : "none";	
}

function checkusername () {
	var username = MM_findObj('username_reg').value;
	var errors = '';
	if (username == '') {
		errors += '- Your UserName is required.\n';
		alert('The following error(s) occurred:\n'+errors);
	} else {
		window.open('/register_check.php?username='+username, 'User Name checked', 'HEIGHT=120,resizable=yes,WIDTH=400');
	}
}

function change(form) {
	var val2 = form.comment.value;
	if (form.comment.value == 'I Think This Site Is So...') {
		form.comment.value = '';
		form.comment.style = "background-color:#FFFFFF;";
	}
}

function checktext(form, form_panel) {
	var comment_text = MM_findObj('comment').value;
    if (form.key_code) {
        var key_code = MM_findObj('key_code').value;
    }
	var errors = "error";
	if (comment_text == 'I Think This Site Is So...') {
		form.comment.focus();
  		form.comment.select();		
	} else if (comment_text == '') {
		form.comment.value = '';
	} else if (key_code == ''){
		alert ('Please enter code!!!');
	} else {
		errors = "";	
	}
    if ((form_panel == 'form_panel') && (errors == '')) {
        var url_id = MM_findObj('url_id').value;
        var topic_id = MM_findObj('topic_id').value;        
        var subject_post = MM_findObj('subject_post').value;
            subject_post = subject_post.replace(/'/,"\'");
        var comment = MM_findObj('comment').value;
            comment = comment.replace(/'/,"\'");
        
        var url =       '/comment_add_new.php';
        var params =    'url_id='+url_id+'&comment='+comment+'&topic_id='+topic_id+'&subject_post='+subject_post+'&panel_frame=1';
        
        MM_findObj('message_box').style.display = 'none';
        MM_findObj('images_loading').style.display = "block";
        
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.setRequestHeader("Connection", "close");
        xmlhttp.send(params);
        xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                switch (xmlhttp.responseText) {
                    case '1':                        
                        MM_findObj('images_loading').style.display = 'none';
                        MM_findObj('message_good').style.display = 'block';
                    break;
                }
            }
        }
    }
	document.returnvalue = (errors == '');
}

function MM_showHideLayers() { //v6.0
	var i,p,v,obj,args=MM_showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3) {
		if ((obj=MM_findObj(args[i]))!=null) { 
			v=args[i+2];
			if (obj.style) { 
				obj=obj.style; 
				v=(v=='show')?'visible':(v=='hide')?'hidden':v; 
			}
			obj.visibility=v; 
		}
	}
}

function loader() {
	var obj1 = MM_findObj('uploadfile');
	var obj2 = MM_findObj('pixa');
	screens='file:///'+obj1.value;
	s=new String(screens);
	s=s.replace("%5C","/");
	s=s.replace("\\","/");
	obj2.src=s;
	MM_showHideLayers('Layer1','','show');
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) {
    test=args[i+2];
    nm=args[i+1];
    val=MM_findObj(args[i]);
	if (val.value == 'checkbox') { if (val.checked == false) errors += '- '+nm+' is required.\n'; }
    if (val) { if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function basename(path) {
	var c;
	path = (c=path.split('/')) ? c[c.length-1] : path;	
	path = (c=path.split('\\')) ? c[c.length-1] : path;
	return path; 
}

function CheckImages() {
	MM_findObj('message').style.display = (MM_findObj('message').style.display == "block") ? 'none' : 'none';
	var name = basename(MM_findObj('uploadfile').value);
	if ((name.indexOf('.jpeg') >= 0) || (name.indexOf('.jpg') >= 0) || (name.indexOf('.JPEG') >= 0) || (name.indexOf('.JPG') >= 0) || (name.indexOf('.gif') >= 0) || (name.indexOf('.GIF') >= 0) || (name.indexOf('.png') >= 0) || (name.indexOf('.PNG') >= 0)) {
		loader();
		return true;
	} else {
		var errors = '- This file is not Images';
		document.getElementById('message').innerHTML = errors;
		document.getElementById('message').style.display = "block";
		return false;
	}
}
function CheckForm() { //v4.0
	var errors;
	if (MM_findObj('register').value == "1") {
		errors = '- You not register. Sorry! But You must register!';
		MM_findObj('message').style.display = "block";
		MM_findObj('message').innerHTML = errors;
	}
	if (!errors) errors = check_images('uploadfile');
	document.MM_returnValue = (errors == '');
}

function report_bad_link (url_id) {
	//MM_findObj('bad_link').style.display = (MM_findObj('bad_link')) ? "none" : '';
	close_tab('bad_link');
	xmlhttp.open("GET", '/added_to_bad_link.php?id_url='+url_id,true);
    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
        if (xmlhttp.responseText == 1) {
          alert ('Thanks, for you help!');
        } else {
		  alert ('Sorry, but Error!');
        }
      }
    }
    xmlhttp.send(null);
}

function JSveryfield (action) {
	var errors = '';	
	if (action == 'award_list') {
		var answer = confirm('You certain that want to add in elected?');
	} else if (action == 'categ_list') {
		var answer = confirm('You kill last hope?');
	} else if (action == 'delete_list') {
		var answer = confirm('You want to delete Link?');
	} else if (action == 'clear_bad_list') {
		var answer = confirm("Clear 'Report Bad' on checked url?");
	}
	if (!answer) {
		errors = 'a';
	}
	document.MM_returnValue = (errors == '');
}
function categ(url_id) {
	var this_url = MM_findObj("this_url_"+url_id).innerHTML;
	if (confirm("You kill last hope '"+this_url+"'? ;)")) {
		alert ("You have killed last hope '"+this_url+"' ;)");
		links = location.href;
		url_redirect = links.split('/');
		link_url = (url_redirect[3] == (c = url_redirect[3].split('?'))) 
							? "/admin_action_site.php"+"?" 
							: "/admin_action_site.php?"+c[1]+"&";
		location = link_url+"categ="+url_id+"&redirect_path="+c[0];
	} else {
		alert ("About yes, you have felt sorry victim '"+this_url+"' ;D");
	}
}
function rescue(url_id) {
	var this_url = MM_findObj("this_url_"+url_id).innerHTML;
	if (confirm("You want to delete Link #'"+this_url+"'?")) {
		alert ("You delete Link #'"+this_url+"'");
		links = location.href;
		url_redirect = links.split('/');
		link_url = (url_redirect[3] == (c = url_redirect[3].split('?'))) 
							? "/admin_action_site.php"+"?" 
							: "/admin_action_site.php?"+c[1]+"&";
		location = link_url+"rescue="+url_id+"&redirect_path="+c[0];
	} else {
		alert ("You not delete #'"+this_url+"'");
	}
}
function award(url_id) {
	var this_url = MM_findObj("this_url_"+url_id).innerHTML;
	if (confirm("You certain that want to add in elected on this url: '"+this_url+"'?")) {
		alert ("You have added in elected on: '"+this_url+"'");
		links = location.href;
		url_redirect = links.split('/');
		link_url = (url_redirect[3] == (c = url_redirect[3].split('?'))) 
							? "/admin_action_site.php"+"?" 
							: "/admin_action_site.php?"+c[1]+"&";
		location = link_url+"award="+url_id+"&redirect_path="+c[0];
	} else {
		alert ("not award: '"+this_url+"'");
	}
}
function clear_bad(url_id) {
	var this_url = MM_findObj("this_url_"+url_id).innerHTML;
	if (confirm("You shoure clear 'Report Bad' on this url: '"+this_url+"'?")) {
		alert ("You have clear 'Report Bad' on '"+this_url+"'");
		links = location.href;
		url_redirect = links.split('/');
		link_url = (url_redirect[3] == (c = url_redirect[3].split('?'))) 
							? "/admin_action_site.php"+"?" 
							: "/admin_action_site.php?"+c[1]+"&";
		location = link_url+"clear_bad="+url_id+"&redirect_path="+c[0];
	} else {
		alert ("not clear '"+this_url+"'");
	}
}
function close_tab(tab_name) {
    jQuery('#'+tab_name).hide("slow");
}
function close_tab_sp(tab_name) {
	MM_findObj(tab_name).style.display = (MM_findObj(tab_name).style.display == 'block')
											? 'none'
											: 'none';
}
function open_tab(tab_name) {    
    jQuery('#'+tab_name).show("slow");
}
function screen(url_id) {
	close_tab('update_form');
	open_tab('screen_form');
	var site_name = MM_findObj('site_name_full_'+url_id).innerHTML;
	var images_path = MM_findObj('images_path_'+url_id).src;
	MM_findObj('url_to_site').innerHTML = site_name;
	MM_findObj('this_images').src = images_path;
	MM_findObj('this_id_url').value = url_id;
}

function edit_all(url_id) {
	close_tab('screen_form');
	xmlhttp.open("GET", '/admin_update_ajax.php?update='+url_id,true);
    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
        if (xmlhttp.responseText != 1) {		  
          MM_findObj('update_form').innerHTML = xmlhttp.responseText;
          open_tab('update_form');
		} else {
			alert ('error');
		}
      }
    }
    xmlhttp.send(null);
}

function open_send_email(send_email,id_user) {
	window.open("/send_email_to_user.php?user_id="+id_user,'Send E-mail',
    			"height=380,width=400,status=no,toolbar=no,menubar=no,scrollbars=yes,location=no");
}

function send_emails() {
    var images_loading = "<img src='images/loading_images.gif' />";
    var user_id = MM_findObj('user_id').value;
    var key = MM_findObj('key').value;
    var key_code = MM_findObj('key_code').value;
    var user_id_grantee = MM_findObj('user_id_grantee').value;
    var user_name_sender = MM_findObj('user_name_sender').value;
    var user_email_sender = MM_findObj('user_email_sender').value;
    var subject = MM_findObj('subject').value;
        subject = subject.replace(/'/,"\'");
    var message = MM_findObj('message').value;
        message = message.replace(/'/,"\'");
    
    var url =       "/send_email_ajax.php";
    var params =    'name_sender='+user_name_sender+'&email_sender='+user_email_sender+'&subject='+subject+'&message='+message+'&key='+key+'&key_code='+key_code+'&user_id='+user_id+'&user_id_grantee='+user_id_grantee;
    
    MM_findObj('temp_block').style.display = 'none';
    MM_findObj('message_error').innerHTML = images_loading;
    
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);
    xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {            
            switch (xmlhttp.responseText) {
                case '1':
                    MM_findObj('message_error').innerHTML = '';
                    alert ('You not registered.');
                    self.opener.reboot_page('/fast-register/');
                    close_page();
                break;
                case '2':
                    MM_findObj('message_error').innerHTML = '';
                    alert ("You have sent message");                    
                    close_page ();
                break;
                default:
                    MM_findObj('message_error').innerHTML = '';
                    alert ('You entered not faithfull data');
                    reboot_page ();
            }
        }
    }
}

function reboot_page (url) {
    if (url) {
        window.location = url; 
    } else {
        window.location = location.href;   
    }
}
function close_page () {
    window.close();
}

function GetSel(form_name, time) {
	var f = document.forms['myFormEdit'];
	var e = f.elements[form_name];
	for (var i=0; i < e.length; i++) {
		if (e[i].value == time) {
			e[i].selected = 'selected';
		} else {
			e[i].selected = '';
		}
	}
}

function edit_portfolio (text_list) {
  for (var i=1; i<(text_list.length); i+=2) {
    if (text_list[i-1] == 'name_images_trmb') {
        var name_images_trmb = text_list[i];
    } else if (text_list[i-1] == 'folder_images') {
        var folder_images = text_list[i];
    } else if (text_list[i-1] == 'category') {
        GetSel(text_list[i-1], text_list[i])
    } else if (MM_findObj(text_list[i-1])) {
        MM_findObj(text_list[i-1]).value = text_list[i];
    }
  }              
  MM_findObj('name_images_trmb').src = '/portpholio/'+folder_images+'/'+name_images_trmb;
}

function edit_all_images (text_list) {
  for (var i=1; i<(text_list.length); i+=2) {
    if (text_list[i-1] == 'name_images_trmb') {
        var name_images_trmb = text_list[i];
    } else if (text_list[i-1] == 'folder_images') {
        var folder_images = text_list[i];
    } else if (text_list[i-1] == 'category') {
        GetSel(text_list[i-1], text_list[i])
    } else if (MM_findObj(text_list[i-1])) {
        MM_findObj(text_list[i-1]).value = text_list[i];
        if (text_list[i-1] == 'type_images') {
            var type_images = text_list[i];
        }
    }
  }              
  MM_findObj('name_images_trmb').src = '/'+type_images+'/'+folder_images+'/'+name_images_trmb;
}

function edit_sites(text_list) {
  for (var i=1; i<(text_list.length); i+=2) {  
    if (text_list[i-1] == 'img_name') {
        var name_images_trmb = text_list[i];
    } else if (text_list[i-1] == 'category') {
        GetSel(text_list[i-1], text_list[i])
    } else if (MM_findObj(text_list[i-1])) {
        MM_findObj(text_list[i-1]).value = text_list[i];
        if (MM_findObj(text_list[i-1]+'_hidden')) {
            MM_findObj(text_list[i-1]+'_hidden').value = text_list[i];
        }
    }
  }
  MM_findObj('name_images_trmb').src = '/screen/'+name_images_trmb;
}

function change_block (text_list,action_page) {
  for (var i=1; i<(text_list.length); i+=2) {
    if (text_list[i-1] == 'name_images_trmb') {
        var name_images_trmb = text_list[i];
    } else if (text_list[i-1] == 'folder_images') {
        var folder_images = text_list[i];
    } else if (text_list[i-1] == 'description_text') {
        var description = text_list[i];
    } else if ((text_list[i-1] == 'id_maket') || (text_list[i-1] == 'id_site')) {
        var this_id = text_list[i];
    } else if (text_list[i-1] == 'type_images') {
        var type_images = text_list[i];
    }
  }
  if (action_page == 'portfolio') {
    MM_findObj('images_block_'+this_id).src = '/portpholio/'+folder_images+'/'+name_images_trmb;
  } else if (action_page == 'all_images') {
    MM_findObj('images_block_'+this_id).src = '/'+type_images+'/'+folder_images+'/'+name_images_trmb;
  }
  MM_findObj('description_block_'+this_id).innerHTML = description;
}

function edit_this_change(action, what_action, id) {
    open_tab('loading');
    if (action == 'close') {
        close_tab('this_edit_block');
    } else if ((action == 'edit') && (what_action)) {
        if ((what_action == 'wallpaper') || (what_action == 'creative') || (what_action == 'photo')) {
            what_action_var = 'all_other_images&type='+what_action;
            what_action_url = 'all_other_images'
        } else {
            what_action_url = what_action;
            what_action_var = what_action;
        }        
        xmlhttp.open("GET", '/ajax_edit_'+what_action_url+'.php?action='+action+'&what_action='+what_action_var+'&id_maket='+id,true);
        xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState == 4) {
            if (xmlhttp.responseText != 1) {
              var text_new = xmlhttp.responseText.split('<--->');
              var text_new = text_new[0].split('=>');
              if (what_action == 'portfolio') {
                edit_portfolio (text_new); 
              } else if (what_action == 'sites') {
                edit_sites (text_new);
              } else if ((what_action == 'wallpaper') || (what_action == 'creative') || (what_action == 'photo')) {
                edit_all_images (text_new);
              }
              //alert(xmlhttp.responseText);
              open_tab('this_edit_block');
              close_tab('loading');
            } else {
              alert ('Error');
              close_tab('loading');
            }
          }
        }
        xmlhttp.send(null);        
    }
}

jQuery(document).ready(function() { 
    var options = { 
        //target:        '#output',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
    };  
    // bind form using 'ajaxForm' 
    jQuery('#myFormEdit').ajaxForm(options); 
}); 

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    var queryString = jQuery.param(formData);
    open_tab('loading');
    return true; 
} 
// post-submit callback 
function showResponse(responseText, statusText)  {    
    var text_new = responseText.split('<--->');
    if (text_new[0].match("=>")) {
        text_new = text_new[0].split('=>');
    } else {
        text_new = text_new[0].split('=&gt;');
    }
    if (text_new[1] == 'portfolio') {
      edit_portfolio(text_new);
      change_block(text_new,'portfolio');
    } else if (text_new[1] == 'sites') {
      edit_sites(text_new);
      change_block(text_new,'sites');
    } else if (text_new[1] == 'all_images') {
      edit_all_images(text_new);
      change_block(text_new,'all_images');
    }
    close_tab('loading');
} 

function push_star_to_base (id_star) {
	var maket_id, const_id, mode_star, prim;
	mode_star = MM_findObj('mode_star').value;
	maket_id = MM_findObj('maket_id').value;
	const_id = MM_findObj('const_id').value;
    //if (id_star) {
    //    alert ('Sorry but you can\'t vote at this moment.');
    //} else {
        if (mode_star == '2') {
            alert ('Sorry, But you already voted!');
        } else {
            MM_findObj('mode_star').value = 2;
            xmlhttp.open("GET", '/setrate.php?id='+maket_id+'&value='+id_star+'&ncok='+mode_star,true);
            xmlhttp.onreadystatechange=function() {
              if (xmlhttp.readyState==4) {			
                prim = xmlhttp.responseText.split(":");
                MM_findObj('const_id').value = prim[0];
                MM_findObj('count_star').innerHTML = prim[1];
                restart_star();			
              }
            }
            xmlhttp.send(null);
        }
    //}
}

function change_star(id_star) {
	var i, to_const;
	if (MM_findObj('star_img_'+id_star).src.indexOf('_black.gif') == -1) {
		for (i=1; i<=id_star; i++) {
			MM_findObj('star_img_'+i).src = "/images/stars_black.gif";
		}
	}
	to_const = Number(MM_findObj('const_id').value)+1;
	if (id_star < to_const) {
		to_const = Number(MM_findObj('const_id').value)+1;
	} else {
		to_const = Number(id_star)+1;
	}
	for (i=to_const; i<=5; i++) {
		MM_findObj('star_img_'+i).src = "/images/stars_no.gif";
	}
}

function restart_star() {
	var to_const, i;
	to_const = Number(MM_findObj('const_id').value);
	for (i=1; i<=to_const; i++) {
		MM_findObj('star_img_'+i).src = "/images/stars_yes.gif";
	}
	to_const = to_const+1;
	for (i=to_const; i<=5; i++) {
		MM_findObj('star_img_'+i).src = "/images/stars_no.gif";
	}
}

function subscribe() {
	close_tab('empty_subscribe');
	close_tab('not_email_subscribe');
	close_tab('not_email_subscribe');
	close_tab('thanks_subscribe');
	close_tab('already_subscribe');
	if (MM_findObj('subscribe').value == '') {
		open_tab('empty_subscribe');
	} else if (MM_findObj('subscribe').value == 'Enter Your Email') {
		open_tab('not_email_subscribe');
	} else {
		var subscribe = MM_findObj('subscribe').value;
		xmlhttp.open("GET", '/action_subscribe.php?subscribe='+subscribe,true);
		xmlhttp.onreadystatechange=function() {
		  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {   		
			//alert (xmlhttp.responseText);
			if (xmlhttp.responseText == '1') {
				open_tab('not_email_subscribe');
			} else if (xmlhttp.responseText == '2') {
				open_tab('thanks_subscribe');
			} else if (xmlhttp.responseText == '3') {
				open_tab('already_subscribe');
			}
		  }
		}
		xmlhttp.send(null);	
	}
	MM_findObj('subscribe').value = 'Enter Your Email';
}

