function selected(cal, date) {
  cal.sel.value = date; 
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
      cal.callCloseHandler();
}
function closeHandler(cal) {
    cal.hide();                        // hide the calendar
   _dynarch_popupCalendar = null;
}

function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    cal.weekNumbers = false;
    cal.isPopup = true;
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

function showClick(id)
{

	contentDetails = document.getElementById(id);
	ln = id.substr(6);
	row = document.getElementById( "r" + id )
	if (row) row.style.backgroundColor = (row.style.backgroundColor=="")?"#afb2b4":"";
	contentLink = document.getElementById('child'+ln);
	if (contentDetails.style.display == "none") {
        contentLink.firstChild.src = baseurl+"/public/images/pointer_up.gif";
		contentDetails.style.display = "";
	} else {
		contentDetails.style.display = "none";
        contentLink.firstChild.src = baseurl+"/public/images/pointer_down.gif";
	}
	return false; // чтобы не происходил переход по ссылке
}

function gotoLink(element, id, name)
{
	details = document.getElementById(id);
	element.href = element.href + '/' + name + '/' + details.value
//    alert(element.href);
	return true; // чтобы не происходил переход по ссылке
}

function changePhoto(id, photoArray)
{

	field_element = document.getElementById(id);
	div_element = document.getElementById('photo');
	images = photoArray.evalJSON();
	content = baseurl;
	if (field_element.value != 'none') {
		if (images['photo'+field_element.value] && images['photo'+field_element.value] != null) {
			content = '<img src="'+content + '/public/photos/mid/' + images['photo'+field_element.value]+'"/>';
			div_element.innerHTML = content;
		} else {
			div_element.innerHTML = '';
		}
	} else {
		div_element.innerHTML = '';
	}
}

function photoselect(element) {
	div_element = document.getElementById('photos_area');
	elements = div_element.getElementsByTagName('img');
	for (var i = 0; i < elements.length; i++) {
        current_element = elements[i];
        if (current_element.className=="selectedphoto") {
        	current_element.className="";
        } else if (current_element.className=="mainstyle selectedphoto") {
        	current_element.className="mainstyle";
        }
	}
	if (element.className=="mainstyle") {
		element.className="mainstyle selectedphoto";
	} else {
		element.className="selectedphoto";
	}
	document.getElementById('selectedphoto').value=element.id;
}

function setupMainPhoto() {
	div_element = document.getElementById('photos_area');
	elements = div_element.getElementsByTagName('img');
	res = false
	for (var i = 0; i < elements.length; i++) {
        current_element = elements[i];
        if (current_element.className=="mainstyle") {
        	current_element.className="";
        } else if (current_element.className=="selectedphoto") {
        	document.getElementById('mainphoto').value=current_element.id;
        	document.getElementById('photos_process').innerHTML = imageProcess;
        	res = true;
        }
	}
	return res;
}

function delPhoto() {
	selectedphoto = document.getElementById('selectedphoto');
	delelement = document.getElementById(selectedphoto.value);
	if (selectedphoto.value && (delelement.className == "mainstyle selectedphoto" || delelement.className == "selectedphoto")) {
	    delelement.className = "";
        document.getElementById('photos_process').innerHTML = imageProcess;
	    return true;
	} else {
		return false;
	}
}
	
function photoshow(element) {
	div_element = document.getElementById('photos_area');
	elements = div_element.getElementsByTagName('img');
	for (var i = 0; i < elements.length; i++) {
        current_element = elements[i];
        if (current_element.className=="selectedphoto") {
        	current_element.className="";
	    }
	}
	element.className = "selectedphoto";
	bigPhoto = document.getElementById('show_photo');
	src = element.src.replace('/small/', '/big/');
	bigPhoto.src = src;
}

function otherImage(type) {
	div_element = document.getElementById('photos_area');
	elements = div_element.getElementsByTagName('img');
    for (var i = 0; i < elements.length; i++) {
         current_element = elements[i];
         if (current_element.className=="selectedphoto") {
         	 if (type=='next' && current_element.nextSibling.tagName=='IMG') {
         	 	 newelement = current_element.nextSibling;
         	 	 photoshow(newelement);
         	 } else if (type=='prev' && current_element.previousSibling.tagName=='IMG') {
         	 	 newelement = current_element.previousSibling;
         	 	 photoshow(newelement);
         	 }
         	 break;
         }
    }
}

