// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


document.observe("dom:loaded", function() {
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body);

  if (container) {

     container.observe('click', function(e) {
      var el = e.element();
      if (el.match('.pagination a')) {
        document.getElementById('to_render_comm').innerHTML = "<div id='loader_div' class='ajaxloader'><img src='/images/basic/gray_loader.gif'/></div>";
        new Ajax.Request(el.href, { method: 'get' });
        e.stop();
      }
    })
  }
})


function ClearEdit(){
	elem = document.getElementById('editplace');
	if (elem != null)
		elem.innerHTML = '';
}

function MoveLogoRight(){	
	elemA = document.getElementById('showLogoTwo');
	elemB = document.getElementById('logoOnRight');
	if (elemA.style.display == 'none'){
		elemA.style.display = 'block';
		elemB.style.display = 'none';
	}
	else{
		elemA.style.display = 'none';
		elemB.style.display = 'block';
	}
}

function SwitchBackground(backId, img){
	elem1 = document.getElementById(backId);
	if (elem1 != null)
		elem1.style.backgroundImage = "url('/images/basic/" + img + ".png')";
	elem2 = document.getElementById('formBackgroundTwo');
	if (elem2 != null)
		elem2.style.backgroundImage = "url('/images/basic/" + img + ".png')";
	document.getElementById('survey_body_bgpattern').value = img;
}

function ShowOrHide(id){
	elem = document.getElementById(id);
	if (elem.style.display == 'none')
		elem.style.display = 'block';
	else
		elem.style.display = 'none';
}

function SelectChanged(){
	elem = document.getElementById('surPass');
	if (document.getElementById('survey_login_type').value == 3){
		elem.style.display = 'block';
		elem.style.display = 'table-row';
	}
	else
		elem.style.display = 'none';
}

