<?php
//session_start();

 	error_reporting(0);

    function dbconnect()
    {
		/*
        $dsn = "localhost";
        $userid = "root";
        $userpw = "root";
        $db_name = "mobivationDB";
    	*/    
    	$dsn = "localhost";
        $userid = "lottery_mobinew";
        $userpw = "lottery_mobinew";
		$db_name = "lottery_mobinew";
		 
		
        $dbcon = mysql_connect ($dsn, $userid, $userpw) or die ("Please contact your administrator 1");

        $link = mysql_select_db ($db_name) or die ("Please contact your administrator 2");
        return $link;
    }

function dbclose()
    {
        mysql_close($link);
    }

function getData($tbFields,$tbName,$tbWhere)
    {
        $con = dbconnect();
        $txtQuery = "SELECT " . $tbFields . " FROM " . $tbName ;
        //echo $txtQuery;//return($txtQuery);
        if ((!$tbWhere == ""))
        {
            $txtQuery = $txtQuery . " WHERE " . $tbWhere;
        }
        //echo $txtQuery;//return($txtQuery);
        $query = mysql_query($txtQuery);
        $data = mysql_fetch_array($query);
        return($data);
        $con = dbclose();
    }

function getList($tbFields,$tbName,$tbWhere)
    {
        $con = dbconnect();
        $txtQuery = "SELECT " . $tbFields . " FROM " . $tbName ;
        if ((!$tbWhere == ""))
        {
            $txtQuery = $txtQuery . " " . $tbWhere;
        }
		//echo "<br><br>" . $txtQuery;//return($txtQuery);
        $query = mysql_query($txtQuery);

        $i=0;
        return($query);
        //$con = dbclose();
    }
	
function getListFilter($tbFields,$tbName,$tbWhere)
    {
        $con = dbconnect();
        $txtQuery = "SELECT " . $tbFields . " FROM " . $tbName ;
        if ((!$tbWhere == ""))
        {
            $txtQuery = $txtQuery . " " . $tbWhere;
        }
//	/echo $txtQuery;//return($txtQuery);
        $query = mysql_query($txtQuery);
        $i=0;
        return($query);
        //$con = dbclose();
    }

function saveData($tbName,$tbUpd,$tbWhere,$type)
    {
        $con = dbconnect();
        if ($type=='update')
        {
            $txtQuery = "UPDATE $tbName SET " . $tbUpd . " WHERE " .$tbWhere;
           // echo $txtQuery;
            $query = mysql_query($txtQuery) or die (mysql_error());
        }
        elseif ($type=='new')
        {
        	$txtQuery = "INSERT INTO ".$tbName. " VALUES (".$tbUpd.")";
            //echo $txtQuery;
            $query = mysql_query($txtQuery) or die (mysql_error());
        }
        elseif ($type=='delete')
        {
            $txtQuery = "DELETE FROM ".$tbName. " WHERE " .$tbWhere;
           // echo $txtQuery;
            $query = mysql_query($txtQuery) or die (mysql_error());

        }
        elseif ($type=='deleteall')
        {
            $txtQuery = "DELETE FROM ".$tbName;
           // echo $txtQuery;
            $query = mysql_query($txtQuery) or die (mysql_error());

        }
		//echo $txtQuery;
        return $query;
        //$con = dbclose();
    }

