
//Load XSL
var xsl = new ActiveXObject('Microsoft.XMLDOM');
xsl.async = true;
xsl.load('http://www.travel-shot.com/ajax_php/statesDisplay.xsl');

function isAvailable(response){    
    var usernameField = document.getElementById("username");
    var username = usernameField.value;
    username = username.toLowerCase();
    
    //username = username.trim();
    var availability = document.getElementById("availability");
    if (username != '') {
    	
        usernameField.style.backgroundImage = 'url(http://www.travel-shot.com/images/loading.gif)';
        if (response != '') {
            usernameField.style.backgroundImage = 'none';
            if (response == 'false') {
                availability.className = 'bold avGreen';
                availability.innerHTML = 'Available';
            } else {
                availability.className = 'bold avRed';
                availability.innerHTML = 'Not Available';
            }
            usernameField.style.backgroundImage = '';
        } else {
            loadXMLDoc('http://www.travel-shot.com/ajax_php/ajaxIsAvailable.php?username=' + username, 'processStateChange');
        }
    }
}

function votingToMovie(pVid, pRating)
{
	loadXMLDoc('http://www.travel-shot.com/ajax_php/ajaxVotingToMovie.php?vid=' + pVid + '&rating=' + pRating, 'processVotingTo');
}

function processVotingTo()
{
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
        response = xmlhttp.responseXML.documentElement;
        newRate = response.getElementsByTagName('new_rating')[0].firstChild.data;
        document.getElementById('stars_of_rating').innerHTML = getStarsRate(newRate);
        document.getElementById('text_of_rating').innerHTML = '<b>Thanks for rating!</b>';
	}
}

function votingToPicture(pPid, pRating)
{	
	loadXMLDoc('http://www.travel-shot.com/ajax_php/ajaxVotingToPicture.php?pid=' + pPid + '&rating=' + pRating, 'processVotingTo');
}

var stateName = "";
function getStateListByLand(pSpan, pSelected, pSize)
{
	var countryField = document.getElementById('country_field');
    var countryValue = countryField.value;
    if (countryValue == 0)
    {
    	alert('please select country');
    }
    
    var landField = document.getElementById('location_land');
    var value = landField.value;
    if ((value != "" && countryValue != 0) || (pSelected != '' && pSelected != '0'))
    {
    	document.getElementById('search_button').disabled=true;
    	landField.style.backgroundImage = 'url(http://www.travel-shot.com/images/loading.gif)';
    	//alert('value=' + value + '&span_name=' + pSpan + '&value_field=location_land&country=' + countryValue + '&selected=' + pSelected + '&size=' + pSize);   	
    	loadXMLDoc('http://www.travel-shot.com/ajax_php/ajaxGetStateListByLand.php?value=' + value + '&span_name=' + pSpan + '&value_field=location_land&country=' + countryValue + '&selected=' + pSelected + '&size=' + pSize, 'processgetStateListByLand');
    }
    
}

var xmlhttp;
function loadXMLDoc(url, pResponseProcess)
{
    // Native
    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
        eval('xmlhttp.onreadystatechange = ' + pResponseProcess);
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
    else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp) {
            eval('xmlhttp.onreadystatechange = ' + pResponseProcess);
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }
    }
}

function processStateChange()
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        response = xmlhttp.responseXML.documentElement;
        method = response.getElementsByTagName('method')[0].firstChild.data;
        result = response.getElementsByTagName('result')[0].firstChild.data;
        eval(method + '(result)');
    }
}

function processgetStateListByLand()
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
    	var xmlDoc = xmlhttp.responseXML;
    	
    	var tagMain = xmlDoc.getElementsByTagName("state_list");
    	
    	var resultElement = document.getElementById(tagMain[0].getAttribute("span_name"));
    	var valueFields = document.getElementById(tagMain[0].getAttribute("value_field"));
    	valueFields.style.backgroundImage = '';
    	document.getElementById('search_button').disabled=false;
    	
    	if (window.ActiveXObject)
		{
			//Transform
			var transform = xmlDoc.transformNode(xsl);
			resultElement.innerHTML = transform;
		}
		else
		{
			var xsltProcessor = new XSLTProcessor();
			
			//Load XSL
			XObject = new XMLHttpRequest();
			XObject.open('GET', 'http://www.travel-shot.com/ajax_php/statesDisplay.xsl', false);
			XObject.send(null);
			xslStylesheet = XObject.responseXML;
			xsltProcessor.importStylesheet(xslStylesheet);
			
			//Transform
			var fragment = xsltProcessor.transformToFragment(xmlDoc, document);
			resultElement.innerHTML = "";
			resultElement.appendChild(fragment);
			
		}
		
		//Clear the object and call the getDocument function in 10 seconds
		xmlhttp.abort();
		//setTimeout('getDocument()', 10000);
		/*
		if (selected != '')
		{
			document.getElementById(stateName).selectedIndex = selected; 
		}*/
	}
}	