function GetQueryString(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function ScrollMenu(id){
	el = document.getElementById(id);
	
	if (el != null){

		var vscroll = (document.all ? document.documentElement.scrollTop : window.pageYOffset);
		new Effect.Move(id, { x: 0, y: vscroll, mode: 'absolute' });
	}
	return false;
}

function SelectAllChb(id_part, max, me){
	mchb = document.getElementById(me);
	
	if (mchb.checked == true){
		toset = true;
	}
	else{
		toset = false;
	}
	
	for (var i=0; i<max; i++){
		newid = id_part + i;
		elem = document.getElementById(newid);
		if (elem != null)
			elem.checked = toset;
	}
	
}

function AddExistingUsers(id_part, max, to_take_id_part){
	var IDs = '', newid, elem, val_elem, cnt = 0;
	for (var i=0; i<max; i++){
		newid = id_part + i;
		elem = document.getElementById(newid);
		if (elem != null){
			if (elem.checked == true){
				val_elem = document.getElementById(to_take_id_part + i);
				if (val_elem != null){
					IDs += val_elem.value + ',';
					cnt = cnt + 1;
				}
			}
		}
	}
	if (IDs != ''){
		if (cnt == max)
			window.location = "/vendormappings/create/all";
		else
			window.location = "/vendormappings/create/" + IDs;
	}
	else{
		window.alert('You must select at least one respondent.');
	}
}

function SetEditTextBox(id, hide_id, show_id){
	elem = document.getElementById(id);
	elem.disabled = false;
	elem.style.background = '#ffffff';
	elem.focus();
	elem.select();
	
	hide = document.getElementById(hide_id);
	hide.style.display = 'none';
	
	show = document.getElementById(show_id);
	show.style.display = 'block';
}

function SetEditTextBoxForm(editElement, id, hide_id, show_id){
	SetEditTextBox(id, hide_id, show_id);
	editElm = document.getElementById(editElement);
	if (editElm != null)
		editElm.style.display = 'block';
}




function SetNewTextBox(id, hide_id, show_id, foc_id){
	elem = document.getElementById(id);
	elem.style.display = 'block';
	
	foc = document.getElementById(foc_id);
	foc.focus();
	
	hide = document.getElementById(hide_id);
	hide.style.display = 'none';
	
	show = document.getElementById(show_id);
	show.style.display = 'block';
}


function ChbChanged(toshow, chb){
	if (chb.checked == true){
		document.getElementById(toshow).style.display = 'block';
		document.getElementById(toshow).style.display = 'table-row';
		if (toshow == 'rurl')
			document.getElementById('survey_endtype').value = 1;
	}
	else
	{
		document.getElementById(toshow).style.display = 'none';
		if (toshow == 'rurl')
			document.getElementById('survey_endtype').value = 0;
	}
}

function RadioChange(id_a, id_b, hiddenChange){
	setVal = document.getElementById(hiddenChange);
	
	to_show = document.getElementById(id_a);
	to_show.style.display = 'block';
	to_show.style.display = 'table-row';
	
	to_hide = document.getElementById(id_b);
	to_hide.style.display = 'none';	
	
	if (id_a == 'finish' || id_a == 'date')
		setVal.value = 1;
	else
		setVal.value = 2;
}

function ManipulateIndexCalTo() {
	document.getElementById('kalendar').style.display = 'block';
	cal_to_update = 'survey_duedate';
	a_cal_to_update = 'acalto';
}

function ManipulateCalImg(cnt,mon,year) {
	document.getElementById(a_cal_to_update).innerHTML = '<br/>' + cnt;
	document.getElementById(a_cal_to_update).style.background = "url('/images/calendar/days/" + mon + ".gif')";
	document.getElementById(cal_to_update).value = year;
	document.getElementById('kalendar').style.display = 'none';
}


 function SwitchDivs(div_one, div_two){
	elem_one = document.getElementById(div_one);
	elem_two = document.getElementById(div_two);
	
	if (elem_one.style.display == 'none'){
		elem_one.style.display = 'block';
		elem_two.style.display = 'none';
	}
	else
	{
		elem_one.style.display = 'none';
		elem_two.style.display = 'block';
	}
 
 }
 
function Show(id){
	document.getElementById(id).style.display = 'block';
}

function ColorDiv(id, color){
	document.getElementById(id).style.background = color;
}

function Close(id){
	document.getElementById(id).style.display = 'none';
}

function AssignValue(id, val){
	document.getElementById(id).value = val;
}

function ShowMandatory(id){
	if (id == 1 || id == 2 || id == 4 || id == 6 || id == 8)
		$('mandatory').show();
	else
		$('mandatory').hide();
}
 
function FocusAndClear(id){
	document.getElementById(id).focus();
	document.getElementById(id).value = '';
}

function Clear(id){
	document.getElementById(id).value = '';
}  

function Focus(id){
	document.getElementById(id).focus();
} 




function ChangeCodeChoice(ablock, show_id){
	var thechoice = document.getElementById( "code_choice" );
	var aArr = thechoice.getElementsByTagName( "a" );

	for(var i=0; i < aArr.length; i++)
	{
		var tag = aArr.item(i); 
		tag.className = '';
	}
	
	ablock.className = 'selected';
	if (show_id == 'embed')
	{
		Show('embed');
		Close('link');
		Close('email');
		Close('popup');
	}
	else if (show_id == 'link')
	{
		Show('link');
		Close('embed');
		Close('email');
		Close('popup');
	}
	else if (show_id == 'email')
	{
		Show('email');
		Close('link');
		Close('embed');
		Close('popup');
	}
	else if (show_id == 'popup')
	{
		Show('popup');
		Close('link');
		Close('email');
		Close('embed');
	}
	
	
}



var msgAnchor = null;
function showMsg(id,anchor,title, totheleft)
{
  if(msgAnchor == anchor)
  {
    hideMsg();
  }
  else
  {
    msgAnchor = anchor;
    sn = $('homeMsg');
    sn.innerHTML = '<p><b>'+ title +'</b><br/>'+ $(id).innerHTML +'</p>';
    sn.style.top = findY(anchor) + 16 + 'px';
	if (totheleft != null)
		sn.style.left = findX(anchor) - totheleft + 'px';
	else
		sn.style.left = findX(anchor) + 'px';
    sn.style.display = 'block';
  }
}

function hideMsg()
{
  sn.style.display = 'none';
  sn.style.top = '-1000px';
  sn.style.left = '-1000px';
  msgAnchor = null; 
}

function findX(obj){
  var curleft = 0;
  if(obj.offsetParent)
    while(1) 
    {curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;}
  else if(obj.x)
    curleft += obj.x;
  return curleft;
}

function findY(obj){
  var curtop = 0;
  if(obj.offsetParent)
    while(1)
    {curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;}
  else if(obj.y)
    curtop += obj.y;
  return curtop;
}









