<?php
/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	set_time_limit(5);
	require('includes/application_top.php');  
	require('includes/classes/pdfTable.php'); 
	require('includes/classes/ldif.php');
	
	tep_get_last_access_file();
	//$delimiter_array=array(array('id'=>',', 'text' => 'comma'),array('id'=>':','text'=>'colon'),array('id'=>';','text'=>'semicolon'),array('id'=>'t','text'=>'tab'));
	$delimiter_array=array(array('id'=>',', 'text' => 'comma'));
	$format_array=array(array('id' =>'E','text'=>'Eudora'),array('id' => 'O','text' =>'Outlook'),array('id' => 'OE','text'=>'Outlook Express'),array('id'=>'l','text'=>'LDIF'),array('id'=>'t','text'=>'TAB file'),array('id' =>'c','text'=>'CSV file'),array('id'=>'x','text' =>'Text file'));
	$action=(isset($HTTP_GET_VARS['action'])?$HTTP_GET_VARS['action']:'');
	$filename=(isset($HTTP_POST_VARS['import_file']['name'])?$HTTP_POST_FILES['import_file']['name']:'');
	$delimiter=(isset($HTTP_POST_VARS['import_delimiter'])?$HTTP_POST_VARS['import_delimiter']:'');
	$format=(isset($HTTP_POST_VARS['format'])?$HTTP_POST_VARS['format']:'');
 	$temp_delimiter=$delimiter;
	if($action=='customers_import'){ 
		if (!is_writeable(DIR_FS_CATALOG_IMAGES)) {
			$directory_writeable = false;
			$error=sprintf(ERROR_DIRECTORY_NOT_WRITEABLE, DIR_FS_CATALOG_IMAGES);
		} else {
			$up_load = new upload('import_file', DIR_FS_CATALOG_IMAGES);
			$file_name = $up_load->filename;
			
			$field['firstname']=-1;
			$field['lastname']=-1;
			$field['emailaddress']=-1;
			
			$unimport_cnt=0;
			$import_cnt=0;
			$error_field="";
			$error_head_field.="First Name,Last Name,Email Address,Reason " ." \n";
			
			if($format!='l'){
				$handle=@fopen(DIR_FS_CATALOG_IMAGES . $file_name,"r");
				if ($handle){ 
					$details=@fread($handle,filesize(DIR_FS_CATALOG_IMAGES . $file_name));
					
					//$records_array=split("\n",$details);
					if(strpos($details,"\r\n")!==false) {
						$records_array=split("\r\n",$details);
					} else {
						$records_array=split("\n",$details);
					}	
					//$records_array=split("\r\n",$details);
					//$records_array=split("\n",$details);
					
					if($delimiter=='t') $delimiter=chr(9);
					
					$head_arr=split($delimiter,$records_array[0]);
					
					switch ($format){
						case 'O':
						case 'OE':
						case 'c':
						case 't':
						case 'x':
							
							for($hcnt=0;$hcnt<sizeof($head_arr);$hcnt++){
								if(strpos($head_arr[$hcnt],'"')!==false)
									$head_arr[$hcnt]=str_replace('"','',$head_arr[$hcnt]);
								$field_name=strtolower(trim($head_arr[$hcnt]));
								$field_name=str_replace(" ","",$field_name);
								switch($field_name){
									case 'firstname' :
										$field['firstname']=$hcnt;
										break;
									case 'lastname':
										$field['lastname']=$hcnt;
										break;
									case 'emailaddress':
									case 'e-mailaddress':
										$field['emailaddress']=$hcnt;
										break;		
								}
								$rcnt=1;
							}
						break;
						case 'E':
							$field['firstname']=4;
							$field['lastname']=3;
							$field['emailaddress']=1;
							$rcnt=0;
						break;
					}
					
					if(sizeof($head_arr)<3 )
						$error='Required parameters are missing';
					else if($field['firstname']!=-1 && $field['lastname']!=-1 && $field['emailaddress']!=-1){
						$ucnt=0;
						//for($icnt=1;$icnt<sizeof($records_array);$icnt++)
						for($icnt=$rcnt;$icnt<sizeof($records_array);$icnt++){
						
							$customer_details=split($delimiter,$records_array[$icnt]);
							
							$first_name=trim($customer_details[$field['firstname']]);
							$last_name=trim($customer_details[$field['lastname']]);
							$email_address=trim($customer_details[$field['emailaddress']]);
							if(strpos($first_name,'"')!==false) $first_name=str_replace('"','',$first_name);
							if(strpos($last_name,'"')!==false) $last_name=str_replace('"','',$last_name);
							if(strpos($email_address,'"')!==false) $email_address=str_replace('"','',$email_address);
							
							
							$email_error=false; $incomplete_error=false; $no_error_flag=true;$invalid_email_error=false;
							
							if(count($customer_details)<3)
								$incomplete_error=true;
							
							if($first_name=='' || $last_name=='' || $email_address=='')	
								$incomplete_error=true;
							if($first_name=='' && $last_name=='' && $email_address=='')	
								continue;
							if(count($customer_details)>=3){
								if($email_address=='' || !tep_validate_email($email_address)) { 
									$invalid_email_error=true;
								}else {	
									$chk_email_query=tep_db_query("select count(*) as total from ".TABLE_CUSTOMERS." where customers_email_address='" . tep_db_input($email_address) . "'");
									$chk_email_array=tep_db_fetch_array($chk_email_query);
									if($chk_email_array['total']>0)
										$email_error=true;
								}	
							}	
							
							if($email_error==false && $incomplete_error==false && $invalid_email_error==false){
								$first_name=tep_db_prepare_input($first_name); $last_name=tep_db_prepare_input($last_name);
								$user_name=tep_create_user_name($first_name,$last_name);
								$query_fields='';
								$query_fields='(customers_firstname,customers_lastname,customers_email_address,customers_username';
								$value_fields="('$first_name','$last_name','$email_address','".tep_create_user_name($first_name,$last_name)."'";
								
								if($HTTP_POST_VARS['chk_autopwd']=='Y'){
									$makePassword = randomize();
									$password=tep_encrypt_password($makePassword);
									$encription_style=(defined('ENCRYPTION_STYLE'))?ENCRYPTION_STYLE:'O';
									$query_fields.=',customers_password,encryption_style';
									$value_fields.=",'$password','$encription_style'";
								}
								
								$query_fields.=')';
								$value_fields.=')';
								tep_db_query('insert into '.TABLE_CUSTOMERS.$query_fields.' values '.$value_fields);
								$customers_id=tep_db_insert_id();
								tep_db_query("insert into " . TABLE_ADDRESS_BOOK. " (customers_id,entry_firstname,entry_lastname,entry_country_id) values ('$customers_id','" . tep_db_input($first_name) . "','" . tep_db_input($last_name) . "','" . STORE_COUNTRY ."')");
								$address_id=tep_db_insert_id();
								tep_db_query("update " . TABLE_CUSTOMERS . " set customers_default_address_id = '" . (int)$address_id . "' where customers_id = '" . (int)$customers_id . "'");
								
								if($HTTP_POST_VARS['chk_autoemail']=='Y'){
									$merge_details=array();
									$merge_details[TEXT_FN]=$first_name;
									$merge_details[TEXT_LN]=$last_name;
									$merge_details[TEXT_GR] = EMAIL_GREET_NONE;
									
									$merge_details[TEXT_SM]=STORE_NAME;
									$merge_details[TEXT_SN]=STORE_OWNER;
									$merge_details[TEXT_SE]=STORE_OWNER_EMAIL_ADDRESS;
									$merge_details[TEXT_LE]=$email_address;
									
									$merge_details[TEXT_LP]=$makePassword;
									
									$send_details[0]['to_name'] = $first_name . ' ' . $last_name;
									$send_details[0]['to_email'] = $email_address;
									$send_details[0]['from_name']=STORE_OWNER;
									$send_details[0]['from_email']=STORE_OWNER_EMAIL_ADDRESS;
									
									tep_send_default_email("CUS",$merge_details,$send_details);
								}
								$import_cnt++;
							} else if($invalid_email_error==true){
								$no_error_flag=false;
								$unimport_cnt++;
								$error_field.=$first_name.','.$last_name.','.$email_address.',Invalid Email' ."\n";
							} elseif($email_error==true){ 
								$unimport_cnt++;
								$no_error_flag=false;
								$error_field.=$first_name.','.$last_name.','.$email_address.',Duplicate Email' ."\n";
							} elseif($incomplete_error==true){
								$unimport_cnt++;
								$no_error_flag=false;
								$error_field.=$first_name.','.$last_name.",".$email_address.',InComplete Data Field' ."\n";
							}
						}  //for 
						
						if($error_field=='')
							$error_field.="No Error Data Found. \n";
							
						$error_field=$error_head_field . $error_field;	
						tep_delete_temp_files("rejected_customers" . $login_id);
						$report_filename=sprintf("rejected_customers%s_%s",$login_id,time()).'.csv';
						tep_write_text_file($report_filename,$error_field);
					} else 
						$error='Required parameters are missing';
				}	
			}	else {
				$delimiter='';
				$ext_pos=strpos($file_name,'.');
				$ext=substr($file_name,$ext_pos+1,(strlen($file_name)-$ext_pos));
				if(strtolower($ext)=='ldif'){
					$ldifReader = new LdifReader(DIR_FS_CATALOG_IMAGES . $file_name,$continuous_mode);
					//while we have a valid entry, 
					while($entry = $ldifReader->readEntry()){
					  $first_name=$entry->attributes['givenName'][0];
					  $last_name=$entry->attributes['sn'][0];
					  $email_address=$entry->attributes['mail'][0];
						if(strpos($first_name,'"')!==false) $first_name=str_replace('"','',$first_name);
						if(strpos($last_name,'"')!==false) $last_name=str_replace('"','',$last_name);
						if(strpos($email_address,'"')!==false) $email_address=str_replace('"','',$email_address);
						
						$email_error=false; $incomplete_error=false; $no_error_flag=true;$invalid_email_error=false;
						
						if($first_name=='' || $last_name=='' || $email_address=='')	
							$incomplete_error=true;
						if($first_name=='' && $last_name=='' && $email_address=='')	
							continue;
							
							if($email_address=='' || !tep_validate_email($email_address)) { 
								$invalid_email_error=true;
							}else {	
								$chk_email_query=tep_db_query("select count(*) as total from ".TABLE_CUSTOMERS." where customers_email_address='" . tep_db_input($email_address) . "'");
								$chk_email_array=tep_db_fetch_array($chk_email_query);
								if($chk_email_array['total']>0)
									$email_error=true;
							}	
						
						if($email_error==false && $incomplete_error==false && $invalid_email_error==false){
							$first_name=tep_db_prepare_input($first_name); $last_name=tep_db_prepare_input($last_name);
							$user_name=tep_create_user_name($first_name,$last_name);
							$query_fields='';
							$query_fields='(customers_firstname,customers_lastname,customers_email_address,customers_username';
							$value_fields="('$first_name','$last_name','$email_address','".tep_create_user_name($first_name,$last_name)."'";
							
							if($HTTP_POST_VARS['chk_autopwd']=='Y'){
								$makePassword = randomize();
								$password=tep_encrypt_password($makePassword);
								$encription_style=(defined('ENCRYPTION_STYLE'))?ENCRYPTION_STYLE:'O';
								$query_fields.=',customers_password,encryption_style';
								$value_fields.=",'$password','$encription_style'";
							}
							
							$query_fields.=')';
							$value_fields.=')';
							tep_db_query('insert into '.TABLE_CUSTOMERS.$query_fields.' values '.$value_fields);
							$customers_id=tep_db_insert_id();
							tep_db_query("insert into " . TABLE_ADDRESS_BOOK. " (customers_id,entry_firstname,entry_lastname,entry_country_id) values ('$customers_id','" . tep_db_input($first_name) . "','" . tep_db_input($last_name) . "','" . STORE_COUNTRY ."')");
							$address_id=tep_db_insert_id();
							tep_db_query("update " . TABLE_CUSTOMERS . " set customers_default_address_id = '" . (int)$address_id . "' where customers_id = '" . (int)$customers_id . "'");
							
							if($HTTP_POST_VARS['chk_autoemail']=='Y'){
								$merge_details=array();
								$merge_details[TEXT_FN]=$first_name;
								$merge_details[TEXT_LN]=$last_name;
								$merge_details[TEXT_GR] = EMAIL_GREET_NONE;
								
								$merge_details[TEXT_SM]=STORE_NAME;
								$merge_details[TEXT_SN]=STORE_OWNER;
								$merge_details[TEXT_SE]=STORE_OWNER_EMAIL_ADDRESS;
								$merge_details[TEXT_LE]=$email_address;
								
								$merge_details[TEXT_LP]=$makePassword;
								
								$send_details[0]['to_name'] = $first_name . ' ' . $last_name;
								$send_details[0]['to_email'] = $email_address;
								$send_details[0]['from_name']=STORE_OWNER;
								$send_details[0]['from_email']=STORE_OWNER_EMAIL_ADDRESS;
								
								tep_send_default_email("CUS",$merge_details,$send_details);
							}
							$import_cnt++;
						} else if($invalid_email_error==true){
							$no_error_flag=false;
							$unimport_cnt++;
							$error_field.=$first_name.','.$last_name.','.$email_address.',Invalid Email' ."\n";
						} elseif($email_error==true){ 
							$unimport_cnt++;
							$no_error_flag=false;
							$error_field.=$first_name.','.$last_name.','.$email_address.',Duplicate Email' ."\n";
						} elseif($incomplete_error==true){
							$unimport_cnt++;
							$no_error_flag=false;
							$error_field.=$first_name.','.$last_name.",".$email_address.',InComplete Data Field' ."\n";
						}
					  
					}
					// close the file
					$ldifReader->done();
					
					if($error_field=='')
							$error_field.="No Error Data Found. \n";
							
						$error_field=$error_head_field . $error_field;	
						tep_delete_temp_files("rejected_customers" . $login_id);
						$report_filename=sprintf("rejected_customers%s_%s",$login_id,time()).'.csv';
						tep_write_text_file($report_filename,$error_field);
				}	 else $error='Required parameters are missing';
			}
		}
	} else if($action=='excel'){
		$filename=isset($HTTP_POST_VARS['filename'])?$HTTP_POST_VARS['filename']:'';
		header('Content-type: application/x-octet-stream');
		header('Content-Disposition: attachment; filename="' . $filename . '"');
		readfile(DIR_FS_CATALOG . "images/".$filename); 
		exit;
  }
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/http.js"></script>

