var NORMAL_STATE = 4;
var ADMIN_LOGIN = 'validadmin.php?';
var WORD_SAVE = 'saveword.php?';
var ADMIN_WORD_SAVE = 'manager/saveword.php?';
var WORD_SAVE_POST = 'saveword.php';


function getHTTPObject() {
 var request_o; //declare the variable to hold the object.

 // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        request_o = new XMLHttpRequest();
    // branch for IE/Windows ActiveX version
    }
 else if (window.ActiveXObject) {
        request_o = new ActiveXObject("Microsoft.XMLHTTP");
    }

 return request_o; //return the object
}

var http = getHTTPObject();

function TrimString(sInString) {
	sInString = sInString.replace( /^\s+/g, "" );
	return sInString.replace( /\s+$/g, "" );
}

function deleteclue(id)
{
var wordid= document.getElementById('wordid').value;
http.open('GET', WORD_SAVE + 'clueid=' + id + '&wordid=' + wordid + '&task=deleteclue', true);
http.onreadystatechange = handleHttpDeleteClue;
http.send(null);
}
function handleHttpDeleteClue()
{
	 if (http.readyState == NORMAL_STATE) {
	  document.getElementById("addclue").innerHTML=http.responseText;
		
	}
}

function makecurrentgame(clueval)
{
 var cid= document.getElementById('cid').value;
 var clues = clueval.split('#');
 var clueid = clues[0];
 var word = clues[1];
 http.open('GET', WORD_SAVE + 'clueid=' + clueid + '&cid=' + cid +'&word=' + word + '&task=savecluecrossgame', true);
 http.onreadystatechange = handleHttpValidCluecrossgame;
 http.send(null);
}

function getpendingchallenge()
{
var challange    =	document.getElementById('challange').value;
if(challange=='0')
	{
      //document.getElementById('chal').innerHTML = '';
	  document.getElementById('chal').style.visibility = 'hidden';
	 document.getElementById('chal').style.display = 'none';
	  document.getElementById('challange').options[0].selected = true;
	  return false;
	  
	}else
	{
		document.getElementById('chal').style.visibility = 'visible';
	    document.getElementById('chal').style.display = 'block';
	}
/*http.open('GET', WORD_SAVE +'&task=getchallengedgame', true);
http.onreadystatechange = handleHttpGetChallange;
http.send(null);*/
}
function handleHttpGetChallange()
{
  if (http.readyState == NORMAL_STATE) {
	 document.getElementById('chal').innerHTML = http.responseText;
	 document.getElementById('chal').style.visibility = 'visible';
	 document.getElementById('chal').style.display = 'block';
		}
}

function changeowner()
{
var isadmin    =	document.getElementById('isadmin').value;
http.open('GET', WORD_SAVE + 'isadmin=' + isadmin + '&task=changeowner', true);
http.onreadystatechange = handleHttpChangeOwner;
http.send(null);
}

function changeownermail()
{
var isadmin    =	document.getElementById('isadmin').value;
http.open('GET', WORD_SAVE + 'isadmin=' + isadmin + '&task=changeowner&mail=1', true);
http.onreadystatechange = handleHttpChangeOwner;
http.send(null);
}

function handleHttpChangeOwner()
{
  if (http.readyState == NORMAL_STATE) {
	 document.getElementById('selectowner').innerHTML = http.responseText;
		}
}

function changeurl()
{
var id    =	document.Charity_frm.charity.value;
http.open('GET', ADMIN_WORD_SAVE + 'id=' + id + '&task=getcharityurl', true);
http.onreadystatechange = handleHttpCharityUrl;
http.send(null);
}

function handleHttpCharityUrl()
{
  if (http.readyState == NORMAL_STATE) {
	 document.getElementById('urlval').innerHTML = "<a href='"+http.responseText+"' target='_blank'>"+http.responseText+"</a>";
		}
}

function makecurrent(clueval)
{
 var cid= document.getElementById('cid').value;
 var clues = clueval.split('#');
 var clueid = clues[0];
 var word = clues[1];
 http.open('GET', WORD_SAVE + 'clueid=' + clueid + '&cid=' + cid +'&word=' + word + '&task=savecluecross', true);
 http.onreadystatechange = handleHttpValidCluecross;
 http.send(null);
}

function savecluecross()
{
var clue= document.getElementById('clue').value;
var wordid= document.getElementById('wordid').value;
http.open('GET', WORD_SAVE + 'clue=' + clue + '&wordid=' + wordid + '&task=saveclue', true);
http.onreadystatechange = handleHttpValidCluecross;
http.send(null);
}
function handleHttpValidCluecross()
{
	
  if (http.readyState == NORMAL_STATE) {
	  viewclue('viewcrossclue');
		
	}
}

