// JavaScript Document

//Function to check 4 emty fields
	function checkEmpty(param1,param2,param3)
	{
		var value1 = document.getElementById(param1).value;	
		
		if(value1 == "null" || value1 == "")
		{
			document.getElementById(param3).innerHTML = "<font color = 'red'>"+param2+"</font>";
			document.getElementById(param1).focus();
			return false;	
		}
		
		return true;
	}
	
	// prompts the user whether or not they would like to delete an entity
	function deleteEntity(url, entity) {
		message = "Are you sure you want to delete this "+entity+"? \n" + 
		"You will lose all data under this "+entity+" if deleted\n" +
						"Press OK to delete the "+entity+" \n" + 
						"Cancel to stay on the current page";
		if (window.confirm(message))
		{
			window.location.href = url;
		}
	}
	//Function to clear bulk textarea off the default text in it
	function clearText(param)
	{
		var defaulttext = false;
		
		if(document.form.fromno.value == "Your Number")
		{
			document.form.fromno.value = "07";
			defaulttext = true;
			return defaulttext;
		}
		else 
		{
			if(document.form.fromno.value == "")
			{
				document.form.fromno.value = "";
				defaulttext = true;
				return defaulttext;
			}
		}
	}
	
	function clearText1(param)
	{
		var defaulttext = false;
		
		if(document.form.tono.value == "Recepient's Number")
		{
			document.form.tono.value = "07";
			defaulttext = true;
			return defaulttext;
		}
		else 
		{
			if(document.form.tono.value == "")
			{
				document.form.tono.value = "Recepient's Number";
				defaulttext = true;
				return defaulttext;
			}
		}
	}
	
	function clearText2(param)
	{
		var defaulttext = false;
		
		if(document.form.mes.value == "Type your message here")
		{
			document.form.mes.value = "";
			defaulttext = true;
			return defaulttext;
		}
		else 
		{
			if(document.form.mes.value == "")
			{
				document.form.mes.value = "Type your message here";
				defaulttext = true;
				return defaulttext;
			}
		}
	}
	
	//Remove leading and trailing spaces
	function trimString(sInString) {
	  sInString = sInString.replace( /^\s+/g, "" );// strip leading
	  return sInString.replace( /\s+$/g, "" );// strip trailing
	}
	
	// general purpose function to see if an input value has been
	// entered at all
	function isEmpty(inputStr) {
		if (inputStr == null || inputStr == "") {
			return true;
		}
		return false;
	}
	
	// entered at all or if the input value has a value "null"
	function isNullOrEmpty(inputStr) {
		// trim; remove leading and trailing spaces
		var trimmedValue = trimString(inputStr);
		if (isEmpty(trimmedValue) || trimmedValue == "null") {
			return true;
		}
		return false;
	}
	
	// Validates the email entered.
	function validateEmail(fieldValue){
	   // The invalid characters that should not be used in an email address
	   var invalidChars = " /:,;"; 
	   var emailAddress = fieldValue;
	   
	   var atPosition = emailAddress.indexOf("@",1);
	   var periodPosition = emailAddress.indexOf(".",atPosition);
	   
	   if (isNullOrEmpty(emailAddress)){
		  return false;
	   }
	   // Checks for the invalid characters listed above.
	   for (var i=0; i<invalidChars.length; i++){
		  badChar = invalidChars.charAt(i);
		  if (emailAddress.indexOf(badChar,0) > -1){
			 return false;		 
		  }
	   }
	
	   if (atPosition == -1){ // Checks for the @
		  return false;
	   }
	   if (emailAddress.indexOf("@",atPosition + 1) > -1){ // Makes sure there is one @
		  return false;
	   }
	   if (periodPosition == -1){ // Makes sure there is a period after the @ 
		  return false;
	   }
	   // Makes sure there is at least 2 characters after the period
	   if ((periodPosition + 3) > emailAddress.length){ 
		  return false;
	   }
	   
	   return true;
	}
	
	// function used to check email and display message
	function isValidEmail(fieldname, msg) {
		if (!validateEmail(document.getElementById(fieldname).value)) {
			document.getElementById(fieldname).focus();
			alert(msg);
			return false;
		}
		return true;
	}
	
	function charCount(form,msgField,countField,evt){
		
	var msgLen = document.getElementById(msgField).value.length;
	var rem = 120 - msgLen;
	evt = (evt) ? evt : window.event
	var chCode = (evt.which) ? evt.which: evt.keyCode
	document.getElementById(countField).innerHTML = rem;
	if(rem<1){

		if(chCode != 8 && chCode != 37 && chCode != 38 && chCode != 39 && chCode != 40 && chCode != 46 && chCode != 13) {
			rem = 1;
			alert("Max character count reached");
			document.getElementById(msgField).focus()
			return false;
			}
			
		}
	}
	
	//Numbers only Field
	function onlyNumbers(evt){
	evt = (evt) ? evt : window.event
	var chCode = (evt.which) ? evt.which: evt.keyCode
	if(chCode > 31 && (chCode < 48 || chCode > 57) && chCode != 8 && chCode != 37 && chCode != 38 && chCode != 39 && chCode != 40 && chCode != 46 && chCode != 13) {
		
		document.getElementById('sendingsms').innerHTML = "<font color = 'red'>Field requires only Numbers</font>";
		return false;
		}
	return true;
	}
	
	//function to show sms div
	function showSMSDiv(hide,show)
	{
		document.getElementById(hide).style.visibility = 'hidden';
		document.getElementById(hide).style.height = '0px'; 
		document.getElementById(show).style.overflow = "hidden";
		document.getElementById(show).style.visibility = 'visible'; 
		document.getElementById(show).style.height = 'auto'; 	
		
		if(navigator.appName != "Opera")
		{
			document.getElementById('fromno').innerHTML = "Your Number";	
			document.getElementById('tono').innerHTML = "Recepient's Number";	
			document.getElementById('mes').innerHTML = "Type your message here";	
		}
		
		return false;
	}
	 //hide this div

	//Funtion to check for file types uplods
	function getFiletype(param1,param2)
	{
		var b_file = document.getElementById(param1).value;
		var file_ext = b_file.split(".");
		if(file_ext[1] != "doc" && file_ext[1] != "pdf" && file_ext[1] != "xls" && file_ext[1] != "ppt" && file_ext[1] != "docx")
		{
			document.getElementById(param2).innerHTML = "<font color = 'red'>Invalid file browsed, valid formats include .doc, .docx, .pdf, .ppt, .xls. </font>";
			return false;	
		}
		else
		{
			return true;	
		}
	}
	
	//function to show textarea for campaigns
	function showcampaigncontent()
	{
		if (document.forms[0].has_link.checked) 
		{
			document.getElementById('campaign_links').style.visibility = "visible";
			document.getElementById('campaign_links').style.height = "auto";
		} 
		else 
		{
			document.getElementById('campaign_links').style.visibility = "hidden";
			document.getElementById('campaign_links').style.height = "0px";
			document.campaigns.campaign_content.value = "";
		}
		
		return false;	
	}
	
	//function to check for checkbox empty
	function checkboxEmpty()
	{
		if (document.forms[0].has_link.checked) 
		{
			var value1 = document.getElementById('campaign_content').value;	
		
			if(value1 == "null" || value1 == "")
			{
				document.getElementById('showerror').innerHTML = "<font color = 'red'>Enter campaign content</font>";
				document.getElementById('campaign_content').focus();
				return false;	
			}
		} 
		
		return true;
	}

	//Function to create the xmlhttp object
	function createObject()
		{
			//Check if we are using IE.
			try 
			{
				//If the javascript version is greater than 5.
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  
			}
			catch (e)
			{
				//If not, then use the older active x object.
				try
				{
					//If we are using IE.
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
				}
				catch (E)
				{
					try
					{
						xmlhttp = new XMLHttpRequest();	
					}
					catch(E)
					{
						alert("Your browser doesnt support Javascript OR Javascript is turned off");
						xmlhttp = false;
						return xmlhttp;
					}
				}
			}	
			
			return xmlhttp;
		}


