<?php

						/*************************************\
						
						    Handshakes Professional (3.2)
						            by DZOIC LTD
						       Copyright(c) 2005-2006
						        http://www.dzoic.com
						
						\*************************************/

						///////////////////////////////////////

						admin_validate_section("develop",2);
						$action = $GLOBALS["Get"]->val("action");

							switch($action){

								case '':
									manage_javascript();
									break;
									
								case 'save':
									save_javascript();
									break;

							}//switch

						///////////////////////////////////////
						
function manage_javascript(){
	
	admin_validate();
	
	$data["section"] = "develop";
	$data["subsection"] = 2;
	$data["title_id"] = "20";	
	$data["themes"] = admin_get_themes();	
	$theme = $GLOBALS["Get"]->val("theme");
	
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	
	if($theme){
		$data["pages"]["all"] = "All Pages";
		$data["pages"] = array_merge($data["pages"],admin_get_pages($theme));
		
		$sql_query = "SELECT page,code,onload FROM javascripts WHERE theme='".$theme."'";
		$result = $GLOBALS["DB"]->result($sql_query);
		$javascript = array();
		foreach($result as $res){
			$javascript[$res["page"]]["onload"] = $res["onload"];
			$javascript[$res["page"]]["code"] = $res["code"];
		}//foreach
				
		$data["javascripts"] = $javascript;
		$data["theme"] = $theme;
		$layout[0] = "javascript";
	}//if
	
	admin_output($layout,$data);
	
}//manage_javascript

function save_javascript(){
	
	admin_validate();
	
	$onloads = $GLOBALS["Get"]->val("onloads");
	$codes   = $GLOBALS["Get"]->val("codes");
	$theme   = $GLOBALS["Get"]->val("theme");	
	
	$sql_query = "DELETE FROM javascripts WHERE theme='".$theme."'";
	$GLOBALS["DB"]->execute($sql_query);
	
	while(list($key,$val) = each($onloads)){
		if(eregi("[A-Za-z]",$val) || eregi("[A-Za-z]",$codes[$key])){
			
			$onload  = stripslashes(html_entity_decode($val));
			$onload  = hs_ereg_replace(preg_quote("&#124;"),"|",$onload);
			$code    = stripslashes(html_entity_decode($codes[$key]));
			$code    = hs_ereg_replace(preg_quote("&#124;"),"|",$code);
			$onload  = ereg_replace("\\\\","\\\\",$onload);
			$onload  = ereg_replace("'","\'",$onload);
			$code    = ereg_replace("\\\\","\\\\",$code);
			$code    = ereg_replace("'","\'",$code);
		
			$sql_query = "INSERT INTO javascripts (page,theme,onload,code)
						  VALUES ('".$key."','".$theme."','".$onload."','".$code."')";
			$GLOBALS["DB"]->execute($sql_query);
		
		}//if
	}//while	
	
	load_page("index.php?section=javascript&theme=".$theme);
	
}//save_javascript

?>