var W3CDOM = (document.createElement && document.getElementsByTagName);
var jsReady = false;
var swfReady = false;
var sender_id;
// ------- functions called by ActionScript -------
// called to check if the page has initialized and JavaScript is available
function isReady()
{
  return jsReady;
}

// called by the onload event of the <body> tag
function pageInit()
{
  // Record that JavaScript is ready to go.
  jsReady = true;  
}

function setSWFIsReady(s_id)
{
  swfReady = true;
  sender_id = s_id;
}

function getSWF(movieName)
{
  if (navigator.appName.indexOf("Microsoft") != -1)
  {
    return window[movieName];
  }
  else
  {
    return document[movieName];
  }
}

function closeChatWindowListener() {
	if (navigator.appName.indexOf("Microsoft") != -1)
	{
	    window.onunload = finish;
	}
	else
	{
	    document.onunload = finish;
	}
	//alert("closeChatWindowListener");
	return 1;
}

function finish() {	
	var callResult = getSWF("player").userShutDown();
	return true;
}

var count_login_show = 0;
function UpdateLoginForm(){
  count_login_show++;  
  //xajax_update_login_form();
  UpdateLoginFormTimeout = setTimeout('UpdateLoginForm();', 1000);
  if (count_login_show == 30)
  {
    clearTimeout(UpdateLoginFormTimeout);
  }
  return false;
}

function UpdatePhoneContent(){
  var sender_id = getParamValue('id');
  xajax_update_phone_content(sender_id);
  UpdatePhoneContentTimeout = setTimeout('UpdatePhoneContent();', 10000);
  return false;
}
function UpdateChatPrices(){
  var sender_id = getParamValue('id');
  xajax_update_chatprice(sender_id);
  UpdateChatPricesTimeout = setTimeout('UpdateChatPrices();', 10000);
  return false;
}
function UpdateStatusChat(sender_id){
	//var sender_id = getParamValue('id');
	xajax_update_status_chat(sender_id);
	xajax_update_setacrd_headonline(sender_id);
	UpdateStatusTimeout = setTimeout('UpdateStatusChat('+sender_id+');', 5000);
	return false;
}


function getParamValue(paramName)
{
  var queryStringPairs = document.location.search.substr(1).split('&');
  for (i = 0; i < queryStringPairs.length; i++)
  {
    var parts = queryStringPairs[i].split('=');
    if (parts[0] == paramName)
      return parts[1];
  }
  return '';
}
 
function initFileUploads() {
	if (!W3CDOM) 
	{	   
	    return;
	}
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	fakeFileUpload.appendChild(document.createElement('input'));
	var image = document.createElement('img');
	//image.src='online_status.png';
	image.src='online_status2.png';
	fakeFileUpload.appendChild(image);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
	//alert (fakeFileUpload.innerHTML);
}


function showUserClient(ArrArg, divID) {
	// <![CDATA[
	var fo = new FlashObject("videochat/UserClient.swf", "player", "946px", "339px", "9", "#FFFFFF");
	fo.addParam("allowFullScreen", "true");
	var pStr = "embedded=1&phpUrl="+ArrArg['phpUrl'];
	if (ArrArg['chat_type'] == null) 
	{
	    ArrArg['chat_type'] = 2;
	}
	pStr = pStr+"&chat_type="+ArrArg['chat_type']+"&sender_id="+ArrArg['sender_id']+"&member_id="+ArrArg['member_id'];
	if (ArrArg['pin']!= null) 
	{
	    pStr = pStr + "&pin="+ArrArg['pin'];
	}
	fo.addParam("FlashVars", pStr);
	fo.addParam("swLiveConnect", "true");
	fo.addParam("allowScriptAccess", "true");
	fo.addParam("quality", "hight");
	fo.addParam("scale", "noscale");
	fo.addVariable("FlashVars", pStr);
	fo.addVariable("phpUrl", ArrArg['phpUrl']);
	fo.write(divID);	
}

function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

function init() {
	if (document.getElementById('header') == null) return;
	var header = document.getElementById('header');
	//header.onclick = function() {	location.href = './';	}
}

function validateEmail(email) {
	valid = /\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi;
	if (email.search(valid) == -1) return false;
	else return true;
}
				
function frm_register() {	
	/*
	if (document.getElementById('frm_register') == null) return;
	var frm = document.getElementById('frm_register');
	frm.button.onclick = function() {
		var error = false;
		if (frm.first_name.value == '' || frm.last_name.value == '' || frm.name.value == '' || frm.login.value == '' || frm.email.value == '' || frm.password.value == '') {
			alert('Please fill out the fields!');
			error = true;
		}
		if (!validateEmail(frm.email.value)) {
			alert('Please enter a valid e-mail address!');
			error = true;
		}
		if (frm.password.value != frm.password_confirm.value) {
			alert('Passwords do not match!');
			//alert('Please enter a valid password!');
			error = true;
		}
		if (frm.email.value != frm.email_confirm.value) {
			alert('Emails do not match!');
			error = true;
		}

		
		if (!error) {
			frm.submit();
		}
	}
	*/	
}

function openWin(url,w,h,resize,close_self, name) {
	var wint = (screen.height-h)/2;
	var winl = (screen.width-w)/2;
	var win  = window.open(url, name ? name : 'myWindow','width=' + w + ',height=' + h + ',resizable=' + resize + ',location=0,scrollbars=0,menubar=0,status=1,top=' + wint + ',left=' + winl);
	
}

function openWinSF(url, w, h)
{
  var winwidth = screen.availWidth*0.7;
  var winheight = screen.availHeight*0.9;
  var leftt=Math.floor((winwidth/2)-(w/2));
  var topt=Math.floor((winheight/2)-(h/2));  
  window.resizeTo(w,h);
  window.moveTo(leftt,topt);
  document.location=url;  
  }
  
function redir(act) {
	opener.location.href = './?act=' + act;
	self.close();
}


addEvent(window, 'load', init); 
addEvent(window, 'load', UpdateLoginForm); 
addEvent(window, 'load', frm_register); 
initFileUploads();