<style type="text/css">
	#pup { border:solid 1px #CCCCCC; position:absolute; visibility:hidden; z-index:200; width:130;}
</style>

<script>
		function validate_image(){ 
		var image_types=Array(".txt",".csv",".tab",".ldif");
		var frm = document.frm_import;
		var error = "";
		if(frm.elements["import_file"].value=='')
			error="* <?php echo ERR_UPLOAD_FILE;?>\n";
		else if (frm.elements["import_file"].value!="" && !check_mime_type(frm.elements["import_file"],image_types)){
			error="* <?php echo ERR_CUSTOMER_UPLOAD_TYPE; ?>\n";
		}
		if(error!=""){
			alert(error);
			return false;
		} else {
			document.frm_import.action='<?php echo FILENAME_CUSTOMERS_IMPORT;?>?action=customers_import';
		}
		return true;
	}

</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();" >
<script language="javascript" src="includes/popup.js"></script>
<script language="javascript">
var baby=new Array();
	baby[1]="";
	function show_help(message){
		popwidth=150;
		baby[1]='<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td style="color:#0000FF;" class="smalltext" align="left">'+message+'</td></tr></table>';		
		if (baby[1]!=""){
			popup("1","#363636");
		}
	}
	
	function do_upload(){
		document.frm_import.action='<?php echo FILENAME_CUSTOMERS_IMPORT;?>?action=excel';
		document.frm_import.submit();
	}
	function do_hover_change(element,eve){ 
	 	 if (eve==2)
			element.firstChild.src=element.firstChild.src.replace(".gif","_hover.gif");
		else
			element.firstChild.src=element.firstChild.src.replace("_hover.gif",".gif");
	}	
	
	function do_change(val) { 
		document.frm_import.import_delimiter.disabled=false;
		if(val!='l') {
			for (j = document.frm_import.import_delimiter.length - 1; j>=0; j--) 
				document.frm_import.import_delimiter.remove(j);
		}	
		switch(val) {
			case 'E':
			case 'O':
			case 'OE':
			case 'c':
				do_add('Comma',',');
			break;
			case 't':
				do_add('Tab','t');
			break;
			case 'x':
				do_add('Comma',',');
				do_add('Colon',':');
				do_add('Semicolon',';');
				do_add('Tab','t');
			break;
			case 'l':
				document.frm_import.import_delimiter.disabled=true;
			break
			
		}
	}
	function do_add(text,value) {
		var oOption = document.createElement("OPTION");
		oOption.text=text;
		oOption.value=value;
		if(document.frm_import.import_delimiter.add.length==2){
		 document.frm_import.import_delimiter.add(oOption,null);
		} else {
		 document.frm_import.import_delimiter.add(oOption,pos);
		}	
	}
	
