
/* YMP - Basic JavaScript functions */

function OpenMap(locationx, locationy)
{

  window.open("kartta/?selx="+locationx+"&sely="+locationy, "mapwindow", 'width=416,height=354');

}

function UpdateLocation(locationx, locationy)
{

  opener.document.mainedit.locationx.value = locationx;
  opener.document.mainedit.locationy.value = locationy;
  self.close();

}

function AddPic(id, type)
{

  window.open("kuvat/?items_id="+id+"&kuva="+type, "picwindow", 'width=400,height=160');

}

function UpdatePic(id, type)
{

  var now = new Date();

  if (type == "ulkokuva")
  {

    opener.document.images.ulkokuva.src = '/admin/mokit/muokkaus/showimage.php?file=' + type + '/' + id + '_thumb.jpg&' + now.getTime();  

  }

  if (type == "sisakuva")
  {

    opener.document.images.sisakuva.src = '/admin/mokit/muokkaus/showimage.php?file=' + type + '/' + id + '_thumb.jpg&' + now.getTime();  

  }

  if (type == "pohjapiirros")
  {

    opener.document.images.pohjapiirros.src = '/admin/mokit/muokkaus/showimage.php?file=' + type + '/' + id + '_thumb.jpg&' + now.getTime();  

  }
  
  if (type == "picture04")
  {

    opener.document.images.picture04.src = '/admin/mokit/muokkaus/showimage.php?file=' + type + '/' + id + '_thumb.jpg&' + now.getTime();  

  }

  if (type == "picture05")
  {

    opener.document.images.picture05.src = '/admin/mokit/muokkaus/showimage.php?file=' + type + '/' + id + '_thumb.jpg&' + now.getTime();  

  }
  
  if (type == "picture06")
  {

    opener.document.images.picture06.src = '/admin/mokit/muokkaus/showimage.php?file=' + type + '/' + id + '_thumb.jpg&' + now.getTime();  

  }
  
  self.close();

}

function AddPagesPic(id)
{

  window.open("kuvat/?items_id="+id, "picwindow", 'width=400,height=160');

}

function UpdatePagesPic(id)
{

  var now = new Date();

  opener.document.images.kuva.src = '/admin/sisalto/showimage.php?id=' + id + '&' + now.getTime();  

  self.close();

}

function PriceWeeks(id)
{

  window.open("hinnat/?items_id="+id, "pricewindow", 'width=900,height=400,scrollbars');

}

function changeMapSelection(sel) {
    var selection;
	if (sel != '') {		
		document.getElementById('map_all').style.display = 'none';
		sel += ",";
		selection = sel.split(",");

		for(var i=0; i< selection.length; i++)
		if (selection[i]) {			
	        	dspl = document.getElementById("mapchoise_" + selection[i]).value == ""?true:false;
	        	document.getElementById("mapchoise_" + selection[i]).value = dspl?"1":"";
	        	document.getElementById('map' + selection[i]).style.display = dspl?"block":"none";	        	
		}	
			
		if (selection.length == 2) { // single select						
			document.getElementById('province').value = selection[0]					
		}
					
	}
}

function selectmap_onchange(sel) {
    var selection;
	if(sel=="0") {				
		document.forms[0].elements['province'].value = 0;	
	    	for(i=1; i<=13; i++) {
		    	map = document.getElementById('map'+(i))
		    	if (map) {
		    		map.style.display = "none";
		    		document.getElementById('mapchoise_'+(i)).value = ''		    		
		    	}	        	    
    		}    	
    		
	} else {		
		choise = document.getElementById("mapchoise_" + sel);
		map = document.getElementById('map' + sel)        
	        if (choise.value == 1) {
			choise.value = 0			
			map.style.display = 'none'
		} else {
			choise.value = 1			
			map.style.display = 'block'	
		}		    	
	}	
}


function submitForm() {
    var selection = new Array();
    for(var i=1; i<=13; i++) {
    	choise = document.getElementById("mapchoise_" + i)
    	if (choise) {
        	isOn = choise.value == "1"?true:false;
        	if(isOn) 
            		selection.push(i);
        }	
    }

    document.forms[0].elements["provinceIds"].value = selection.join(",");
    document.forms[0].elements["evName"].value = "clean";
    document.forms[0].submit();
}