function savecluecrossgame()
{
var clue= document.getElementById('clue').value;
var wordid= document.getElementById('wordid').value;


http.open('GET', WORD_SAVE + 'clue=' + clue + '&wordid=' + wordid + '&task=saveclue', true);
http.onreadystatechange = handleHttpValidCluecrossgame;
http.send(null);
}
function handleHttpValidCluecrossgame()
{
	
  if (http.readyState == NORMAL_STATE) {
	  viewclue('viewcrosscluegame');
		
	}
}

function deletegamescross(id)
{
 var gameid = document.getElementById('gameid').value;
 http.open('GET', WORD_SAVE + 'cid=' + id +'&gameid=' + gameid + '&task=deletegamecross', true);
 http.onreadystatechange = handleHttpValidDeleteCross;
 http.send(null);
}

function handleHttpValidDeleteCross()
{
 if (http.readyState == NORMAL_STATE) 
	 {
	 document.getElementById('crossset').innerHTML = http.responseText;
	 }
}

function deletegameskqs(id)
{
 var gameid = document.getElementById('gameid').value;
 http.open('GET', WORD_SAVE + 'id=' + id +'&gameid=' + gameid + '&task=deletegamekqs', true);
 http.onreadystatechange = handleHttpValidDeleteKqs;
 http.send(null);
}

function handleHttpValidDeleteKqs()
{
 if (http.readyState == NORMAL_STATE) 
	 {
	 document.getElementById('kqsset').innerHTML = http.responseText;
	 }
}
function savesummarycross()
{
var cid= document.getElementById('cid').value;
var summary= document.getElementById('summary').value;
http.open('GET', WORD_SAVE + 'cid=' + cid +'&summary=' + summary +  '&task=editcrosssummary', true);
http.onreadystatechange = handleHttpValidSummaryCross;
http.send(null);
}
function handleHttpValidSummaryCross()
{
	
 if (http.readyState == NORMAL_STATE) {
	 var cid= document.getElementById('cid').value;
	 window.opener.document.getElementById('summary'+cid).innerHTML = http.responseText;
	 }
}
function savebasiccrossinfo()
{
var cid= document.getElementById('cid').value;
changethmaevalue();
var themeid= document.getElementById('themeid').value;
var summary= document.getElementById('summary').value;
var grade= document.getElementById('grade').value;

http.open('GET', WORD_SAVE + 'cid=' + cid + '&theme=' + themeid + '&summary=' + summary + '&grade=' + grade + '&task=editcrossbasic', true);
http.onreadystatechange = handleHttpValidBasicCross;
http.send(null);
}
function handleHttpValidBasicCross()
{
 if (http.readyState == NORMAL_STATE) {
	 window.opener.document.getElementById('baseinfo').innerHTML = http.responseText;
	
	}
}

function viewclue4()
{
var task='viewallgamecrossclue';
	var x=1;
	var y=1;
	
	var cid = document.getElementById('cid').value;
	
	http.open('GET', WORD_SAVE + 'x=' + x + '&y=' + y +'&cid=' + cid + '&task='+task + '&close=1', true);
    http.onreadystatechange = handleHttpValidViewclue;
    http.send(null);	

}

function viewclue3()
{
var task='viewallcrossclue';
	var x=1;
	var y=1;
	
	var cid = document.getElementById('cid').value;
	
	http.open('GET', WORD_SAVE + 'x=' + x + '&y=' + y +'&cid=' + cid + '&task='+task + '&close=1', true);
    http.onreadystatechange = handleHttpValidViewclue;
    http.send(null);	

}

function viewclue2()
{
var task='viewallcrossclue';
	viewclue(task);
}
function saveclue()
{
var clue= document.getElementById('clue').value;
var wordid= document.getElementById('wordid').value;
http.open('GET', WORD_SAVE + 'clue=' + clue + '&wordid=' + wordid + '&task=saveclue', true);
http.onreadystatechange = handleHttpValidClue;
http.send(null);
}
function handleHttpValidClue()
{
  if (http.readyState == NORMAL_STATE) {
	  document.getElementById("addclue").innerHTML=http.responseText;
		
	}
}

function viewclue(task)
{
	if(task !='viewallcrossclue')
	{
	var x = document.getElementById('curx').value;
	var y = document.getElementById('cury').value;
	}else
	{
		var x=1;
		var y=1;
	}
	var cid = document.getElementById('cid').value;
	
	http.open('GET', WORD_SAVE + 'x=' + x + '&y=' + y +'&cid=' + cid + '&task='+task, true);
    http.onreadystatechange = handleHttpValidViewclue;
    http.send(null);	
}