//Function for Login
function checkLogin(param1,param2,param3)
{
	var uname = document.getElementById(param1).value;
	var upass = document.getElementById(param2).value;
	var result;
	
	//Create an object
	xmlhttp=createObject();
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState < 4)
		{
			document.getElementById(param3).innerHTML = "<font color = 'green'>Processing Login ... </font><img src='../images/loading.gif' name = 'img1' align='middle' width='32' height = '32' alt='loading'>";
				
		}
		if(xmlhttp.readyState == 4)
		{
			//document.getElementById(param).innerHTML = xmlhttp.responseText;
			result = xmlhttp.responseText;
			
			if(result == 200)
			{
				document.location.href = "../dmz_zone/control_panel.php";	
			}
			else
			{
				document.getElementById(param3).innerHTML = "<font color = 'red'>"+xmlhttp.responseText+"</font>";
			}
		}
	}
					
	xmlhttp.open("post","../.inkcls/.processlogin.php",true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send("username="+uname+"&password="+upass);
		
	return false;
}

	//Numbers only Field
	function onlyNumbers(evt){
	evt = (evt) ? evt : window.event
	var chCode = (evt.which) ? evt.which: evt.keyCode
	if(chCode > 31 && (chCode < 48 || chCode > 57) && chCode != 8 && chCode != 37 && chCode != 38 && chCode != 39 && chCode != 40 && chCode != 46 && chCode != 13) {
		
		alert("Field requires only Numbers");
		return false;
		}
	return true;
	}
	
	//Function to load sub menu's
	function loadSubmenu(param1,param2,param3)
	{
		var menuid = document.getElementById(param1).value;
		var result;
		var mysplit;
		//Create an object
		xmlhttp=createObject();
		
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState < 4)
			{
				document.getElementById(param2).innerHTML = "<font color = 'green'> Loading submenus ... </font>";
				
				document.getElementById(param3).innerHTML = "<font color = 'green'>Loading....</font>";
					
			}
			if(xmlhttp.readyState == 4)
			{
				//document.getElementById(param2).innerHTML = xmlhttp.responseText;
						
				result = xmlhttp.responseText;
				
				mysplit = result.split("--");
				
				if(mysplit[0] == 100)
				{
					document.getElementById(param2).innerHTML = "<font color = 'red'> Select Menu Item has no Sub Menus but content can still be added in the text area below. </font> <input type = \"hidden\" name = \"submenu_id\" value = \"0\" /> <input type = \"hidden\" name = \"menuid\" value = \""+menuid+"\" />";
					
					//document.myform.content.innerHTML = mysplit[1];
					//document.myform.content.innerHTML = focus();
					var p2 =Math.random();
					document.getElementById('showtextarea').innerHTML = "<textarea cols=\"70\" rows=\"25\" name=\"content\" id='"+p2+"3'>"+mysplit[1]+"</textarea>";
					tinyMCE.execCommand('mceAddControl', false, p2+"3");
				}
				else if(mysplit[0] == 200)
				{
					document.getElementById(param2).innerHTML = mysplit[1];
					document.getElementById('showtextarea').innerHTML = "<textarea cols=\"70\" rows=\"25\" name=\"content\" id='"+p2+"4'></textarea>";
					tinyMCE.execCommand('mceAddControl', false, p2+"4");
				}				
			}
		}
						
		xmlhttp.open("post","../.inkcls/.loadsubmenus.php",true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		xmlhttp.send("menuid="+menuid);
			
		return false;
	}
	
	//Load the submenu
	function loadContent(param1,param2)
	{
		var submenuid = document.getElementById(param1).value;
		
		xmlhttp=createObject();
	
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState < 4)
			{
				document.getElementById('showtextarea').innerHTML = "<font color = 'green'>Getting content ...";
					
			}
			if(xmlhttp.readyState == 4)
			{
				var p = Math.random();
				//document.getElementById(param).innerHTML = xmlhttp.responseText;
			document.getElementById('showtextarea').innerHTML = "<textarea cols=\"70\" rows=\"25\" name=\"content\" id='"+p+"2' >"+xmlhttp.responseText+"</textarea>";
				tinyMCE.execCommand('mceAddControl', false, p+"2");
			}
		}
						
		xmlhttp.open("post","../.inkcls/.loadsubmenu.php",true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		xmlhttp.send("id="+submenuid);
			
		return false;
	}
	
	function loadMobile(param1,param2,param3,param4)
	{
		var menuid = param1;
		var submenuid = param2;
		var mytitle = param3;
		
		xmlhttp=createObject();
	
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState < 4)
			{
				document.getElementById(param4).innerHTML = "<font color = 'green'> ... loading </font><b>"+mytitle+"</b>";
					
			}
			if(xmlhttp.readyState == 4)
			{
				document.getElementById(param4).innerHTML = xmlhttp.responseText;
			}
		}
						
		xmlhttp.open("post","../.inkcls/.load_mobilecontent.php",true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		xmlhttp.send("menu="+menuid+"&submenu="+submenuid+"&mytitle="+mytitle);
				
		return false;	
	}
	
	//load press release
	function loadPress(param1,param2)
	{
		
		var contentid = param1;
		
		xmlhttp=createObject();
	
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState < 4)
			{
				document.getElementById(param2).innerHTML = "<font color = 'green'> ... loading </font>";
					
			}
			if(xmlhttp.readyState == 4)
			{
				document.getElementById(param2).innerHTML = xmlhttp.responseText;
			}
		}
						
		xmlhttp.open("post","../.inkcls/.load_content.php",true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		xmlhttp.send("contentid="+contentid);
				
		return false;
	}
	
	//function to load press release
	function getPress(param1,param2,param3)
	{
		var id = param1;
		
		xmlhttp=createObject();
	
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState < 4)
			{
				document.getElementById(param2).innerHTML = "<font color = 'green'>Loading ... </font><img src='../images/loading.gif' name = 'img1' align='middle' width='32' height = '32' alt='loading'>";
					
			}
			if(xmlhttp.readyState == 4)
			{
				document.getElementById(param2).innerHTML = xmlhttp.responseText;
			}
		}
						
		if(param3 == 1)
		{
			xmlhttp.open("post","../.inkcls/.getpressrelease.php",true);
		}
		else
		{
			xmlhttp.open("post","../.inkcls/.getnews.php",true);
		}
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		xmlhttp.send("id="+id);
				
		return false;
	}
	
	//function to send an SMS
	function sendSms(param1,param2,param3,param4)
	{
		var fromno = document.getElementById(param1).value;
		var tono = document.getElementById(param2).value;
		var mes = document.getElementById(param3).value;
		
		if(fromno == "" || fromno == "07" || fromno == "Your Number" || fromno == "null")
		{
			document.getElementById(param4).innerHTML = "<font color = 'red'>Enter - Your Number.</font>";
			return false;
		}
		else if(tono == "" || tono == "07" || tono == "Recepient's Number" || tono == "null")
		{
			document.getElementById(param4).innerHTML = "<font color = 'red'>Enter - Recepient's Number.</font>";
			return false;
		}
		else if(mes == "" || mes == "Type your message here" || mes == "null")
		{
			document.getElementById(param4).innerHTML = "<font color = 'red'>Enter - message to recipient.</font>";
			return false;
		}
		else
		{
			xmlhttp=createObject();
	
			xmlhttp.onreadystatechange = function()
			{
				if(xmlhttp.readyState < 4)
				{
					document.getElementById(param4).innerHTML = "<font color = 'green'>Sending SMS to <br>...<b>"+tono+"<b></font>";
						
				}
				if(xmlhttp.readyState == 4)
				{
					var result = xmlhttp.responseText; 
					
					if(result == "101")
					{
						document.getElementById(param4).innerHTML = "<img src=\"../images/send_sms.gif\" name = \"img1\"  width=\"24\" height=\"21\" align=\"middle\" border=\"0\" /><font color = 'green'> SMS sent to <b>"+tono+"</b></font>";
					}
					else if(result == "202")
					{
						document.getElementById(param4).innerHTML = "<img src=\"../images/stop_sms.gif\" name = \"img2\"  width=\"24\" height=\"21\" align=\"middle\" border=\"0\" /><font color = 'red'> SMS not sent to "+tono+"</font>";
					}
					else
					{					
						document.getElementById(param4).innerHTML ="<font color='red'>"+ xmlhttp.responseText +"</font>";
						document.getElementById('mes').value="";
						document.getElementById('tono').value="";

					}
					
				}
			}
							
			xmlhttp.open("post","../.inkcls/.sendsms.php",true);
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
			xmlhttp.send("fromno="+fromno+"&tono="+tono+"&mes="+mes);
					
			return false;
		}

		return false;
	}
	
	//get the contents of campaigns
	function getContent(param1,param2,param3)
	{
		var campaignid = param1;
		
		xmlhttp=createObject();
	
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState < 4)
			{
				if(param3 == "show")
				{
					document.getElementById(param2).innerHTML = "<font color = 'green'> ... loading </font>";
				}
				else
				{
					document.getElementById(param2).innerHTML = "<font color = 'green'> ... hiding </font>";
				}
					
			}
			if(xmlhttp.readyState == 4)
			{
				document.getElementById(param2).innerHTML = xmlhttp.responseText;
			}
		}
						
		xmlhttp.open("post","../.inkcls/.load_campaign.php",true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		xmlhttp.send("id="+campaignid+"&task="+param3);
		
		return false;
	}
	
	//Function to post user comments
	function postComment(param1,param2,param3,param4)
	{
		var user_names = document.getElementById(param1).value;
		var user_email = document.getElementById(param2).value;
		var user_comment = document.getElementById(param3).value;
		
		xmlhttp=createObject();
	
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState < 4)
			{
				document.getElementById(param4).innerHTML = "<font color = 'green'> Sending comment ... </font>";					
			}
			
			if(xmlhttp.readyState == 4)
			{
				document.getElementById(param4).innerHTML = xmlhttp.responseText;
				document.getElementById(param1).value = "";
				document.getElementById(param2).value = "";
				document.getElementById(param3).value = "";
			}
		}
						
		xmlhttp.open("post","../.inkcls/.postcomment.php",true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		xmlhttp.send("names="+user_names+"&email="+user_email+"&comments="+user_comment);
		
		return false;
	}