function fireEvent(evName,evArgs,form){
	if (form==null) form=0; 
	var name = document.forms[form].elements['evName'];
	var arg = document.forms[form].elements['evArgs'];
	var type = document.forms[form].elements['evType'];

	if (type.value == '') {
		type.value='DESC';
	} else {
		type.value='';
	}

	if ( name && arg ) { 
		name.value=evName;
		arg.value=evArgs;
		document.forms[form].submit();
	}
}

function submitAction(itemId, action) {
	document.getElementById('fAction').name = action;
    document.forms[0].elements["items_id"].value = itemId;
    document.forms[0].submit();
}


function print_view(content,title,css_path, prn_label) {
	win = window.open("", 'popup', 'toolbar = no, status = no, width=900,height=800, left=100,top=10, screenX=100, screenY=10, resizable=yes');
	win.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	win.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">');
	win.document.writeln('<header>');
	win.document.writeln('<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />');
	win.document.writeln('<title>' +  title + '</title>');	
	win.document.writeln('<link rel="stylesheet" type="text/css" href="' + css_path + '" media="all" />');
	win.document.writeln('<style type="text/css">');
	win.document.writeln('	body {');		
	win.document.writeln('		background-color: white;');
	win.document.writeln('		margin: 0;');
	win.document.writeln('		padding: 0;');
	win.document.writeln('	}');	
	win.document.writeln('	#content {');
	win.document.writeln('		border: 0px;');
	win.document.writeln('		background-image: none;');	
	win.document.writeln('	}');
	win.document.writeln('</style>');

	win.document.writeln('</header>');
	win.document.writeln('<body>');

	win.document.writeln('<script>');
	win.document.writeln('	 if (window.Event)');
	win.document.writeln('		return true;');
	win.document.writeln('</script>');

	win.document.writeln('<div id="content">');
	win.document.writeln(content);
	win.document.writeln('<input type="button" onclick="window.print();" value="' + prn_label + '">');
	win.document.writeln('</div>'); 

	win.document.writeln('</body>');
	win.document.writeln('</html>');
	win.document.close();	
}



function getXMLHttpRequestObject(){
    var xmlobj;
    // check for existing requests
    if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
        xmlobj.abort();
    }
    try{
        // instantiate object for Mozilla, Nestcape, etc.
        xmlobj=new XMLHttpRequest();
    }
    catch(e){
    	document.getElementsByTagName('form')[1]
        try{
            // instantiate object for Internet Explorer
            xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch(e){
            // Ajax is not supported by the browser
            xmlobj=null;
            return false;
        }
    }
    return xmlobj;
}

// instantiate email XMLHttpRequest object
var emailXMLHttpObj=getXMLHttpRequestObject();


function sendEmailRequest(){	
	
    // open socket connection
    emailXMLHttpObj.open('POST','http://www.finntrip.fi/includes/sendmail.php',true);
    // set form http header
    emailXMLHttpObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=iso-8859-1');
	// send http request
	var message=document.getElementsByTagName('form')[0].elements['message'].value;			
	var subject=document.getElementsByTagName('form')[0].elements['subject'].value;
	
	emailXMLHttpObj.send('message=' + message + '&subject=' + subject);	
   	  
// 	emailXMLHttpObj.onreadystatechange=emailStatusChecker;
}

function emailStatusChecker(){	
	// if mail request is completed
    if(emailXMLHttpObj.readyState==4){    	
        if(emailXMLHttpObj.status==200){
            // if status == 200 display server response
            displayServerResponse();          
        }
        else{
            alert('Failed to get response :'+ emailXMLHttpObj.statusText);
        }
    }
}


// display server response
function displayServerResponse(){
    var status=document.getElementsByTagName('h1')[0].firstChild;
    if(!status){return};
    // display messages by <h1> header
    status.data=emailXMLHttpObj.responseText; 
}