function  handleHttpValidViewclue()
{
 if (http.readyState == NORMAL_STATE) {
	 
	  document.getElementById("vclues").innerHTML=http.responseText;
		
	}
}

function viewset(qid)
{
http.open('GET', WORD_SAVE + 'qid=' + qid + '&task=viewqset', true);
http.onreadystatechange = handleHttpValidVset;
http.send(null);	

}

function handleHttpValidVset()
{
document.getElementById("vset").innerHTML=http.responseText;
}
function viewwordclues(wordid)
{
http.open('GET', WORD_SAVE + 'wordid=' + wordid + '&task=viewclues', true);
 http.onreadystatechange = handleHttpValidViewClues;
 http.send(null);	
}
function handleHttpValidViewClues()
{
	
if (http.readyState == NORMAL_STATE) {
	  document.getElementById("clues").innerHTML=http.responseText;
		
	}
}
function editvalidclue(id)
 {
 http.open('GET', WORD_SAVE + 'clueid=' + id + '&task=editclue', true);
 http.onreadystatechange = handleHttpValidClue;
 http.send(null);
 }
 function getvalidclue(id)
 {
 http.open('GET', WORD_SAVE + 'clueid=' + id + '&task=getclue', true);
 http.onreadystatechange = handleHttpValidGetClue;
 http.send(null);
 }
 


function handleHttpValidGetClue()
{
	
 if (http.readyState == NORMAL_STATE) {
	  document.getElementById("clue").value=http.responseText;
		
	}
}

function saveeditword()
{
var name= document.getElementById('name').value;
var themeid= '';
var active= document.getElementById('active').value;
var wordid= document.getElementById('wordid').value;
for(var i=0;i<document.getElementById('theme').options.length;i++)
	{
	if(document.getElementById('theme').options[i].selected )
	themeid += document.getElementById('theme').options[i].value + ',';
	}
http.open('GET', WORD_SAVE + 'name=' + name + '&themeid=' + themeid  + '&wordid=' + wordid + '&active=' + active+ '&task=editword', true);
http.onreadystatechange = handleHttpValidEitWord;
http.send(null);

}
function handleHttpValidEitWord()
{
if (http.readyState == NORMAL_STATE) {
	  document.getElementById("msg").innerHTML=http.responseText;
		
	}
}

function saveword()
{
var name= document.getElementById('name').value;
var themeid= '';
var active= document.getElementById('active').value;

for(var i=0;i<document.getElementById('theme').options.length;i++)
	{
	if(document.getElementById('theme').options[i].selected )
	themeid += document.getElementById('theme').options[i].value + ',';
	}

http.open('GET', WORD_SAVE + 'name=' + name + '&themeid=' + themeid + '&active=' + active+ '&task=saveword', true);
http.onreadystatechange = handleHttpValidWord;
http.send(null);

}

