// Copyright (c) 2005 Timothy R. Morgan
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// mini/ajax.js - http://timmorgan.org/mini
function VPmodalDialogShow(url,width,height)
{
			if(navigator.appName.indexOf('Microsoft')!=-1){
			// activeModalWin=
			 	 window.showModalDialog(url,window,
					"dialogWidth:"+width+"px;dialogHeight:"+height+"px;edge:Raised;center:Yes;help:No;Resizable:Yes;Maximize:Yes");
			  // document.adform2.src.value=activeModalWin
			}else {
				var left = screen.availWidth/2 - width/2;
				var top = screen.availHeight/2 - height/2;
				activeModalWin = window.open(url, "", "width="+width+"px,height="+height+",left="+left+",top="+top+",resizable=yes");
				window.onfocus = function(){if (activeModalWin.closed == false){activeModalWin.focus();};};
			};
}
function setAssetValue(v) 
{
	document.getElementById("src").value = v;
}

function ajax_item(e){if(typeof e=='string')e=document.getElementById(e);return e};
function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};

ajax={};
ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
ajax.serialize=function(f)
  {
  var g=function(n)
    {
    return f.getElementsByTagName(n)
    };
  var nv=function(e)
    {
    if(e.name)
      {
      return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);
      } else {return '';}
    };
  var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');
  };
  
ajax.send=function(u,f,m,a){var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
ajax.get=function(url,func){ajax.send(url,func,'GET')};
ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};
ajax.update=function(url,elm){var e=ajax_item(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)};
ajax.submit=function(url,elm,frm){var e=ajax_item(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};



var vietpro_var_cookieid = "";
var vietpro_var_cookie_domain = "";
var vietpro_var_cookie_path   = "/";
var vietpro_login_click = 0;
var vietpro_login_status = 1;
var vietpro_request_sent = 0;
var vietpro_request_received = 0;
var startday = new Date();
var clockStart = startday.getTime(); 
var vietpro_tips = ""; 
var is_ie = 0;


var IE = document.all?true:false
	
if ( ! IE )
{
	document.captureEvents( Event.MOUSEMOVE );
}

document.onmousemove = getMouseXY;

var tempX = 0;
var tempY = 0;
	
function getMouseXY(e)
{
	if ( IE )
	{
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	}
	else
	{
	    tempX = e.pageX;
	    tempY = e.pageY;
	}

	if ( tempX < 0 )
	{
		tempX = 0
	}
	
	if ( tempY < 0 )
	{
		tempY = 0
	}
		
	return true;
}

function initStopwatch()   
{   
	var myTime = new Date();   
	var timeNow = myTime.getTime();    
	var timeDiff = timeNow - clockStart;   
	this.diffSecs = timeDiff/1000;   
	return(this.diffSecs);
}

if ( window.ActiveXObject )
{
	is_ie = 1;
}

function page_loading_html( text )
{
	
	return text;
}

function clear(id)
{	
document.getElementById( id ).innerHTML ="";
}
function page_loading(type)
{
	if ( type == 1 )
	{
		document.getElementById( 'displaypage' ).innerHTML = page_loading_html("Can't run ajax! plz reupload...");
	}
	else if ( type == 2 )
	{
		document.getElementById( 'displaypage' ).innerHTML = "";
	}
	else
	{
		document.getElementById( 'displaypage' ).innerHTML = page_loading_html("Loading!...");
	}
}


function vietpro_request(request, url, type, form_id, no_status)
{
	try
	{
		this.clicked = request;
		
		vietpro_request_sent++;
		update_status(0);
		
		this.no_status = no_status;
		
		if ( no_status == 1 )
		{
			no_status = 1;
		}
		else
		{
			no_status = 0;
		}
		
		if ( ! type && type!='post')
		{
			type = "GET";
		}
		else
		{
			type = "POST";
		}
		
		var fields_data = "";
		var lhl_data_got = "";

		if ( form_id )
		{
			if ( type == "GET" )
			{
				var fields_data = "?";
			}
			else if ( type == "POST" )
			{
				var fields_data = "";
			}

			var frm = document.forms[form_id];
			var numberElements = frm.elements.length;
					
			for( var i = 0; i < numberElements; i++ )
			{
				if( i < numberElements-1 )
				{
					if ( frm.elements[i].type == "select-one" )
					{
						fields_data += frm.elements[i].name+"="+frm.elements[i].value+"&";
					}
					else if ( frm.elements[i].type == "select-multiple" )
					{
						art_name = frm.elements[i].name;
						
						selmul = frm.elements[i];
		
						var seldt = new Array();
						var selcnt = 0;
						
						for ( var i = 0; i < selmul.length; i++ )
						{
							if ( selmul.options[i].selected == true )
							{
								seldt[selcnt] = selmul.options[i].value;
								selcnt++;
							}
						}
											
						fields_data += art_name+"="+seldt+"&";
					}
					else if ( ( frm.elements[i].type == "radio" ) || ( frm.elements[i].type == "checkbox" ))
					{
						if (frm.elements[i].checked)
						{
							fields_data += frm.elements[i].name+"="+frm.elements[i].value+"&";
						}
					}
					else
					{
						fields_data += frm.elements[i].name+"="+frm.elements[i].value+"&";
					}
				}
				else
				{
					if (frm.elements[i].type == "radio")
					{					
						if (frm.elements[i].checked)
						{
							fields_data += frm.elements[i].name+"="+frm.elements[i].value;
						}
					}
					else
					{
						fields_data += frm.elements[i].name+"="+frm.elements[i].value;
					}
				}
			}
			
		}


		http_request = false;

		this.lhl_id_packet_send_to = request;
		
		if ( lhl_id_packet_send_to )
		{
			if ( no_status != 1 )
			{
				page_loading();
			}
		}
		
		if (window.XMLHttpRequest) // Mozilla, Safari,...
		{
			http_request = new XMLHttpRequest();
			
			if (http_request.overrideMimeType)
			{
				http_request.overrideMimeType('text/xml');
			}
		
		}
		else if (window.ActiveXObject) // IE
		{
			try
			{
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
				
				}
			}
		}


		if ( ! http_request )
		{
			if ( lhl_id_packet_send_to )
			{
				page_loading(1);
			}

			return false;
		}
			
		http_request.onreadystatechange = contents;

		if ( type == "POST" )
		{
			http_request.open('POST', url, true);
			http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			http_request.setRequestHeader("Content-length", fields_data.length);
			http_request.setRequestHeader("Connection", "close");
			http_request.send(fields_data);
		}
		else if ( type == "GET" )
		{
			http_request.open('GET', url + fields_data, true);
			http_request.send(null);
		}
		runEditor();
	}
	catch (error)
	{
	}
}


