<?php  
             function tep_db_insert($table,$sql_data_array){           
			 	   reset($sql_data_array);
					 $query = 'insert into ' . $table . ' (';
						while (list($columns, ) = each($sql_data_array)) {
						$query .= $columns . ', ';}				    
						$query = substr($query, 0, -2) . ') values (';
				   reset($sql_data_array);
				        while (list(, $value) = each($sql_data_array)) {
						       switch ((string)$value) {
                                case 'now()':
                                      $query .= 'now(), ';
                                      break;
                                case 'null':
                                      $query .= 'null, ';
                                      break;
                                default:
                                      $query .= '\'' . tep_db_input($value) . '\', ';
                                      break;
                                    }
						 }
				        $query = substr($query, 0, -2) . ')';	
											
			            $result = mysql_query($query) or die(mysql_error());	
					    return $result;	
		  }
		  
    function tep_db_update($table,$sql_data_array,$parameters){  
	       reset($sql_data_array);
		    $query = 'update ' . $table . ' set ';
              while (list($columns, $value) = each($sql_data_array)) {
                  switch ((string)$value) {
                   case 'now()':
                       $query .= $columns . ' = now(), ';
                       break;
                  case 'null':
                      $query .= $columns .= ' = null, ';
                      break;
                 default:
                     $query .= $columns . ' = \'' . tep_db_input($value) . '\', ';
                 break;
                }
               }
             $query = substr($query, 0, -2) . ' where ' . $parameters;
			 $result = mysql_query($query) or die(mysql_error());
			 return $result;	
	     } 
		  
    function tep_db_input($string) {
         return addslashes($string);}
		 
	function tep_db_fetch_row($table,$parameters){ 	  
	    $sql = 'SELECT * FROM '.$table.' where '. $parameters;
		$query = mysql_query($sql) or die(mysql_error());
		$row = mysql_fetch_array($query);
	    return $row;	
	   }  
	   
     function tep_db_fetch_field($table,$parameters){ 	  
	   $sql = 'SELECT id,image FROM '.$table.' where '. $parameters;
		$query = mysql_query($sql) or die(mysql_error());
		while($row = mysql_fetch_array($query)){
		 $array_imgid[]=$row['id'];	
	     $array_field[]=$row['image'];	
	   }  
	   return array($array_imgid,$array_field);
	  }  
	 function tep_db_maxid($table){ 	  
	   $sqlg = 'SELECT MAX(id) As higID FROM '.$table;
	   $resultg = mysql_query($sqlg) or die(mysql_error());
	   $rowg = mysql_fetch_object($resultg);
	      $newID = $rowg->higID;
	           if(empty($newID))
				{
				$newID = "1";
				return $newID;
				}
				else
				{
				$newID = $newID + 1;
				return $newID;
				}				      
	          
	    }     
	function tep_db_fetch_table($table){ 
	    $tableresults=array();	  
	    $sql = 'SELECT * FROM '.$table;
		$query = mysql_query($sql) or die(mysql_error());
		$noof_tablerow=mysql_num_rows($query);
		while($row = mysql_fetch_object($query)){
		 $uname=$row->username;
		 array_push($tableresults,$uname);		
		}
	    return array($tableresults,$noof_tablerow);	
	   }     	 
		
		
   function mendatry_check_field($mendatry_check_array,$validate_check_array,$post_data,$edit_id){  
       $proceed_flag=0;
       $errormsg='';
       foreach($mendatry_check_array as $key=>$value){		 
		    if(!isset($post_data[$value]) || strlen($post_data[$value]) < 1){		    
		          $proceed_flag= $proceed_flag + 1;} 		     	
	        }
            if($proceed_flag>0)
 		    {
            $errormsg='<span lass=\"errFont\">Please ensure all mandatory fields (marked with *) are filled in.</span>';
		    }
            else {
	           $errormsg='';
			   
			   
			   foreach($post_data as $key=>$value)
			   {
			   if(list($data,$data_array)=array_multi_search($key,$validate_check_array ))
				{
				$data=substr($data,0,strlen($data)-1);
				$match_array=explode("_",$data);
				$result_matcharray = array_reverse($match_array);
				
				     if($result_matcharray[0]=='unique')
					 {
						$editcond='';
							if($edit_id>0){
							$editcond= " and ".$data_array['updateidmatch']."!=".$edit_id;}							
														
					                   $query="SELECT ".$data_array['name']."  FROM ".$data_array['tablename']." where ".$data_array['tablefield']."='$value' ".$editcond;	 
						$result=mysql_query($query);
						$row=mysql_num_rows($result);
						if($row){
						$proceed_flag= $proceed_flag + 1; 	
						$errormsg.="<br>".$data_array['error'];}
					}
					
					 if($result_matcharray[0]=='uniqueValidEmail')
					 {
						 if(!ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$',$value))
						{						
						$proceed_flag= $proceed_flag + 1; 	
						$errormsg.="<br>".$data_array['error1'];
						}
						else {
						$editcond='';
							if($edit_id>0){
							$editcond= " and ".$data_array['updateidmatch']."!=".$edit_id;}							
														
					                   $query="SELECT ".$data_array['name']."  FROM ".$data_array['tablename']." where ".$data_array['tablefield']."='$value' ".$editcond;	 
						$result=mysql_query($query);
						$row=mysql_num_rows($result);
						if($row){
						$proceed_flag= $proceed_flag + 1; 	
						$errormsg.="<br>".$data_array['error'];}
						}
						
					}
					
					if($result_matcharray[0]=='numeric')
					{					
						if(strlen($value)>0 && !is_numeric($value)){
						$proceed_flag= $proceed_flag + 1; 	
						$errormsg.="<br>".$data_array['error'];}
						
					}//end if
					
					if($result_matcharray[0]=='emailValid')
					{					
					   if(!ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$',$value))
						{						
						$proceed_flag= $proceed_flag + 1; 	
						$errormsg.="<br>".$data_array['error'];
						}						
					}//end if
					if($result_matcharray[0]=='match')
					{
						if(strlen($value)>0 && $post_data[$data_array['name']]!=$post_data[$data_array['name2']]){
						unset($post_data[$data_array['name2']]);
						$proceed_flag= $proceed_flag + 1; 	
						$errormsg.="<br>".$data_array['error'];}						
					}
				}//end list
			   }//foreach
            }
            return array($proceed_flag,$errormsg);
           }
		
		function array_multi_search($p_needle, $p_haystack)
		{		
		static $matchstr='';
		static $matchstr_array=array();
			if( !is_array( $p_haystack ) )
			{
			return false;
			}
		if(isset($p_haystack['name']))	
			if( $p_needle==$p_haystack['name'] )
			{
			$matchstr_array=$p_haystack;
			return true;
			}
		foreach( $p_haystack as $k=>$row )
		{
		$matchstr='';
			if(array_multi_search( $p_needle, $row ) )
			{		
			$matchstr.=$k."_";
			return array($matchstr,$matchstr_array);
			}
		}
		
		return false;
		} 
		
	function formatPhoneNumber($strPhone)
	{
		$strPhone = ereg_replace("[^0-9]",'', $strPhone);
			if (strlen($strPhone) != 10)
			{
			return $strPhone;
			}
		
		$strArea = substr($strPhone, 0, 3);
		$strPrefix = substr($strPhone, 3, 3);
		$strNumber = substr($strPhone, 6, 4);
		
		$strPhone = "(".$strArea.") ".$strPrefix."-".$strNumber;
		
		return ($strPhone);
	}
	
	
    function image_create_thumb($filename, $width, $height)
    {
        list($image["Width"], $image["Height"], $image["Type"]) = getimagesize($filename);


        switch($image["Type"])
        {
            case IMG_JPG:
            case IMG_JPEG:
                        $im_image = ImageCreateFromJPEG($filename);
                        break;
            case IMG_GIF:
                        $im_image = ImageCreateFromGIF($filename);
                        break;
            case IMG_PNG:
                        $im_image = ImageCreateFromPNG($filename);
                        break;
        }

        if($image["Width"] > $image["Height"])
        {
                $scale = $width / $image["Width"];
                $thumb_width = $width;
                $thumb_height  = floor($image["Height"]*$scale);
        }
        else
        {
                $scale = $height / $image["Height"];
                $thumb_height = $height;
                $thumb_width  = floor($image["Width"]*$scale);
        }

        $im_thumb = @ImageCreateTrueColor($thumb_width, $thumb_height) or die("Cannot Initialize new GD image stream");

        ImageCopyResized($im_thumb, $im_image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image["Width"], $image["Height"]);

        ImageDestroy($im_image);
        ImagePNG($im_thumb, $filename);
        ImageDestroy($im_thumb);
        return true;
    }
	
	function imageResize($width, $height, $target) 
	{ 
		if ($width > $height) 
		{ 
		$percentage = ($target / $width); 
		} 
		else 
		{ 
		$percentage = ($target / $height); 
		} 
		$width = round($width * $percentage); 
		$height = round($height * $percentage); 
		return "width=$width height=$height"; 
	}
	
	function tep_image($src, $alt = '', $width = '', $height = '', $parameters = '') 
	{
		$req_width= $width;
		$req_height=$height;
		$split_image=explode("/",$src);
		$imagename=$split_image[count($split_image)-1];
		
		if(file_exists($src) && $imagename)
		{
		$image = '<img src="' . $src . '"   alt="' .$alt . '"';
			if ($image_size = @getimagesize($src)) 
			{			
			$image .= imageResize($image_size[0], $image_size[1], $req_height);	
			} 		
			if(tep_not_null($parameters))
			$image .= $parameters;
			$image.=' border="0"';			
			$image .= ' >';		
			return $image;
	  } 
	  else return "";
  }
  
  
  function makePassword($len = 8)
  {
        $vowels = array('a', 'e', 'i', 'o', 'u', 'y');
        $confusing = array('I', 'l', '1', 'O', '0');
        $replacements = array('A', 'k', '3', 'U', '9');
        $choices = array(0 => rand(0, 1), 1 => rand(0, 1), 2 => rand(0, 2));
        $parts = array(0 => '', 1 => '', 2 => '');

        if ($choices[0]) $parts[0] = rand(1, rand(9,99));
        if ($choices[1]) $parts[2] = rand(1, rand(9,99));

        $len -= (strlen($parts[0]) + strlen($parts[2]));
        for ($i = 0; $i < $len; $i++)
        {
                if ($i % 2 == 0)
                {
                        do $con = chr(rand(97, 122));
                        while (in_array($con, $vowels));
                        $parts[1] .= $con;
                }
                else
                {
                        $parts[1] .= $vowels[array_rand($vowels)];
                }
        }
        if ($choices[2]) $parts[1] = ucfirst($parts[1]);
        if ($choices[2] == 2) $parts[1] = strrev($parts[1]);

        $r = $parts[0] . $parts[1] . $parts[2];
        $r = str_replace($confusing, $replacements, $r);
         //$r=makepassword();
        return $r ;
    }
  
  
function display_error_msg($msg){
$st='';
if(strlen($msg)>0)
{
$st='
<table width="100%"  border="0" cellpadding="0"  class="errborder" align="center">
  <tr>
    <td>
<table width="100%"  border="0" cellpadding="2" cellspacing="0" align="center">
  <tr>
    <td width="91%" class="errFont">'.$msg.'</td>
  </tr>
</table>
</td>
  </tr>
</table>';
}
return $st;
}

 ?>	