function handleSuccess(transport) {
    timeoutID=setTimeout("startTimer()", timerInterval);
//    alert(transport.responseText);
    data = transport.responseText.evalJSON(true);
    if (data.redirect) {
    	document.location = data.url;
    }
    if (data.type == 'update') {
    	divelement = document.getElementById("chatmessages");
    } else if (data.type == 'history') {
    	divelement = document.getElementById("chathistory");
    	divelement.style.display = "block";
    }
    rows = data.data.size();
    for(var i = 0; i < rows; i++) {
    	divhtml = '<div>' + data.data[i] + '</div>';
    	divelement.innerHTML += divhtml;
    }
    divelement.scrollTop=divelement.scrollHeight-divelement.offsetHeight;
}

function inviteSuccess(transport) {
    timeoutID=setTimeout("startTimer()", timerInterval);
    data = transport.responseText.evalJSON(true);
    divelement = document.getElementById("chatinvitations");
    rows = data.data.size();
    for(var i = 0; i < rows; i++) {
    	divelement.innerHTML += data.data[i];
    }
}

function refusedSuccess(transport) {
    timeoutID=setTimeout("startTimer()", timerInterval);
    data = transport.responseText.evalJSON(true);
    if (data.process=='ok') {
    	divelement = document.getElementById(data.chat_id);
    	parentelement = divelement.parentNode;
    	parentelement.removeChild(divelement);
    }
}

function handleFailure(transport) {
    alert('Error: ' + transport.statusText);
    timeoutID=setTimeout("startTimer()", timerInterval);
}


function accessChat(type) {
    var url = baseurl+"/chat/update/";
    var chat_id = document.getElementById('chat_id');
    if (type == 'none') {
    	postvalues = 'chat_id=' + chat_id.value;
    } else if (type == 'send') {
    	message = document.getElementById('text');
    	postvalues = 'chat_id=' + chat_id.value + '&text=' + message.value;
    } else if (type == 'history') {
    	postvalues = 'chat_id=' + chat_id.value;
    	url = baseurl+"/chat/history/";
    }
    clearTimeout(timeoutID);
    var options = {
                    method    : 'post',
                    onSuccess : handleSuccess,
                    parameters : postvalues,
                    onFailure : handleFailure
                  };

    new Ajax.Request(url, options);
}

function inviteChat() {
    var url = baseurl+"/chat/updatechatroom/";
    clearTimeout(timeoutID);
    var options = {
                    method    : 'post',
                    onSuccess : inviteSuccess,
                    onFailure : handleFailure
                  };

    new Ajax.Request(url, options);
}

function addChat(type, chat_id) {
	if (type == 'ok') {
    	divelement = document.getElementById(chat_id);
    	parentelement = divelement.parentNode;
    	parentelement.removeChild(divelement);
        window.open(baseurl + "/chat/connect/chat_id/" + chat_id);
	} else if (type == 'cancel') {
        var url = baseurl+"/chat/refused/";
        clearTimeout(timeoutID);
        setTimeout("startTimer()", 0);
        var options = {
                    method    : 'post',
                    onSuccess : refusedSuccess,
                    onFailure : handleFailure,
                    parameters : 'chat_id=' + chat_id
                  };

        new Ajax.Request(url, options);
	}
    return false;
}

function newPage(ln) {
	var selectform = document.getElementById('selectForm');
	selectform.action = ln.href;
	selectform.submit();
    return false;
}

function addSmileys(filename) {
	var areaElement = document.getElementById('text');
    areaElement.value += '[:' + filename + ':]';
	return false;
}

function verificationOrder(id) {
    var url = baseurl+"/payments/process/";
    clearTimeout(timeoutID);
    var options = {
                    method    : 'post',
                    onSuccess : inviteVerification,
                    onFailure : handleFailure,
                    parameters : 'order_id=' + id
                  };

    new Ajax.Request(url, options);
}

function inviteVerification(transport) {
    data = transport.responseText.evalJSON(true);
    if (data.order_status == 'unknown') {
    	timeoutID=setTimeout("verificationOrder(" + data.id + ")", timerInterval);
    } else {
        divelement = document.getElementById("order_status");
        divelement.innerHTML = data.order_status;
    	if (data.message != '') {
    		divelement = document.getElementById("message");
        	divelement.innerHTML = data.message;
    	}
    }
}