function contents()
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			vietpro_request_received++;
			update_status(1);
				
			if ( lhl_id_packet_send_to )
			{
				if ( no_status != 1 )
				{
					page_loading(2);
				}
			
				vietpro_request_time = 1;
			
				document.getElementById( lhl_id_packet_send_to ).innerHTML = http_request.responseText;
			}
		}
		else
		{
			if ( lhl_id_packet_send_to )
			{
				if ( no_status != 1 )
				{
					page_loading(1);
				}
			}
		}
	}
}


function update_status(type)
{
	
	if ( document.getElementById( 'vietpro_request_sent' ) )
	{
		if ( type == 1 )
		{
			status_received = vietpro_request_received;
	
			document.getElementById( 'vietpro_request_received' ).innerHTML = status_received;
		}
		else
		{
			status_sent = vietpro_request_sent;
	
			document.getElementById( 'vietpro_request_sent' ).innerHTML = status_sent;
		}
	}
}


function contact_form()
{
	vietpro_request( "vietpro_contact","sendmessage.php?do=docontactus", "POST", "contact", 0 );
}


function ajaxSubmit( thisForm , actionForm )
{
	return false;
}

var css_selected=0;

function changeTimeColor( this_id )
{
		if ( this_id != css_selected )
		{
			document.getElementById( this_id ).className = "text_time2"; 
			try
			{
				if(css_selected)document.getElementById( css_selected ).className = "text_time"; 
			}
			catch (error)
			{
			}				
			css_selected = this_id;
		}
}

function ShowContacts(str){
		if(str!=true){		
			document.getElementById("datetime").style.display = "none";
		}
		else{
			document.getElementById("datetime").style.display = "";
		}
	}