function getArr()
    {
        if(!isset($_SESSION['SESS_ARR']) ||
                  (trim($_SESSION['SESS_ARR'])==""))
                      {
           $tbname = array('language' => 'tb_language',
                           'platform' => 'tb_platform',
                           'top_page' => 'tb_top_page',
           				   'news'     => 'tb_news',
                           'product'  => 'tb_products',
                           'customsolution'  => 'tb_customsolution',
                           'admin'    => 'tb_admin');
           $select = array('language' => 'language_id,language',
                           'platform' => 'platform_id,platform',
                           'top_page' => 'product_id',
           				   'news'     => 'news_id,news_title,news_date,news_content,created_date,created_by',
                           'product'  => 'product_id,product_name,product_logo,product_price,product_brief_desc,product_desc,user_guide,paypal_text,order_id',
                           'customsolution'  => 'custom_id,client_name,client_website,product_name,product_logo,brief_desc, product_desc',
                           'admin'    => 'admin_id,admin_name,username,password,is_admin,is_active,created_by');
           $label = array('language' => array('Language ID','language_id','label','Language','language','varchar'),
                             'platform' => array('Platform ID','platform_id','label','Platform','platform','varchar'),
                             'top_page' => array('Product ID','product_id','listbox'),
           					 'news'     => array('News ID','news_id','label','Title','news_title','varchar','Date','news_date','date',
                                                 'Content','news_content','texteditor','Created Date', 'created_date','date',
                                                 'Created By','created_by','date'),
                             'product'  => array('Product ID','product_id','label',
							 					 'Product Name','product_name','varchar',
                                                 'Language','language','select',
												 'Platform','platform','listbox',
                                                 'Logo (Max Width 128 px)','product_logo','image',
												 'Price','product_price','varchar',
                                                 'Brief Description','product_brief_desc','text',
                                                 'Description','product_desc','texteditor',
												 'User Guide <br> PDF, Max 2MB','user_guide','file',
												 'Paypal Text','paypal_text','text_paypal',
												 'Order ID','order_id','varchar'),
                             'customsolution'  => 
							 			array	(	'Custom ID','custom_id','label',
													'Client Name','client_name','varchar',
                                                 	'Client Website','client_website','varchar',
													'Product Name','product_name','varchar',
                                                 	'Logo','product_logo','image',
													'Platform','platform','listbox',
													'Brief description','brief_desc','text',
                                                 	'Description','product_desc','texteditor'
												),
                             'admin'    => array('Admin ID','admin_id','label','Admin Name','admin_name','varchar',
                                                 'Username','username','varchar','Password','password','password',
                                                 'Admin Flag','is_admin','checkbox','Active','is_active','checkbox')
                         );
           $ncolumn = array('language' => 6,
                           'platform' => 6,
                           'top_page' => 3,
           				   'news'     => 18,
                           'product'  => 33,
                           'customsolution'  => 24,
                            'admin' => 18);
           $_SESSION['SESS_ARR'] = array($tbname,$select,$label,$ncolumn);
         }
     }
