var noValue = '-99';
var curOption = new Array();
var isLoaded = new Array();

//обработчик кнопок FG1...3
function ChangeStatusToGL(response){
    var arr = response.split('|');
    document.getElementById('togl1_'+arr[1]).style.backgroundColor = '#EEE';
    document.getElementById('togl2_'+arr[1]).style.backgroundColor = '#EEE';
    document.getElementById('togl3_'+arr[1]).style.backgroundColor = '#EEE';
    document.getElementById(arr[0]+arr[1]).style.backgroundColor = '#7F7';
    return true;
}

function SendFormToPHP(form){
    var request = ''; var write = false;
    for(var i=0; i<form.elements.length; i++){
	write = false;
	if(/*form.elements[i].type != 'hidden' && */form.elements[i].type != 'button' && form.elements[i].type != 'submit'){
	    if(form.elements[i].type == 'radio' || form.elements[i].type == 'checkbox'){
		if (form.elements[i].checked) write = true;
	    }else write = true;
	}
	if (form.elements[i].value == '' || form.elements[i].value == '-' || form.elements[i].name == '') write = false;
	if (write) request += form.elements[i].name+'='+form.elements[i].value.replace(/"/g, '|kvchk|') + ' ';
    }
    //alert(request);
    jsrsExecute('nl_select.php', NothingToDo, 'VisaOrderForm', request);
}

function NothingToDo(){
    return true;
}

function ShowMessage(message){
    alert(message);
    return true;
}

//добавляем/удаляем из списка профайлов
function AddRemoveFromUserTypes(response){
    var arr = response.split('|');
    if (response == '-1|-1|-1' || response == ''){ alert('Error!'); return false; }
    button = document.getElementById('M'+arr[0]+'_'+arr[1]);
    if (arr[2] == '1') button.style.backgroundColor = '#EEE';
    if (arr[2] == '2') button.style.backgroundColor = '#F55';
    return true;
}

//после добавления/удаления фоты в/из слайдшоу
function ChangePhotoSlideshow(response){
    var id = response.split('|');
    if (id[0] == '-1') alert(id[1]);
    else{
        var photo = document.getElementById('imgphoto'+id[0]);
        if (photo.className != 'present') photo.className = 'present';
        else photo.className = '';
    }
    return true;
}

function fillAvailUsername(response){
    document.getElementById('travailusernames').style.display = 'none';
    if ((response != 'OK') && (response != 'NOTALLOWED')){
	InputArr = response.split('|');
	var oTd = document.getElementById('availusernames');
	for( var i = 0; i < InputArr.length; i++ ){
	    if (i == 0){
		document.getElementById('username1').value = InputArr[i];
		checked = 'CHECKED';
	    }else checked = '';
	    oTd.innerHTML += '<input type="radio" onclick="document.getElementById(\'username1\').value=this.value;" '+
		'name="avail_username" value="'+InputArr[i]+'" '+checked+'>';
	    oTd.innerHTML += InputArr[i];
	    oTd.appendChild(document.createElement('BR'));
	}
	document.getElementById('availtitle2').style.display = 'none';
	document.getElementById('availtitle3').style.display = 'none';
	document.getElementById('availtitle1').style.display = 'inline';
    }else if (response == 'NOTALLOWED'){
	document.getElementById('availusernames').innerHTML = '';
	document.getElementById('availtitle1').style.display = 'none';
	document.getElementById('availtitle2').style.display = 'none';
	document.getElementById('availtitle3').style.display = 'inline';
	document.getElementById('username1').value = '';
    }else{
	document.getElementById('availusernames').innerHTML = '';
	document.getElementById('availtitle1').style.display = 'none';
	document.getElementById('availtitle3').style.display = 'none';
	document.getElementById('availtitle2').style.display = 'inline';
    }
    //alert(oTr.style.display);
    return true;
}

function fillList( listName, strLot )
{   // fill any list with options
    if(listName=='state')
    {
	document.forms['formpost'].city1.style.display='none';
//	document.forms['formpost'].zipcode.style.width=100;
//	document.forms['formpost'].city1.style.width=0;
	document.forms['formpost'].zipcode.style.display='inline';
//	document.forms['formpost'].lzip_city.value='Zip/Postal code';
	document.getElementById('field_city').innerHTML='Zip/Postal code*';		
    }
    else if(listName=='message')
    {
	var aOptional = strLot.split('|');
	var aLot = aOptional[1].split('~');

//	document.forms['post'].setMode();
//	alert(HTMLArea);
//	HTMLArea.setMode();
//	alert(top.frames['message_iframe'].document.body.innerHTML);
	if( top.frames['message_iframe'] )
	{
	    if( top.frames['message_iframe'].document.body.innerHTML.trim() != '<br>')
		top.frames['message_iframe'].document.body.innerHTML += aLot[0]+'<br>';
	    else
	    {    
		top.frames['message_iframe'].document.body.innerHTML = aLot[0] + '<br>';    
	    }    
	}
	if( document.forms['post'] )    
	{
	    document.forms['post'].message.value+=aLot[0]+'\n';
	    document.forms['post'].subject.value+=((document.forms['post'].subject.value.length>1) ? ', ' : '') + aLot[1];	    
	}    
//	alert(document.forms['post'].message.value);
	return 1;	
    }
    else 
    {
//	alert('test');
	document.forms['formpost'].zipcode.style.display='none';
//	document.forms['formpost'].zipcode.style.width=0;
	document.forms['formpost'].zipcode.value='';
//	document.forms['formpost'].city1.style.width=152;
	document.forms['formpost'].city1.style.display='inline';
//	document.forms['formpost'].lzip_city.value='City';
	document.getElementById('field_city').innerHTML=city_name;		
    }
    listName='city1';
    emptyList( listName );
    // always insert selection prompt
    var lst = document.forms['formpost'][listName];
    lst.disabled = true;
    lst.options[0] = new Option('!!! Not Found !!!', noValue);
    // options in form "value~displaytext|value~displaytext|..."
    var aOptionPairs = strLot.split('|');
    lst.options[0] = new Option("-------", 0);
    for( var i = 0; i < aOptionPairs.length; i++ )
    {   if (aOptionPairs[i].indexOf('~') != -1) 
	{   var aLot = aOptionPairs[i].split('~');
	    lst.options[i+1] = new Option(aLot[1], aLot[0]);
	}  
    }
    // init to no value
    selectOption( listName, noValue );
    isLoaded[listName] = true;
    lst.disabled = false;
}

function emptyList( listName )
{  var lst = (document.forms['formpost']) ? document.forms['formpost'][listName] : null; 
//    var lst = document.forms['formpost'][listName];
    if(lst)
    {
	lst.options.length = 0;
    }	
//    lst.onchange = null;
    isLoaded[listName] = false;
    curOption[listName] = noValue;
}

function selectOption( listName, optionVal )
{   // set list selection to option based on value
    var lst = document.forms['formpost'][listName];
    for( var i = 0; i< lst.options.length; i++ )
    {  if( lst.options[i].value == optionVal )
	{   lst.selectedIndex = i;
    	    curOption[listName] = optionVal;
    	    return;
	}  
    }
}

function body_onLoad()
{
   emptyList('city1');
//   emptyList('state');
}

function cbFill_city(strStocks)
{   
    window.status = '';
    fillList('city1',strStocks); 
}

function cbFill_state(strStocks)
{   window.status = '';
    fillList('state',strStocks);
}

function cbFill_message(strStocks)
{   window.status = '';
    fillList('message',strStocks);
}
function cbPostMessage()
{
    window.status = '';
    alert('ready');
}    