</script>
<!-- header //-->
<?php 
require(DIR_WS_INCLUDES . 'header.php'); ?>

<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2" onMouseOut="javascript:clearTimeout(1)" >
  <tr >
<!-- body_text //-->
    <td width="100%" valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
			<tr>
				<td><?php echo tep_draw_form('frm_import',FILENAME_CUSTOMERS_IMPORT,'action=customers_import','post',' onSubmit="javascript:return validate_image();" enctype="multipart/form-data"');?>
					<table cellpadding="2" cellspacing="5" border="0" width="100%">
						<tr>
							<td class="pageHeading" colspan="2"><?php echo HEADING_TITLE;?></td>
						</tr>
						<tr>
							<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','10','10');?></td>
						</tr>	
						<?php if($error!=''){?>
						<tr>
							<td colspan="2"><font color="#FF0000" class="main"><?php echo $error;?></font></td>
						</tr>
						<?php }?>	
						<tr>
							<td class="main" width="15%"><?php echo TEXT_IMPORT_FILE ;?></td>
							<td class="main"><?php echo tep_draw_file_field('import_file');?></td>
						</tr>
						<tr>
							<td class="main" colspan="2"><?php echo 'These file formats are supported: "Eudora, Outlook, Outlook Express, LDIF, TAB, CSV and Text file".';?><span onMouseOver="javascript:show_help('Column Headings should be Firstname, Lastname, Emailaddress');" onMouseOut="javascript:kill();"><?php  echo tep_image(DIR_WS_LANGUAGES.$language.'/images/buttons/help_icon.gif'); ?></span></td>
						</tr>
						<tr>
							<td class="main"><?php echo TEXT_FILE_FORMATS;?>	</td>
							<td class="main"><?php echo tep_draw_pull_down_menu('format',$format_array,$format,'onChange="javascript:do_change(this.value);"');?></td>
						</tr>	
						<tr>
							<td class="main"><?php echo TEXT_IMPORT_DELIMITER;?>	</td>
							<td class="main"><?php echo tep_draw_pull_down_menu('import_delimiter',$delimiter_array,$delimiter);?></td>
						</tr>	
						<tr>	
							<td colspan="2" class="main"><label for="chk_autopwd"><?php echo tep_draw_checkbox_field('chk_autopwd','Y') . '&nbsp;' .TEXT_AUTO_PASSWORD;?></label>&nbsp;<span onMouseOver="javascript:show_help('If chosen create a strong password including some non-alphanumeric characters will be set');" onMouseOut="javascript:kill();"><?php  echo tep_image(DIR_WS_LANGUAGES.$language.'/images/buttons/help_icon.gif'); ?></span></td></tr>
						<tr><td colspan="2" class="main"><label for="chk_autoemail"><?php echo tep_draw_checkbox_field('chk_autoemail','Y') . '&nbsp;' . TEXT_AUTO_EMAIL;?></label>&nbsp;<span onMouseOver="javascript:show_help('The new customers will be sent an email including personal details');" onMouseOut="javascript:kill();"><?php  echo tep_image(DIR_WS_LANGUAGES.$language.'/images/buttons/help_icon.gif'); ?></span></td></tr>	
						<tr>
							<td class="main" colspan="2" align="center"><?php echo tep_image_submit('button_confirm.gif','Confirm')?></td>
						</tr>
						<?php if($action=='customers_import' && $error==''){?>
							<tr><td colspan="2"><table cellpadding="0" cellspacing="0" border="0" width="100%">
								<tr class="dataTableHeadingRow">
									<td class="main" width="40"><?php echo TEXT_IMPORT_CUSTOMERS;?></td>
									<td width="10"></td>
									<td class="main" width="40"><?php echo TEXT_REJECTED_CUSTOMERS;?></td>
									<td width="420"></td>
								</tr>
								<tr>
									<td class="main" align="right"><?php echo $import_cnt;?>	</td>
									<td width="10"></td>
									<td class="main" align="right"><?php echo $unimport_cnt;?></td>
									<td width="420"></td>
								</tr>	
								<tr>
									<td colspan="4"><?php echo tep_draw_separator('pixel_trans.gif','10','10');?></td>
								</tr>	
								<tr>
									<td colspan="4" class="main">
										<table cellpadding="0" cellspacing="0" border="0" width="100%">
											<tr>
												<td  width="25" style="cursor:hand;cursor:pointer" onClick="javascript:do_upload();"onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,1);"><?php echo tep_image(DIR_WS_IMAGES . 'template/img_download.gif', 'Download','','',' align="absmiddle"');?></td>
												<td class="main"><?php echo '<a href="javascript:do_upload();" ><font color="#0000ff">' .TEXT_CUSTOMERS_REJECTED . '</font></a>'; ?></td>
											</tr>
										</table>
									</td>			
									<?php //echo '<a href="javascript:do_upload();" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,1);"><font color="#0000ff">' .tep_image(DIR_WS_IMAGES . 'template/img_download.gif', 'Download','','',' align="absmiddle"') . '&nbsp;' .TEXT_CUSTOMERS_REJECTED . '</font></a>';?>
								</tr>	
								<input type="hidden" name="filename" value="<?php echo tep_output_string($report_filename);?>">
							</table></td></tr>	
						<?php }?>	
					</table></form>		
				</td>
			</tr>		
    	</table>
	</td>
<!-- body_text_eof //-->
  </tr>
</table>
<script>
	var value=document.frm_import.format.value;
	do_change(value);
	<?php if($temp_delimiter!=''){?>
		document.frm_import.import_delimiter.value='<?php echo $temp_delimiter;?>';
	<?php }?>	
</script>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php');

   function randomize() {
            $salt = "abchefghjkmnpqrstuvwxyz0123456789";
            srand((double)microtime()*1000000); 
            $i = 0;
    	    while ($i <= 7) {
    		$num = rand() % 33;
    		$tmp = substr($salt, $num, 1);
    		$pass = $pass . $tmp;
    		$i++;
  	    }
  	    return $pass;
          }
	
		  
	function tep_create_user_name($first_name,$last_name){
	
	$user_name=$first_name.$last_name.rand(1,100);
	$chk_user_name_query=tep_db_query("select count(*) as total from ".TABLE_CUSTOMERS." where customers_username='" . tep_db_input($user_name) . "'");
	$chk_user_name_array=tep_db_fetch_array($chk_user_name_query);
	
	if($chk_user_name_array['total']>0)
		tep_create_user_name($first_name,$last_name);
	else
		return tep_db_prepare_input($user_name);
	
	}

 ?>