function getInputType($inputType,$label,$value,$id)
{
    if($inputType=='varchar'){
        echo '<td><input type="text" name="'.$label.'" value="'.$value.'"/></td>';
    }
    elseif($inputType=='date'){
        echo '<td><input type="text" name="'.$label.'" value="'.$value.'"  class="hasDatepicker"></td>';
    }
    elseif($inputType=="text"){
        echo'<td><textarea name="'.$label.'" cols="50" rows="8" onKeyDown="textCounter('.$label.',this.form.remLen,250);" onKeyUp="textCounter('.$label.',this.form.remLen,250);">'. $value .'</textarea></td>';
    }
    elseif($inputType=="text_paypal"){		
        echo'<td><textarea name="'.$label.'" cols="50" rows="8" onKeyDown="textCounter('.$label.',this.form.remLen2,250);" onKeyUp="textCounter('.$label.',this.form.remLen2,250);">'. $value .'</textarea></td>';
    }

	elseif($inputType=="texteditor"){
    	//echo'<td><textarea name="'.$label.'" cols="90" rows="14" class="mceEditor">'.$value.'</textarea></td>';
    ?>
        
        <?php
		echo'<td>';
		/*
		$sBasePath = $_SERVER['PHP_SELF'] ;
		$sBasePath = substr( $sBasePath, 0, strpos( $sBasePath, "_samples" ) ) ;
		
		$oFCKeditor = new FCKeditor($label) ;
		$oFCKeditor->BasePath	= $sBasePath ;
		$oFCKeditor->Value		= $value ;
		$oFCKeditor->Create() ;
		 */

		echo '<textarea id="' . $label . '" name="' . $label . '">' . $value . '</textarea>';
		echo '<script type="text/javascript">';
		echo ' CKEDITOR.replace( "' . $label . '", { filebrowserBrowseUrl: "filemanager/index.html"} )';
		echo '</script>';
			
		echo'</td>';
		?>
	<?php
    }
    elseif($inputType=='password'){
        echo '<td><input type="password" name="'.$label.'" value="'.$value.'" /></td>';
    }
    elseif($inputType=="select"){
        $tmpTable=getList($label.','.$label.'_id','tb_'.$label,$id);
        echo '<td><select name="'.$label.'">';
        if (count($tmpTable > 0)){
            while($row=mysql_fetch_array($tmpTable) ){
                echo '<option value="'.$row[$label.'_id'].'">'.$row[$label].'</option>';
            }
        }
        echo '</select></td>';
    }
    elseif($inputType=="selectedit"){
         $tmpTable=getList($label.','.$label.'_id','tb_'.$label,"");
        echo '<td><select name="'.$label.'">';
        if (count($tmpTable > 0)){
            while($row=mysql_fetch_array($tmpTable) ){
                echo '<option value="'.$row[$label.'_id'].'" ';
				if ($row[$label.'_id']==$id ){echo "selected";}
				echo '>'.$row[$label].'</option>';
            }
        }
        echo '</select>';
    }
    elseif($inputType=="listboxedit"){
         $tmpTable=getList($label.','.$label.'_id','tb_'.$label,"");       
        if (count($tmpTable > 0)){
            while($row=mysql_fetch_array($tmpTable) ){
                echo '<option value="'.$row[$label.'_id'].'" ';
				echo showOptionsDrop($row[$label.'_id'], $id);
				echo '>'.$row[$label].'</option>';
            }
        }
    }
    elseif($inputType=="listboxview"){
        $tmpTable=getList('*','tb_products','');
        if (count($tmpTable > 0)){
            while($row=mysql_fetch_array($tmpTable) ){
                echo '<option value="'.$row['product_id'].'" ';
				echo showOptionsDrop($row['product_id'], $id);
				echo '>'.$row['product_name'].'</option>';
            }
        }
    }
    elseif($inputType=="listbox"){
        $tmpTable=getList($label.','.$label.'_id','tb_'.$label,$id);
        echo '<td><select multiple name="'.$label.'[]">';
        if (count($tmpTable > 0)){
            while($row=mysql_fetch_array($tmpTable) ){
                echo '<option value="'.$row[$label.'_id'].'">'.$row[$label].'</option>';
            }
        }
        echo '</select></td>';
    }
    elseif($inputType=="selectview"){
        $tmpTable=getList($label.','.$label.'_id','tb_'.$label,"");
        echo '<select  name="'.$label.'">';
        if (count($tmpTable > 0)){
            while($row=mysql_fetch_array($tmpTable) ){
                echo '<option value="'.$row[$label.'_id'].'" ';
				if ($row[$label.'_id']==$id ){echo "selected";}
				echo '>'.$row[$label].'</option>';
            }
        }
        echo '</select>';
    }
    elseif($inputType=="selectproduct"){
		$order = '';
		if ($label == 'platform')
		{
			$order = ' order by platform ';
		}
        $tmpTable=getList($label.','.$label.'_id','tb_'.$label,$order);
        echo '<select  name="'.$label.'">';
        /* echo '<option value=""> -- ALL -- </option>'; */
        if (count($tmpTable > 0)){
            while($row=mysql_fetch_array($tmpTable) ){
                echo '<option value="'.$row[$label.'_id'].'" ';
				if ($row[$label.'_id']==$id ){echo "selected";}
				echo '>'.$row[$label].'</option>';
            }
        }
        echo '</select>';
    }
    elseif($inputType=="checkbox"){
        echo'<td><input type="checkbox" value="' .$value . '" name="'.$label.'"';
        if ($value=="1") {
           echo "value='". $value ."'";
           echo "checked"; }
        echo '></td>';
    }
    elseif($inputType=="image"){
        echo '<td><input name="uploadimage" type="file" /></td>';
    }
	elseif($inputType=="file"){
        echo '<td><input name="uploadfile" type="file" /></td>';
    }
    else{
         echo '<td><input type="text" name="'.$label.'" value="'.$value.'" /></td>';
    }
}
    function showOptionsDrop($array, $active){
        $string = '';
		//foreach($active as $k => $v){
            if(in_array( $array,$active))
                $string = ' selected';
            else
                $string =  $active;
       // }

      //  if($echo)   echo $string;
       // else        return $string;      
        return $string;
    }
	
	
		function remove_tag($tag,$string)
	{
		$text = preg_replace('#</?' . $tag . '[^>]*>#is', '', $string);
		return $text; 
	}

function WordDelimiter($long_string,$limit=8) 
{
	$str_temp=@explode(" ",$long_string);
	$temp2 = "";
	for ($i=0;$i<$limit;$i++) {
		if ($i > @count($str_temp)-1) break;
		if (strstr($str_temp[$i],"<br")) break;
		$temp2 = $temp2." ".$str_temp[$i];
	}
	$temp2 = $temp2;
	return $temp2;
}

?>