function getwords()
{
var themeid= '';
var active = '1';
var maxlen = document.getElementById('maxlen').value;
for(var i=0;i<document.getElementById('theme').options.length;i++)
	{
	if(document.getElementById('theme').options[i].selected )
	themeid += document.getElementById('theme').options[i].value + ',';
	}
var params = 'themeid=' + themeid + '&active=' + active+ '&maxlen=' + maxlen+ '&task=getwords';
http.open('POST', WORD_SAVE_POST , true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = handleHttpValidgetWord;
http.send(params);

}
function handleHttpValidgetWord()
{
	if (http.readyState == NORMAL_STATE)
		{
		var wordname = http.responseText.split('#');
		var wordcount = wordname[0];
         wordname = wordname[1];
		document.getElementById('fillwords').value= wordname;
		document.getElementById('wordcount').innerHTML= wordcount;
		document.getElementById('indicator1').style.visibility = 'hidden';
	    document.getElementById('indicator1').style.display = 'none';
	    }
}
function changethmaevalue()
{
	var themeid = '';
    
	for(var i=0;i<document.getElementById('theme').options.length;i++)
	{
	if(document.getElementById('theme').options[i].selected )
		{
		themeid +=  document.getElementById('theme').options[i].value +',';
		}
	}
    
	document.getElementById('themeid').value = themeid;
}

function changecatvalue()
{
	var catid = '';
    
	for(var i=0;i<document.getElementById('category').options.length;i++)
	{
	if(document.getElementById('category').options[i].selected )
		{
		catid +=  document.getElementById('category').options[i].value +',';
		}
	}
    
	document.getElementById('catid').value = catid;
}

function handleHttpValidWord()
{
	
		if (http.readyState == NORMAL_STATE) {
		if(http.responseText > 0){ 
			
				if(http.responseText== 1)
				{
				document.getElementById("addword").style.visibility="hidden";
				document.getElementById("addword").style.display="none";
				document.getElementById("msg").style.visibility="visible";
				document.getElementById("msg").style.display="block";
				document.getElementById("msg").innerHTML='Please enter word.';
				}else if(http.responseText== 2)
					{
					document.getElementById("msg").style.visibility="visible";
				    document.getElementById("msg").style.display="block";
					document.getElementById("msg").innerHTML='sorry this word already exists.';
				    }else
						{
                         document.getElementById("addword").style.visibility="hidden";
				         document.getElementById("addword").style.display="none";
				         document.getElementById("msg").style.visibility="hidden";
				         document.getElementById("msg").style.display="none"; 
						 document.getElementById("addclue").innerHTML=http.responseText; 
						 document.getElementById("addclue").style.visibility="visible";
				         document.getElementById("addclue").style.display="block";
						 document.getElementById("msg").innerHTML='';
						 document.getElementById("newword").innerHTML='';
						 				
						}
			}else
				{
				
				document.getElementById("addword").style.visibility="hidden";
				document.getElementById("addword").style.display="none";
				
				document.getElementById("addword").innerHTML='';
				
				document.getElementById("msg").innerHTML='';
				document.getElementById("addclue").innerHTML=http.responseText;
				document.getElementById("newword").innerHTML='';
				}
	}
}

function checkvalidadmin()
{
var username= document.getElementById('username').value;
var password= document.getElementById('password').value;
http.open('GET', ADMIN_LOGIN + 'username=' + username + '&password=' + password , true);
http.onreadystatechange = handleHttpValidAdmin;
http.send(null);
}

function handleHttpValidAdmin()
{
	if (http.readyState == NORMAL_STATE) {
		if(http.responseText > 0){
			 
				if(http.responseText== 1)
				{
				document.getElementById("next").style.visibility="hidden";
				document.getElementById("next").style.display="none";
				document.getElementById("msg").style.visibility="hidden";
				document.getElementById("msg").style.display="none";
				document.getElementById("forgotpass").style.visibility="visible";
				document.getElementById("forgotpass").style.display="block";
				}else if(http.responseText== 2)
					{
					document.getElementById("msg").style.visibility="visible";
				    document.getElementById("msg").style.display="block";
				    }else
						{
                         document.getElementById("next").style.visibility="hidden";
				         document.getElementById("next").style.display="none";
				         document.getElementById("msg").style.visibility="hidden";
				         document.getElementById("msg").style.display="none"; 
						 document.getElementById("question").innerHTML=http.responseText; 
						}
			}else
				{
				document.getElementById("next").style.visibility="hidden";
				document.getElementById("next").style.display="none";
				 document.getElementById("question").innerHTML=http.responseText;
				}
	}
}

function CheckAvailability(imgType,imgName)
{
	//alert(LOGIN_PREFIX_AVAIL + 'imgN=' + imgName + '&imgType=' + imgType );
	if ( imgName!='')
	{
	
	http.open('GET', LOGIN_PREFIX_AVAIL + 'imgN=' + imgName + '&imgType=' + imgType , true);
	if(imgType==1)
		http.onreadystatechange = handleHttpCheckAvailability;
	if(imgType==2)
		http.onreadystatechange = handleHttpCheckAvailability2;
	http.send(null);
	}
}

function handleHttpCheckAvailability()
{
	if (http.readyState == NORMAL_STATE) {
		if(http.responseText > 0){
			alert('The image name is already exists. Please enter the different name.');
			document.product_management_frm.smallImgNameVal.focus();
			document.product_management_frm.smallImgNameVal.select();
		}
	}
}
function handleHttpCheckAvailability2()
{
	if (http.readyState == NORMAL_STATE) {
		if(http.responseText > 0){
			alert('The image name is already exists. Please enter the different name.');
			document.product_management_frm.bigImgNameVal.focus();
			document.product_management_frm.bigImgNameVal.select();
		}
	}
}


/*to trim the values */
// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}


function EvalSound(soundobj) 
{
	var thissound= eval("document."+soundobj);
	thissound.Play();
}

function checkUserGamesToPlay()
{
	http.open('GET', 'AlarmBellCheck.php' , true);
	http.data = null;
	http.onreadystatechange = function()
	{
		if (http.readyState == NORMAL_STATE)
		{
			var gamescount = http.responseText;
			if(!isNaN(gamescount) && gamescount != 0)
			{
				//EvalSound('GameAlarm');
				sendToActionScript();
				//thisMovie("music").sendToActionScript("yes");
			}
			setTimeout("checkUserGamesToPlay();", 60000);
	    }
	}
	http.send(null);
}

