<?php // Check to ensure this file is included in Freeway!
defined('_FEXEC') or die(); ?>
<script language="javascript">
var img_src="<?php echo HTTP_SERVER.DIR_WS_ADMIN.'images/';?>";
var selectedTab;
var tax_rates = new Array();
var lang = new Array();
var display_flag=1;
<?php
$tax_class_query = tep_db_query("SELECT tax_class_id, tax_class_title from " . TABLE_TAX_CLASS . " order by tax_class_title");
    	while ($tax_class = tep_db_fetch_array($tax_class_query)) {
		 echo 'tax_rates[' . $tax_class['tax_class_id'] . '] = ' . tep_get_tax_rate_value($tax_class['tax_class_id']) . ';' . "\n";
   		 }  
?>
<?php 
 $languages = tep_get_languages();
    for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
    	echo "lang[".$i."]=".$languages[$i]['id']."\n";
    }
?>
function doRound(x, places) {
  return Math.round(x * Math.pow(10, places)) / Math.pow(10, places);
}

function getTaxRate() {

if(document.new_coupon)
  var selected_value = document.new_coupon.coupon_tax_class_id.selectedIndex;
if(document.new_coupon && selected_value)
  var parameterVal = document.new_coupon.coupon_tax_class_id[selected_value].value;
  if ( (parameterVal > 0) && (tax_rates[parameterVal] > 0) ) {
    return tax_rates[parameterVal];
  } else {
    return 0;
  }
}

function updateGross() { 
  var taxRate = getTaxRate();
if(document.new_coupon){
  var grossValue = document.new_coupon.coupon_amount.value;
 //alert (document.new_coupon.coupon_amount.value);
// for(i=0;i<document.new_coupon.length;i++)
 //	alert(document.new_coupon[i].name);
  var percentage = "";
  
	if(grossValue.lastIndexOf("%")!=-1){
		grossValue = grossValue.substring(0,grossValue.length-1);
		percentage = "%";
	}
  if (taxRate > 0) {
    grossValue = grossValue * ((taxRate / 100) + 1);
  }
  if(percentage=="%"){
  	if(isNaN(doRound(grossValue, 4)))
  		document.new_coupon.coupon_amount_gross.value = doRound(grossValue, 4).toFixed(4);
	else 
		document.new_coupon.coupon_amount_gross.value = doRound(grossValue, 4).toFixed(4)+"%";
		}
  else
  	document.new_coupon.coupon_amount_gross.value = doRound(grossValue, 4).toFixed(4);
  }
}

function updateNet() {
  var taxRate = getTaxRate();
  var netValue = document.new_coupon.coupon_amount_gross.value;
  var percentage = "";
	if(netValue.lastIndexOf("%")!=-1){
		netValue = netValue.substring(0,netValue.length-1);
		percentage = "%";
	}
  if (taxRate > 0) {
    netValue = netValue / ((taxRate / 100) + 1);
  }
  if(percentage=="%")
  	if(isNaN(doRound(netValue, 4)))
	  	document.new_coupon.coupon_amount.value = doRound(netValue, 4).toFixed(4);
	else
		document.new_coupon.coupon_amount.value = doRound(netValue, 4).toFixed(4)+"%";
  else
	document.new_coupon.coupon_amount.value = doRound(netValue, 4).toFixed(4);
}
function validatemail(){
	var customers_email_address = document.new_coupon.customers_email_address.value;
	if(customers_email_address==''){
	alert('Please select a Customer or Newsletter Group from the drop down list to proceed');
	return false;
	}else return true;
	
}
function send_email(id){
	command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=send_mail_to_user');?>&id="+id; 
	do_post_command('new_coupon',command);
}
function preview_email(id){
	if(validatemail()){
	command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=preview_email');?>&id="+id; 
	do_post_command('new_coupon',command);
	}
}
var previous_id='';
function expand_category(id,category,s,sta){
		if(category==2 && id==0){
			display_flag=0;
			document.getElementById('img_new').firstChild.src=img_src+'template/img_new_hover.gif';
			if(document.getElementById('no_coupon'))
				document.getElementById('no_coupon').style.display='none';
		}
		if(category==1 && id==0){
			if(document.getElementById('no_coupon'))
				document.getElementById('no_coupon').style.display='';
		display_flag=1;
		document.getElementById('img_new').firstChild.src=img_src+ 'template/img_new.gif' ;
		//document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('_hover.gif','.gif');
		}
		if(!sta) sta="";
		if(category=='1' && document.getElementById("page_''") && sta!=""){
 	 		id=parseInt(document.getElementById("page_''").options[document.getElementById("page_''").selectedIndex].value);
 	 		if(sta=='next' && document.getElementById("page_''")) { 
 	 			if(document.getElementById("page_''").length==document.getElementById("page_''").selectedIndex+1) return;
 	 			id+=1;
 	 		}else if(sta=='prev') {
 	 			if(document.getElementById("page_''").selectedIndex==0) return;
 	 			id-=1;
 	 		}
 	 		if(id<=1) id=1;
 	 	}else if(category=='1' && sta=="") id="";
 	 	var res_display=document.getElementById("res_display"); 	 	 	 	 	 	 	 	 	 		 	  	 	
 	 	if(res_display) res_display.style.display="none"; 	 		 	
 	 	 if(document.new_coupon && document.new_coupon.coupon_id && document.new_coupon.coupon_id.value>0)  { 
			var old_ids=document.new_coupon.coupon_id.value;			
			var pre_data_id=document.getElementById("data_"+old_ids);
			var pre_head=document.getElementById("head_"+old_ids);			
			var loadings=document.getElementById("loading");
			if(loadings) loadings.style.display="none";
			if(category==1 && pre_head && pre_head.innerHTML!='') pre_head.innerHTML="";									
		 }
		if(category!=1 && document.new_coupon && document.new_coupon.coupon_id && (document.new_coupon.coupon_id.value=="" ||document.new_coupon.coupon_id.value==0 )) document.new_coupon.coupon_id.value=id; 	 	
			if(id>0 && document.new_coupon.coupon_id.value>0 && document.new_coupon.coupon_id.value!=id){			   
			   if(pre_head && pre_head.style.display=="") pre_head.style.display="none";
			   if(pre_data_id && pre_data_id.style.display=="none") pre_data_id.style.display="";
			   document.new_coupon.coupon_id.value=id;			   
			}							 								 	 	 	 
 	 	if(category){ 
 	 		var span_details=document.getElementById("span_details");
 	 		var span_edit=document.getElementById("span_edit");			
			var span_delete=document.getElementById("span_delete");	
			var span_email=document.getElementById("span_email");	
			var span_report=document.getElementById("span_report");	
			var span_preview=document.getElementById("span_preview");						
			var data_id=document.getElementById("data_"+id);	
			var head=document.getElementById("head_"+id);
			var img_save=document.getElementById("img_save");	
			var status = document.status.status.value;		
			var command="";																		 
 	 		switch(category){ 	 			
 	 			case '1': 
					   var ser="";
 	 			       var page="";
					  
					   		//var status = document.status.status.value;
					   		status_cmd = '&status='+status;
							
 	 			       if(document.getElementById('fst_nav')) document.getElementById('fst_nav').style.display='none';
					   if(id!="") page="&page="+id; 
					   
 	 			       command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=page_navigation');?>"+page+ser+status_cmd;
					   if(document.getElementById("ajax_details")) document.getElementById("ajax_details").innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>"; 	 			   					   
 	 			break;
				case 2:
					display_flag=0;
					document.getElementById('img_new').firstChild.src=img_src+'template/img_new_hover.gif';
				 		if(previous_id!=''){
							if(document.getElementById('head_'+previous_id))
								document.getElementById('head_'+previous_id).innerHTML='';	 
								previous_id='';
						}					 	
 	 					command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=edit');?>"; 	 		 	 					 	 					 	 				 	 		
 	 			break; 	
 	 			case 3:
					display_flag=1;
					document.getElementById('img_new').firstChild.src=img_src+'template/img_new.gif';
 	 			   if(head && head.style.display==""){  
 	 			   	  if(span_details.style.display==""){
 	 			   	    head.style.display="none"; 	 			   	    
 	 			   	  	data_id.style.display=""; 	 			   	  	
 	 			   	  }else { 
 	 			   	  	if(span_edit.style.display=="") span_edit.style.display="none";
 	 			   	  	if(span_delete.style.display=="") span_delete.style.display="none"; 
						if(span_email.style.display=="") span_email.style.display='none';	
						if(span_report.style.display=="") span_report.style.display='none'; 
						if(span_preview.style.display=="") span_preview.style.display='none';
						span_preview.innerHTML="";
						span_edit.innerHTML="";
						 head.style.display="none"; 	 			   	    
 	 			   	  	data_id.style.display=""; 
 	 			   	  //	span_details.style.display="";
 	 			   	  	if(img_save) img_save.style.display="none"; 
 	 			   	  	if(document.getElementById("img_ids")){
 	 			   	  		var tr_obj=document.getElementById("img_ids");
							if(tr_obj.childNodes[1].childNodes[0].src==img_src+'template/img_edit_hover.gif')	
 	 			   	  			tr_obj.childNodes[1].childNodes[0].src=img_src+'template/img_edit.gif';
 	 			   	  		if(tr_obj.childNodes[2].childNodes[0].src==img_src+'template/img_trash_hover.gif')	
 	 			   	  			tr_obj.childNodes[2].childNodes[0].src=img_src+'template/img_trash.gif';
						    if(tr_obj.childNodes[3].childNodes[0].src==img_src+'template/img_sms_hover.gif')	
 	 			   	  			tr_obj.childNodes[3].childNodes[0].src=img_src+'template/img_sms.gif';	 	 			   	  			
						    if(tr_obj.childNodes[4].childNodes[0].src==img_src+'template/img_copy_hover.gif')	
 	 			   	  			tr_obj.childNodes[4].childNodes[0].src=img_src+'template/img_copy.gif';	 	 			   	  			
						}	 			   	  	  	 			   	  	 	 			   	  	
 	 			   	  }	 	 			    			   	   	 			   
 	 			 }else {  
				 		if(previous_id!='')
							if(document.getElementById('head_'+previous_id))
								document.getElementById('head_'+previous_id).innerHTML='';	 			 	
 	 			 	if(data_id && data_id.style.display=="") data_id.style.display="none";
 	 			 	if(pre_head && pre_head.style.display=="") pre_head.style.display="none"; 	 			 	
 	 			 	if(head) {
 	 			 	 	head.style.display=""; 	 			 	 	 	 			 	  	 			
 	 			 		head.innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>";	 			 		
 	 			 	}
 	 			 	command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=get_details');?>&id="+id; 	 	
					previous_id=id;
 	 			 } 	 			  	 			  	 			
 	 			break;
 	 			case 4:
						span_edit.style.display=""; 	 					
 	 					span_report.style.display=span_details.style.display=span_delete.style.display=span_email.style.display="none";
						
 	 					span_edit.innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>"; 	 					
 	 					command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=edit');?>&id="+id; 	 		 	 					 	 					 	 				 	 		
 	 			break; 	 			
 	 			case 5:	 	 				 	 				 	 		 	 				
 	 					span_delete.style.display="";
 	 					span_report.style.display=span_edit.style.display=span_details.style.display="none";
 	 					span_delete.innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>";
 	 					command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=delete');?>&id="+id; 	 	 	 					 	 					 	 			
 	 			break; 
				case 6:
						
						command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=st_change');?>&status="+status; 	 
				break;	
				case 7:
						//status = document.status.status.value;
						command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=tbl_write');?>&status="+status; 
				break;
				case 8:
						span_email.style.display="";
						span_report.style.display=span_edit.style.display=span_details.style.display=span_delete.style.display="none";
						command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=email');?>&id="+id; 
				break;
				case 9: 
						span_email.style.display="";
						span_email.innerHTML="";
						span_edit.style.display=span_details.style.display=span_delete.style.display="none";
						
				break;
				case 10:
						span_report.style.display="";
						span_email.style.display=span_edit.style.display=span_details.style.display=span_delete.style.display="none";
						command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=report');?>&id="+id; 
				break;

 	 		}//alert(command);
 	 		if(command!="") do_get_command(command);
			
 	 	}		
 	 }
function do_result(result){  //alert(result);
	
			if(result.substr(0,11)=='get_details')  print_details('get_details',result.substr(11));
		    else  if(result.substr(0,4)=='edit') print_details('edit',result.substr(4));

			 else if(result.substr(0,4)=='newc')  print_details('newc',result.substr(4));
			 else  if(result.substr(0,6)=='delete') print_details('delete',result.substr(6));
			 else  if(result.substr(0,6)=='status') expand_category(result.substr(6),4);
			 else if(result.substr(0,9)=='st_change') print_details('st_change',result.substr(9));
			 else  if(result.substr(0,14)=='success_delete')  print_details('success_delete',result.substr(0,14));
			 else  if(result.substr(0,15)=='page_navigation') print_details('page_navigation',result.substr(15));
			 else  if(result.substr(0,6)=='update') print_details('update',result.substr(6));
			 else if(result.substr(0,7)=='preview') print_details('preview',result.substr(7));
			 else if (result.substr(0,10)=='coupon_new')expand_category(0,7);
			 else if(result.substr(0,11)=='insert_newc') print_details('insert_newc',result.substr(11));
			 else if(result.substr(0,5)=='email') print_details('email',result.substr(5));
			 else if(result.substr(0,14)=='pre_view_email')	 print_details('pre_view_email',result.substr(13));
			 else if(result.substr(0,4)=='sent') print_details('sent',result.substr(4));
			 else if(result.substr(0,8)=='Not_sent') print_details('Not_sent',result.substr(8));
			 else if(result.substr(0,6)=='report') print_details('report',result.substr(6));
			 else if(result.substr(0,5)=='ccode'){
			 		if(result.substr(5)=='failed'){ 
					alert('<?php echo ERROR_COUPON_CODE_EXISTS; ?>');
					document.new_coupon.coupon_code.value = '';
					}
			 } 
			
			 else if(result.substr(0,4)=='load'){ 
			 			result = result.substr(4);
					document.getElementById('gen_id').innerHTML = result;
					if(document.getElementById('category')){
						if(document.getElementById('category').length>0)
						document.getElementById('category').selectedIndex =0;
						if(document.getElementById('cat_coupon_avail').length>0)
						document.getElementById('cat_coupon_avail').selectedIndex = 0;
					}
				    if(document.getElementById('items')){
						if(document.getElementById('items').length>0)
						document.getElementById('items').selectedIndex = 0;
						if(document.getElementById('items_coupon_avail').length>0)
						document.getElementById('items_coupon_avail').selectedIndex =0;
				}
			 }
 }
 function print_details(category,result){ 
 	 	var no_result=""; 	 	
 	 	var include="";
		
		if(result.substr(0,15)=='No Record Found'){
 	 		no_result="No Record Found";
 	 	}else if(result!='' && result.length>0 && category!='edit' && result.substr(0,15)!='No Record Found' && result!='preview'){ 
			var splt_result = result.split("^");
			var coupon_name="";
			var coupon_id="";
			var coupon_amount ="";
			var start_date="";
			var end_date="";
			var per_coupon="";
			var per_customer="";
			var date_created="";
			var date_modified="";
			var rprd_td,rprd="";
			var reve_td,reve="";
			var rsub_td,rsub="";
			var rser_td,rser="";
			var rcat_td,rcat="";
			var revecat_td,revecat="";
			var rsubcat_td,rsubcat="";
			var rsercat_td,rsercat="";
			var display2="";
			var display3="";
			var coupon_details="";
			var check="";
			var ccode="";
			var newcpn="";
			var chk=0,view_len="",display_view="";
			var check_span="";
			
			
			coupon_id = splt_result[0];
			coupon_amount = splt_result[1];
			start_date = splt_result[2];
			end_date = splt_result[3];
			per_coupon = splt_result[4];
			per_user = splt_result[5];
			rprd = splt_result[6];
			rcat = splt_result[7];
			date_created =splt_result[8];
			date_modified =splt_result[9];
			reve = splt_result[10];
			rsub = splt_result[12];
			rsercat = splt_result[12];
			revecat = splt_result[13];
			rsubcat = splt_result[14];
			rser = splt_result[15];
			coupon_name = splt_result[16];
			ccode = splt_result[17];
			
			 if(splt_result[18]=='newc'){ 
					 document.getElementById('preview').style.display='none';
					 var status = document.status.status.value;
					command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=tbl_write');?>&status="+status; 
					do_get_command(command);
				 }
			var view_arr = new Array(rprd,reve,rsub,rser,rcat,revecat,rsubcat,rsercat);
			view_len = view_arr.length;
			rprd_td = '<td colspan="2" class="search"><?PHP echo COUPON_PRODUCTS . '&nbsp;&nbsp;::&nbsp;&nbsp;'; ?><A HREF="sales_coupon_listproducts.php?cid='+coupon_id+'" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listproducts.php?cid=' + coupon_id + '\', \'Valid_Categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A>; </td>';
			reve_td = '<td colspan="2" class="search"><?PHP echo COUPON_EVENTS . '&nbsp;&nbsp;::&nbsp;&nbsp;'; ?><A HREF="sales_coupon_listevents.php?cid=' + coupon_id + '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listevents.php?cid=' + coupon_id + '\', \'Valid_Events\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A></td>';
			rsub_td = '<td colspan="2" class="search"><?PHP echo COUPON_SUBSCRIPTION . '&nbsp;&nbsp;::&nbsp;&nbsp;'; ?><A HREF="sales_coupon_listsubscription.php?cid=' +coupon_id+ '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listsubscription.php?cid=' +coupon_id + '\', \'Valid_subscription\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A></td>';
			rser_td = '<td colspan="2" class="search"><?PHP echo COUPON_SERVICE . '&nbsp;&nbsp;::&nbsp;&nbsp;'; ?><A HREF="sales_coupon_listservice.php?cid=' +coupon_id +'" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listservice.php?cid=' +coupon_id + '\', \'Valid_service\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A></td>';
			rcat_td = '<td colspan="2" class="search"><?PHP echo COUPON_CATEGORIES . '&nbsp;&nbsp;::&nbsp&nbsp;'; ?><A HREF="sales_coupon_listcategories.php?cid=' +coupon_id + '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listcategories.php?cid=' +coupon_id + '\', \'Valid_Categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A> </td>';
			revecat_td = '<td colspan="2" class="search"><?PHP echo COUPON_EVENT_CATEGORIES . '&nbsp;&nbsp;::&nbsp;&nbsp;'; ?><A HREF="sales_coupon_listeventcategories.php?cid=' +coupon_id+ '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listeventcategories.php?cid=' +coupon_id + '\', \'Valid_event_Categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A></td>';
			rsubcat_td = '<td colspan="2" class="search"><?PHP echo COUPON_SUB_CATEGORIES . '&nbsp;&nbsp;::&nbsp;&nbsp;'; ?><A HREF="sales_coupon_listsubcategories.php?cid=' +coupon_id + '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listsubcategories.php?cid=' +coupon_id + '\', \'Valid_sub_categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A></td>';
			rsercat_td =  '<td colspan="2" class="search"><?PHP echo COUPON_SERVICE_CATEGORIES . '&nbsp;&nbsp;::&nbsp;&nbsp;'; ?><A HREF="sales_coupon_listservicecategories.php?cid='+coupon_id+'" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listservicecategories.php?cid=' +coupon_id + '\', \'Valid_service_categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A></td>';
			var view_td = new Array(rprd_td,reve_td,rsub_td,rser_td,rcat_td,revecat_td,rsubcat_td,rsercat_td);
			
			for(i=0;i<view_len;i++){
				if(view_arr[i]!=''){ 
				 	if(chk==0) display_view += '<tr>';
					display_view +=  view_td[i];
					chk = chk + 1;
					if(chk ==2){
						chk=0;
						display_view +=  '</tr>';
					}
					check='true';
				}	
			}
			if(check!='true') coupon_details = 'All Categories ';
		}
		if(document.new_coupon && document.new_coupon.coupon_id) var id=document.new_coupon.coupon_id.value; 
 		var span_details=document.getElementById("span_details");	
 		var span_edit=document.getElementById("span_edit"); 	 		 	 		
 		var span_delete=document.getElementById("span_delete");	
		var span_email=document.getElementById("span_email");			
		var span_report=document.getElementById("span_report");		
		var span_preview=document.getElementById("span_preview");
		var head=document.getElementById("head_"+id); 	
 		var data_id=document.getElementById("data_"+id);
 		var res_display=document.getElementById("res_display");
 		var img_save=document.getElementById("img_save");
		if(img_save && category!='get_details') img_save.style.display=""; 
 	 	switch(category){ 	 		 	  		
 	 		case 'get_details': 
				//alert(head);
				if(data_id)
				{
					data_id.innerHTML='<table width="100%" cellspacing="0" cellpadding="4" border="0"><tbody><tr><td width="32%" class="dataTableContent">'+coupon_name+'</td><td width="38%" align="center" class="dataTableContent">'+coupon_amount+'</td><td align="center" class="dataTableContent">'+ccode+'</td></tr></tbody></table>';
				}	
				if(document.getElementById('newc')) document.getElementById('newc').innerHTML='';
			 if(head  && no_result!="")
				head.innerHTML=no_result; 	 			 	 		
 	 		else if(head && no_result==""){ 
	 	 		display1 ="<table width='100%' border='0' cellspacing='0' cellpadding='0' style='padding-top:4px;'>"+
				"<tr heigth='40'>"+
				  "<td width='10'></td>"+				  							  
				  '<td align="left" valign="middle" width="350" class="search">coupon:'+coupon_name+'<?php echo tep_draw_separator('pixel_trans.gif',180,1) ?>code:'+ccode+'</td>'+
				  "<td width='470' align='left' class='main' valign='top'>"+
				  	"<table border='0' cellspacing='0' cellpadding='0' style='width:100;height:30px;display:inline;position:relative;'>"+
				  	  "<tr class='img_move' id='img_ids'>"+
				  	  	"<td class='cell_bg_popm_left' width='10'>&nbsp;</td>"+
				  	  	
				  	    "<td width='10%' onclick='javascript:change_class(this,1);expand_category("+coupon_id+",4);' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(this,3);' ><img alt='Edit' title='Edit' src='images/template/img_edit.gif'></td>"+
				  	    "<td width='10%' onclick='javascript:change_class(this,1);expand_category("+coupon_id+",5);' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(this,3);' ><img alt='Delete' title='Delete' src='images/template/img_trash.gif'></td>"+				  	    
				  	    "<td width='10%' onclick='javascript:change_class(this,1);expand_category("+coupon_id+",8);' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(this,3);' ><img alt='Email' title='Email' src='images/template/img_sms.gif'></td>"+
						"<td width='10%' onclick='javascript:change_class(this,1);expand_category("+coupon_id+",10);' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(this,3);' ><img alt='Report' title='Report' src='images/template/img_copy.gif'></td>"+
						"<td class='cell_bg_popm_right' width='10'>&nbsp;</td>"+
				  	  "</tr>"+
				  	"</table>"+
				  "</td>"+
				 "<td width='240' align='right' id='img_save' style='display:'><input type='image' src='images/template/img_savel.gif' alt='save' title='save' id='img_sav' border='0'>&nbsp;&nbsp;<a href='javascript:expand_category("+coupon_id+",3);'><img src='images/template/img_closel.gif' alt='close' id='img_cls' title='close' border='0'></a>&nbsp;&nbsp;</td>"+
				"</tr>"+
				"<tr style='height:10'><td id='loading' style='display:none' colspan='5'><img src='images/24-1.gif' alt='Loading...' title='Loading...'></td></tr>"+				
				"<tr>"+
				  "<td colspan='8' class='main'><span style='display:none' id='span_preview'></span><span style='display:none' id='span_report'></span><span style='display:none' id='span_email'></span><span style='display:none' id='span_edit'></span><span style='display:none' id='span_delete'></span><span id='span_details'>"+	"<table width='100%' border='0' cellspacing='0' cellpadding='0'>"+
				  		"<tr>"+
				  			"<td width='100'>&nbsp;</td>"+				  							  			
							"<td width='700'>"+				  							  			
				  				"<table width='100%' border='0' cellspacing='5' cellpadding='0' class='main'>"+
				  					"<tr>"+
				  						"<td align='left' width='100' valign='top' class='search'><?php echo COUPON_NAME;?></td>"+
				  						"<td align='left' width='100' valign='top' class='search'>"+coupon_name+"</td>"+
										"<td align='left' width='100' class='search'><?php echo COUPON_AMOUNT ;?></td>"+
										"<td align='left' width='250' class='search'>"+coupon_amount+"</td>"+
				  					"</tr>"+			  					
				  												
									"<tr>"+
										"<td align='left' width='100' class='search'><?php echo COUPON_STARTDATE ;?></td>"+
										"<td align='left' width='100' class='search'>"+start_date+"</td>"+
										"<td align='left' width='100' class='search'><?php echo COUPON_FINISHDATE ;?></td>"+
										"<td align='left' width='250' class='search'>"+end_date+"</td>"+
									"</tr>"+
									
									"<tr>"+
										"<td align='left' width='100' class='search'><?php echo COUPON_USES_COUPON ;?></td>"+
										"<td align='left' width='100' class='search'>"+per_coupon+"</td>"+
										"<td align='left' width='100' class='search'><?php echo COUPON_USES_USER;?></td>"+
										"<td align='left' width='250' class='search'>"+per_user+"</td>"+
									"</tr>"+
									
									"<tr>"+
										"<td align='left' width='100' class='search'><?php echo DATE_CREATED  ;?></td>"+
										"<td align='left' width='100' class='search'>"+date_created+"</td>"+
										"<td align='left' width='100' class='search'><?php echo DATE_MODIFIED ;?></td>"+
										"<td align='left' width='250' class='search'>"+date_modified+"</td>"+
									"</tr>"+
									
									"<tr>"+
										'<td colspan="4" class="search"><?php echo 'Apply Coupon' . tep_draw_separator('pixel_trans.gif',100,1); ?>'+coupon_details+'</td>'+
									"</tr>";
						display2 +=								
									"<tr>"+
										'<td colspan="4"><?php echo tep_draw_separator('pixel_trans.gif',50,10); ?></td>'+
									"</tr>"+
									
				  			  "</table></td></tr>"+
					"</span>"+
				  "</td>"+
				"</tr>"+
			"</table>";
			head.innerHTML = display1+display_view+display2;
			document.getElementById('preview').style.display='none';
			document.getElementById('ajax_details').style.display='';
			document.getElementById('img_cls').style.display='';
			document.getElementById('img_sav').style.display='none';
			document.getElementById('newc').style.display='none';
			data_id.style.display='';
			check_span=1;
 	 		}		 	 		
 	 	break;
 	 	case 'edit': 	 		 	 		 	 		 	 		
			if(span_edit) span_edit.style.display="";			
 	 		if(span_edit && span_details){ 	 			 	 			
 	 			span_edit.innerHTML=result; 	
 	 			span_preview.style.display=span_email.style.display=span_report.style.display=span_delete.style.display=span_details.style.display='none';
				document.getElementById('img_sav').style.display='none';
 	 		}
			updateGross();
			if(document.new_coupon.coupon_selected) apply_selected();
 	 	break;
		case 'pre_view_email':
		case 'email': 
				if(span_email){ 
				 	span_email.style.display="";
					span_email.innerHTML = result;
					span_preview.style.display=span_details.style.display=span_edit.style.display=span_delete.style.display='none';
					document.getElementById('img_sav').style.display='none';
					//define('customers_email_address', 'string', 'Customer or Newsletter Group');
					<?php if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Enable') { ?>		 	
							if(document.getElementById("message")&& category=='email' ){ 		 
							initEditor('message');}
					<?php } ?>    
			       }
		break;
 	 	case 'delete': 	 				 	 	
 	 		if(span_delete) span_delete.style.display="";
			if(document.getElementById('img_sav')) document.getElementById('img_sav').style.display='';
 	 		if(span_delete){ 	 			
 	 			span_delete.innerHTML=result; 	 			
 	 			span_preview.style.display=span_email.style.display=span_report.style.display=span_details.style.display=span_edit.style.display='none';
 	 		} 	 								   			   
 	 	break; 	 	
 	 	case 'success_delete':			
 	    	 if(head && data_id) {
			 	var tbl_obj=head.parentNode.parentNode.parentNode.parentNode;			 				 				 	
			 	if(tbl_obj) tbl_obj.deleteRow(head.parentNode.parentNode.rowIndex);
			 	data_id.innerHTML=head.innerHTML="";
			 	data_id.style.display=head.style.display="none";			 				 			
			 }
 	    	 if(res_display){ 
	    	 	res_display.innerHTML="<?php echo TEXT_DELETE_SUCCESS;?>";
 	    	 	res_display.style.display=""; 	    	 	
 	    	 } 	    	 			 			 	
			 var obj=document.getElementById("page_''");
 	    	 	if(obj){  	    	 		
 	    	 		var len=obj.options[obj.selectedIndex].value;
					var status = document.status.status.value;
 	    	 		command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=page_navigation');?>&page="+len+"&status="+status;
					do_get_command(command);
 	    	 	} 
 	 	break;	
 	 	case 'page_navigation':
 	 		document.getElementById("ajax_details").innerHTML ="";
			if(document.getElementById("ajax_details"))
 	 		 document.getElementById("ajax_details").innerHTML=result;
			//if(document.getElementById("pag_nav")) document.getElementById("pag_nav").style.display='none';
 	 	break;	
		
		case 'st_change': 
				if(document.getElementById("fst_nav"))  document.getElementById("fst_nav").style.display='none';
				if(document.getElementById("ajax_details"))
 	 				 document.getElementById("ajax_details").innerHTML=result;
		break;
		case 'preview':	
					if(document.getElementById('preview')){
					 	document.getElementById('preview').innerHTML = result;
						document.getElementById('preview').style.display='';
						document.getElementById('newc').style.display='none';
						document.getElementById('ajax_details').style.display='none';
						if(check_span==1){
							span_delete.style.display=span_email.style.display=span_report.style.display=span_details.style.display=span_edit.style.display='none';
						}
					}
		break;
		case 'newc':
					document.getElementById('newc').innerHTML = result;
					document.getElementById('newc').className='openContent';
					document.getElementById('newc').style.display='';
					if(document.getElementById("preview")) document.getElementById("preview").style.display='none';
					if(span_edit) span_edit.style.display="none";
					if(head){ 
							if(head.style.display=='') head.style.display="none"; 	 			   	    
 	 			   	  		data_id.style.display=""; 
						}	
						
				//	document.getElementById('ajax_details').style.display='none';
		break;
		case 'insert_newc': 
					
					 document.getElementById('ajax_details').innerHTML="";
					 document.getElementById('ajax_details').innerHTML=result;
					 document.getElementById('ajax_details').style.display='';
					 document.getElementById('preview').style.display='none';
					 document.getElementById('img_new').firstChild.src=img_src+ 'template/img_new.gif' ;
					 discplay_flag=0;
					 //document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('_hover.gif','.gif');
		break;
		case 'sent':
				 if(res_display){ 
				 	res_display.innerHTML="";
	    	 		res_display.innerHTML="<?php echo TEXT_EMAIL_SENT;?>";
 	    	 		res_display.style.display=""; 	    	 	
 	    	 	}
				expand_category(result,3);
		break;
		case 'Not_sent':
				 if(res_display){ 
				 	res_display.innerHTML="";
	    	 		res_display.innerHTML="<?php echo TEXT_EMAIL_NOT_SENT;?>";
 	    	 		res_display.style.display=""; 	    	 	
 	    	 	} 	
				expand_category(result,3);
		break;
		case 'report':
				var report=""
				var row="";
				var report_head="";
				if(document.getElementById("img_sav")) document.getElementById("img_sav").style.display='none'; 
				report_head = "<table width='100%' border='0' cellspacing='0' cellpadding='0' style='padding-top:4px;'>"+
						 "<tr height='20'>"+
						 "<td class='search'><?php echo  CUSTOMER_ID;?></td>"+
						 "<td class='search'><?php echo CUSTOMER_NAME;?></td>"+
						 "<td class='search'><?php echo IP_ADDRESS;?></td>"+
						 "<td class='search'><?php echo REDEEM_DATE;?></td></tr>";
				if(result!=""){ 
						result = result.split(',');
						var splt_report=result[0].split('@');
						var splt_len=splt_report.length-1;
						if(splt_len > 0){
						 for(i=0;i<splt_len;i++){
							report = splt_report[i].split('#');
							row = row +	 "<tr height='15'>"+
							 		"<td class='search'>"+report[0] +"</td>"+
								 	"<td class='search'>"+report[1]+"</td>"+
									 "<td class='search'>"+report[2]+"</td>"+
						 			"<td class='search'>"+report[3]+"</td></tr>";
						  }
						 
						}else {
							row =   "<tr height='20'>"+
									"<td colspan='4' class='search' align='center'>No Report Found</td></tr>";
				
							}	
						row = row + "<tr height='20'>"+
									"<td colspan='4' class='search'>"+ result[1]+"</td></tr>";
				     }
						row = row +  "<tr height='20'>"+
								'<td colspan="4"><?php echo tep_draw_separator('pixel_trans.gif',1,10); ?></td></tr></table>';
				if(span_report){
				 	span_report.style.display="";
					span_report.innerHTML = report_head + row;
					span_preview.style.display=span_email.style.display=span_details.style.display=span_edit.style.display='none';
				}
		break;
 	 	}
		
		
 	 	if(res_display.style.display=="") setTimeout("res_display.style.display='none'",2000); 	 	 	 	
 	 	clearTimeout();
	
 }
 function change_class(obj,eve){ 		 		 	 	
		 var span_details=document.getElementById("span_details"); 
		 var span_edit=document.getElementById("span_edit");
		 var span_delete=document.getElementById("span_delete");	
		 var span_email=document.getElementById("span_email"); 
		 var span_report=document.getElementById("span_report"); 
		 if(obj){		   		    		    
		    var img_get_src=obj.childNodes[0];
			if(img_get_src){
			    if(img_get_src.src==img_src+'template/img_copy.gif') {
					if(span_report && span_report.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_copy_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_copy_hover.gif' && span_report && span_report.style.display=="none") img_get_src.src=img_src+'template/img_copy.gif';
			    if(img_get_src.src==img_src+'template/img_edit.gif') {
					if(span_edit && span_edit.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_edit_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_edit_hover.gif' && span_edit && span_edit.style.display=="none") img_get_src.src=img_src+'template/img_edit.gif';
			  
			   if(img_get_src.src==img_src+'template/img_sms.gif') {
					if(span_email && span_email.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_sms_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_sms_hover.gif' && span_email && span_email.style.display=="none") img_get_src.src=img_src+'template/img_sms.gif';
			  
			    if(img_get_src.src==img_src+'template/img_trash.gif') {
					if(span_delete && span_delete.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_trash_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_trash_hover.gif' && span_delete && span_delete.style.display=="none") img_get_src.src=img_src+'template/img_trash.gif';		    		    		     		   	
		  }
		 }		 
	  }
function apply_selected(){
	if(document.new_coupon.coupon_selected.checked==true){
		document.new_coupon.coupon_all.checked = false;
		document.getElementById('tabstrib').style.display='';
		goto_ajax('p','<?php echo $coupon_categories; ?>');
	}else{
	   document.new_coupon.coupon_all.disabled= false;
	   document.getElementById('tabstrib').style.display='none';
    }
}
function goto_ajax(type,prev_ids){ 
 
		 if (selectedTab) selectedTab.className="tab";
		 selectedTab=document.getElementById(type);
		 selectedTab.className="tabSelect";
		 var frm = document.new_coupon;
		 
		if(type=='p' && frm.coupon_categories.value !="")
		   prev_ids = frm.coupon_categories.value ;
		else if( type=='e'&& frm.coupon_event_categories.value != "")
		   prev_ids = frm.coupon_event_categories.value;
		else if(type=='s' && frm.coupon_sub_categories.value !="" )
		   prev_ids = frm.coupon_sub_categories.value;
		else if(type=='v' && frm.coupon_service_categories.value !="")  
			prev_ids = frm.coupon_service_categories.value;
		else if(type=='pi' && frm.coupon_products.value !="" )
			prev_ids = frm.coupon_products.value;
		else if(type=='ei' && frm.coupon_events.value !="")
			prev_ids = frm.coupon_events.value ;
		else if(type=='si' && frm.coupon_subscription.value !="")
			prev_ids =frm.coupon_subscription.value;
		else if(type=='vi' && frm.coupon_service.value !="")
			prev_ids = frm.coupon_service.value ;
		  //alert(prev_ids);
		  
		if(frm.coupon_selected.checked==true){
		if(type == 'p' || type == 'e' || type == 's' || type == 'v')
		command ="<?php echo tep_href_link(FILENAME_COUPON_ADMIN, 'command=load_categories','NONSSL',true,false); ?>&type="+type+"&prev_ids="+prev_ids; 
		else
		command ="<?php echo tep_href_link(FILENAME_COUPON_ADMIN, 'command=load_items','NONSSL',true,false); ?>&type="+type+"&prev_ids="+prev_ids; 
		}
		//alert(command);
		do_get_command(command);
}
//load the selected category list box
function load_selected(type){
	con = document.getElementById('cat_coupon_avail');
	category = document.getElementById('category');
	hid_value1 ="";
	dublicate = '';
	count = '';
	
	if(con.selectedIndex == -1 ){ return;
	}else{
		con_len = con.length;
		for(i=0;i<con.length;i++){
		   if(con.options[i].selected){
				var option = document.createElement('option');
					option.text = con.options[i].text;
					option.value = con.options[i].value;
			 		 sel = con.selectedIndex;
			  		
				for(j=0;j<category.length;j++){
				if(category[j].value ==option.value)
			   		dublicate = 'exists';
				}	
				if( dublicate != 'exists'){ count = count + 1;
		  			 try {
			   			category.add(option, null); // standards compliant; doesn't work in IE
			 			}
	  			 	catch(ex) {
	 		  			category.add(option); // IE only
	 				 }
			
				 }else return;}}
	  } 
	  for(jcnt=0;jcnt<con.length;jcnt++){ 
	  if(con.options[jcnt].selected)
	  	con.remove(con.selectedIndex);
	  }
	  if(!con[sel]) sel=0;
	 	con.selectedIndex = sel;
	 	category_len = category.length;
	  for(icnt=0;icnt<category_len;icnt++)
	  	  hid_value1= hid_value1 + ','+ document.getElementById('category')[icnt].value;
		  
	  hid_value1 = hid_value1.substring(1);
	  
	  //Loading hidden values to appropriate selected categories
	  if(type=='p')
	  		document.new_coupon.coupon_categories.value = hid_value1;
	  else if(type=='e')
	  		document.new_coupon.coupon_event_categories.value = hid_value1;
	  else if(type=='s')
	  		document.new_coupon.coupon_sub_categories.value = hid_value1;
	  else if(type=='v')
	  		document.new_coupon.coupon_service_categories.value = hid_value1;
		
		if(category_len>0) category.selectedIndex = 0;
	}
function remove_selected(type){
	category = document.getElementById('category');
	con = document.getElementById('cat_coupon_avail');
	hid_value1 ="";
	count_r = '';
	if(category.selectedIndex== -1) return; 
	else{
	 	for(i=0;i<category.length;i++){
			if(category.options[i].selected){
				var option = document.createElement('option');
	 			option.text = category.options[i].text;
				option.value= category.options[i].value;
				sel = category.selectedIndex;
				count_r = count_r + 1;
		
		try {
		  con.add(option, null); 
		 	}
	  	catch(ex) {
	 	  	con.add(option); 
	 	 	}
		  }
		 }	
		}
		for(jcnt=0;jcnt<category.length;jcnt++){ 
		if(category.options[jcnt].selected)
		   category.remove(category.selectedIndex);
		 }
		if(!category[sel]) sel = 0;
		category.selectedIndex = sel;
		
		category_len = category.length;
		for(icnt=0;icnt<category_len;icnt++)
	  	  hid_value1= hid_value1 + ','+ document.getElementById('category')[icnt].value;
		  
	hid_value1 = hid_value1.substring(1);
	if(type=='p')
	  		document.new_coupon.coupon_categories.value = hid_value1;
	  else if(type=='e')
	  		document.new_coupon.coupon_event_categories.value = hid_value1;
	  else if(type=='s')
	  		document.new_coupon.coupon_sub_categories.value = hid_value1;
	  else if(type=='v')
	  		document.new_coupon.coupon_service_categories.value = hid_value1;
			
		if(con.length>0) con.selectedIndex = 0;
}

function remove_all(type){
	category = document.getElementById('category');
	con = document.getElementById('cat_coupon_avail');
	cat_len = category.length; 
	 	for(i=0;i<cat_len;i++){
		var option = document.createElement('option');
	 	option.text = document.getElementById('category')[i].text;
	 	option.value = document.getElementById('category')[i].value;
		try {
		  con.add(option, null); 
		 	}
	  	catch(ex) {
	 	  	con.add(option); 
	 	 	}
		}
	 //  }
	 document.getElementById('cat_coupon_avail').selectedIndex = 0;
	 while(category.options.length>0){
			category.remove(category.options.length - 1);
	 }
	 	document.new_coupon.coupon_categories.value = '';
	 	document.new_coupon.coupon_event_categories.value = '';
		document.new_coupon.coupon_sub_categories.value = '';
		document.new_coupon.coupon_service_categories.value = ''; 
	
}

function remove_all_items(type){
	items = document.getElementById('items');
	con_item = document.getElementById('items_coupon_avail');
	items_len = items.length;
	
	
	 	for(i=0;i<items_len;i++){
		var option = document.createElement('option');
	 	option.text = document.getElementById('items')[i].text;
	 	option.value = document.getElementById('items')[i].value;
		try {
		  con_item.add(option, null); 
		 	}
	  	catch(ex) {
	 	  	con_item.add(option); 
	 	 	}
		}
	 while(items.options.length>0){
			items.remove(items.options.length - 1);
	 }
	 document.getElementById('items_coupon_avail').selectedIndex = 0;
	 	document.new_coupon.coupon_products.value = '';
	 	document.new_coupon.coupon_events.value = '';
		document.new_coupon.coupon_subscription.value = '';
		document.new_coupon.coupon_service.value = ''; 
	
}

function remove_selected_items(type){
	items = document.getElementById('items');
	con_item = document.getElementById('items_coupon_avail');
	hid_item_sel = '';
	
	if(items.selectedIndex == -1) return; //alert('<?PHP echo ERROR_SELECT_ITEMS; ?>');
	else{
	   for(i=0;i<items.length;i++){
	   	   if(items.options[i].selected){
	   		var option = document.createElement('option');
			option.text = items.options[i].text;
			option.value = items.options[i].value;
			sel = items.selectedIndex;
		try {
		  con_item.add(option, null); 
		 	}
	  	catch(ex) {
	 	  	con_item.add(option); 
	 	 	}
		  }	
		}
	}
	 for(jcnt=0;jcnt<items.length;jcnt++){
	 if(items.options[jcnt].selected)
	  	items.remove(items.selectedIndex);
	 }
	 if(!items[sel]) sel =0;
		  items.selectedIndex = sel;
			  
	  items_len = items.length;
	  for(icnt=0;icnt<items_len;icnt++)
	  	  hid_item_sel= hid_item_sel + ','+ document.getElementById('items')[icnt].value;
		  
	  hid_item_sel = hid_item_sel.substring(1);
	  if(type=='pi')
	  		document.new_coupon.coupon_products.value = hid_item_sel;
	  else if(type=='ei')
	  		document.new_coupon.coupon_events.value = hid_item_sel;
	  else if(type=='si')
	  		document.new_coupon.coupon_subscription.value = hid_item_sel;
	  else if(type=='vi')
	  		document.new_coupon.coupon_service.value = hid_item_sel;
			
		if(con_item.length>0) con_item.selectedIndex = 0;
}

function load_all(type){
	category = document.getElementById('category');
	con = document.getElementById('cat_coupon_avail');
	len = document.getElementById('cat_coupon_avail').length;
	hid_value="";
	
	/*if(con.options.length == 0) alert('<?php echo ERROR_AVILABLE_LIST_EMPTY; ?>');
	else{
	while(category.options.length>0){
			category.remove(category.options.length - 1);
			
		}*/
	var option = document.createElement('option');
	for(icnt=0;icnt<len;icnt++){
		//alert(document.getElementById('cat_coupon_avail')[icnt].value);
		var option = document.createElement('option');
		option.text = document.getElementById('cat_coupon_avail')[icnt].text;
		option.value = document.getElementById('cat_coupon_avail')[icnt].value;
		try {
		   category.add(option, null); // standards compliant; doesn't work in IE
		}
	  	catch(ex) {
	 	   category.add(option); // IE only
	 	}
		hid_value =   hid_value + ',' + document.getElementById('cat_coupon_avail')[icnt].value ;
	 }
	 //}
	 document.getElementById('category').selectedIndex = 0;
	 while(con.options.length>0){
			con.remove(con.options.length - 1);
			//if(category.options.length==1) alert('Already moved'); 
		}
	 
	 hid_value = hid_value.substring(1);
	 // loading hidden value to appropriate categories
	 if(type=='p')
	 	document.new_coupon.coupon_categories.value = hid_value;
	 else if(type=='e') 
	 	document.new_coupon.coupon_event_categories.value = hid_value;
	 else if(type=='s')
		document.new_coupon.coupon_sub_categories.value = hid_value;
	 else if(type=='v')
		document.new_coupon.coupon_service_categories.value = hid_value;
	}
	
function load_selected_items(type){
	con_item = document.getElementById('items_coupon_avail');
	items = document.getElementById('items');
	hid_item_sel ="";
	dublicate = '';
	count ='';
	
	if(con_item.selectedIndex == -1 ){ return;
	}else{
		for(i=0;i<con_item.length;i++){
		 if(con_item.options[i].selected){
				var option = document.createElement('option');
				option.text = con_item.options[i].text;
				option.value = con_item.options[i].value;
			  	sel = con_item.selectedIndex;
			  
			for(j=0;j<items.length;j++){
				if(document.getElementById('items')[j].value == option.value)
			     	dublicate = 'exists';
			}
			if( dublicate != 'exists'){
				
				count = count +1;
				try {
					items.add(option, null); // standards compliant; doesn't work in IE
				}
	  			catch(ex) {
	 				items.add(option); // IE only
	 			}
			 	
		 	}else return;
		  }
		}
	  } 
	  for(jcnt=0;jcnt<con_item.length;jcnt++){
	  if(con_item.options[jcnt].selected)
	  	con_item.remove(con_item.selectedIndex)
	  }
	  if(!con_item[sel]) sel = 0;
	  con_item.selectedIndex = sel;
	  items_len = items.length;
	  for(icnt=0;icnt<items_len;icnt++)
	  	  hid_item_sel= hid_item_sel + ','+ document.getElementById('items')[icnt].value;
		  
	  hid_item_sel = hid_item_sel.substring(1);
	  //Loading hidden values to appropriate selected products/items
	  if(type=='pi')
	  		document.new_coupon.coupon_products.value = hid_item_sel;
	  else if(type=='ei')
	  		document.new_coupon.coupon_events.value = hid_item_sel;
	  else if(type=='si')
	  		document.new_coupon.coupon_subscription.value = hid_item_sel;
	  else if(type=='vi')
	  		document.new_coupon.coupon_service.value = hid_item_sel;
		
		if(items.length>0) items.selectedIndex = 0;
	}
 	
	
	function load_all_items(type){ 
	items = document.getElementById('items');
	con_item = document.getElementById('items_coupon_avail');
	len_item = document.getElementById('items_coupon_avail').length;
	hid_items_all="";
	
	/*if(con_item.options.length == 0) alert('<?php echo ERROR_AVILABLE_LIST_EMPTY; ?>');
	else{
	while(items.options.length > 0){
		items.remove(items.options.selectedIndex -1);        
	}*/
	var option = document.createElement('option');
	for(icnt=0;icnt<len_item;icnt++){
		//alert(document.getElementById('cat_coupon_avail')[icnt].value);
		var option = document.createElement('option');
		option.text = document.getElementById('items_coupon_avail')[icnt].text;
		option.value = document.getElementById('items_coupon_avail')[icnt].value;
		try {
		   items.add(option, null); // standards compliant; doesn't work in IE
		}
	  	catch(ex) {
	 	   items.add(option); // IE only
	 	}
		hid_items_all =   hid_items_all + ',' + document.getElementById('items_coupon_avail')[icnt].value ;
	 }
	 //}
	 document.getElementById('items').selectedIndex = 0;
	 while(con_item.options.length>0){
			con_item.remove(con_item.options.length - 1);
		}
	 
	 hid_items_all = hid_items_all.substring(1);
	 // loading hidden value to appropriate categories
	 if(type=='pi')
	 	document.new_coupon.coupon_products.value = hid_items_all;
	 else if(type=='ei') 
	 	document.new_coupon.coupon_events.value = hid_items_all;
	 else if(type=='si')
		document.new_coupon.coupon_subscription.value = hid_items_all;
	 else if(type=='vi')
		document.new_coupon.coupon_service.value = hid_items_all;
	}
	
function apply_all(){
	var frm = document.new_coupon;
	if(frm.coupon_all.checked==true){
		document.getElementById('tabstrib').style.display='none';
		frm.coupon_selected.checked = false;
		frm.coupon_categories.value ='';
		frm.coupon_event_categories.value = '';
		frm.coupon_sub_categories.value = '';
		frm.coupon_service_categories.value = '';
		frm.coupon_products.value = '';
		frm.coupon_events.value = '';
		frm.coupon_subscription.value = '';
		frm.coupon_service.value = '';
		frm.coupon_alll.value = 1;
	}else{
	    document.getElementById('tabstrib').style.display='';
	}

}
function check_edit_form(){ 
	var netValue = document.new_coupon.coupon_amount.value;
	var grossValue = document.new_coupon.coupon_amount_gross.value;
	var error_message="";
	
	if(netValue.lastIndexOf("%")!=-1)
		netValue = netValue.substring(0,netValue.length-1);

	if(grossValue.lastIndexOf("%")!=-1)
		grossValue = grossValue.substring(0,grossValue.length-1);
			
	if(isNaN(netValue) || isNaN(grossValue)){
	//	alert(" <?php echo TEXT_ERROR_PRICE;?>");
		error_message = " <?php echo TEXT_ERROR_PRICE;?>";
		//return false;
	}
	if(document.new_coupon.coupon_selected.checked==true){
	frm = document.new_coupon;
		if(frm.coupon_categories.value<1 && frm.coupon_event_categories.value<1 && frm.coupon_sub_categories.value<1 && frm.coupon_service_categories.value <1 && frm.coupon_products.value<1 && frm.coupon_events.value<1 && frm.coupon_subscription.value<1 && frm.coupon_service.value<1){
			//alert('<?php echo ERROR_NO_SELECT; ?>');
			error_message = error_message + '<?php echo ERROR_NO_SELECT; ?>'
			//return false;
			}
	}
	if(document.new_coupon.coupon_code.value==""){
		error_message = "<?php echo TEXT_ERROR_COUPON  ?>";
	}
	if(error_message!=''){
		alert(error_message);
	     return false;
	}else return true;
	
	//return false;
}
function validateForm(){

	  var coupon_id = document.new_coupon.coupon_id.value;
	  var id = coupon_id;
	  var newc = document.getElementById("newc");
	  var frm = document.new_coupon;
	  var error='';
	  var span_delete=document.getElementById("span_delete");
	  
   	  if(newc.innerHTML=='' && span_delete.style.display!=''){
	 		
			for (icnt=0;icnt<lang.length;icnt++){ 
			if (frm.elements["coupon_name["+lang[icnt]+"]"].value=="" || str_trim(frm.elements["coupon_name["+lang[icnt]+"]"].value)==""){
				  error = 'Give Valid Coupon name';
			}
			} 
			if(str_trim(frm.coupon_code.value)=='' || frm.coupon_code.value==''){
			error +='\n'+'Give Valid Coupon code';
			}
			if(str_trim(frm.coupon_min_order.value)=='' || frm.coupon_min_order.value==''){
			error +='\n'+'Minimum Amount should be Valid';
			}
			if(str_trim(frm.coupon_amount.value)=='' || frm.coupon_amount.value==''){
			error += '\n'+'Coupon Amount is must';
			}
			if(error){
			alert(error);
			return false;	
			} 
	 }
	 
	  if(newc.innerHTML!=''){ 
		if(id=="") id=0;
		 for (icnt=0;icnt<lang.length;icnt++){ 
			 if (str_trim(frm.elements["coupon_name["+lang[icnt]+"]"].value)=="" || frm.elements["coupon_name["+lang[icnt]+"]"].value==''){
				  error = 'Give Valid Coupon name';
				}
			  } 
		if(str_trim(frm.coupon_code.value)=='' || frm.coupon_code.value==''){
			 error +='\n'+'Give Valid Coupon code';
		 }
		 if(str_trim(frm.coupon_min_order.value)=='' || frm.coupon_min_order.value==''){
			 error +='\n'+'Minimum Amount should be Valid';
		 }
		 if(str_trim(frm.coupon_amount.value)=='' || frm.coupon_amount.value==''){
			 error += '\n'+'Coupon Amount is must';
		 }
		if(error){
		alert(error);
		return false;	
		} 
		command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=preview');?>&id="+id;
		if(!error) do_post_command('new_coupon',command);

		
	}else if(newc.innerHTML==''){ 
			 var span_details=document.getElementById("span_details");
			 var span_edit=document.getElementById("span_edit");				
			 var span_delete=document.getElementById("span_delete");
		
			 if(coupon_id!='' && span_edit.style.display=='' && check_edit_form()){
				 command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=preview');?>&id="+id;
				 do_post_command('new_coupon',command);
			 }
	   
			 else if(coupon_id!='' && span_delete.style.display==''){
					var status = document.status.status.value;
					 command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=delete_confirm');?>&id="+id+"&status="+status;
					 do_get_command(command);
					 return false;
			 }

	}
	return false;
}

function preview(){ 
	frm = document.new_coupon;
	var free_ship = "";
	if(frm.coupon_free_ship.checked) free_ship='Free Shipping';
	else free_ship = 'No Free Shipping';
	var content="";
	var coupon_all ="";
	if(frm.coupon_all.checked) coupon_all = "<tr><td width='40%' class='main'>Apply Couopn</td><td width='40%' class='main'>To All Categories</td>"
	
	
	content = "<table width='80%' border='0' cellspacing='0' cellpadding='0' style='padding-top:4px;'>"+
			   "<tr><td width='40%' class='search'><?php echo TEXT_COUPON_TAX_CLASS; ?></td><td width='40%' class='main'>"+frm.coupon_tax_class_id.options[frm.coupon_tax_class_id.selectedIndex].text+"</td></tr>"+
			   "<tr><td width='40%' class='search'><?php echo TEXT_COUPON_PRICE_NET; ?></td><td width='40%' class='main'>"+frm.coupon_amount.value+"</td></tr>"+
			   "<tr><td width='40%' class='search'><?php echo TEXT_COUPON_PRICE_GROSS;?></td><td width='40%' class='main'>"+frm.coupon_amount_gross.value+"</td></tr>"+
			   "<tr><td width='40%' class='search'><?php echo COUPON_MIN_ORDER;?></td><td width='40%' class='main'>"+frm.coupon_min_order.value+"</td></tr>"+
			   "<tr><td width='40%' class='search'><?php echo COUPON_FREE_SHIP;?></td><td width='40%' class='main'>"+free_ship+"</td></tr>"+
			   "<tr><td width='40%' class='search'><?php echo COUPON_CODE;?></td><td width='40%' class='main'>"+frm.coupon_code.value+"</td></tr>"+
			   "<tr><td width='40%' class='search'><?php echo COUPON_USES_COUPON;?></td><td width='40%' class='main'>"+frm.coupon_uses_coupon+"</td></tr>"+
			   "<tr><td width='40%' class='search'><?php echo COUPON_USES_USER;?></td><td width='40%' class='main'>"+frm.coupon_uses_user.value+"</td></tr>"+
			   
			   "<tr><td width='40%' class='search'><?php echo COUPON_USES_USER;?></td><td width='40%' class='main'>"+frm.coupon_uses_user.value+"</td></tr>"+coupon_all+
			    "<tr><td width='40%' class='search'><?php echo COUPON_USES_USER;?></td><td width='40%' class='main'>"+frm.coupon_uses_user.value+"</td></tr>";
			   
	document.getElementById('preview').innerHTML = content;
	document.getElementById('preview').style.display='';
	document.getElementById('ajax_details').style.display='none';

}
function confirm_coupon(){
	var frm = document.new_coupon;
	var id="";
	var command="";
	var action="";
	if(!document.getElementById('newc').innerHTML){ 
		id = frm.coupon_id.value;
		if(id !=0) action="&oldaction=voucheredit";
	}	
	
	command="<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=confirm');?>&id="+id+action;
	do_post_command('new_coupon',command);
	return false;
}
function do_hover_change(element,mode){ 
		if (element.mode && mode==element.mode) return;
	//	alert('last:'+element.mode);
		var span_new = document.getElementById("newc");
		if(display_flag==1) {
		if(mode==2){ 
			element.firstChild.src=element.firstChild.src.replace(".gif","_hover.gif");
		}else if(mode==1 && element.mode==2 && element.mode!=3 && span_new.style.display=='none'){
			element.firstChild.src=element.firstChild.src.replace("_hover.gif",".gif");
			} 
	}		
		element.mode=mode;
	}
function compare(){ 
	if( parseInt(document.new_coupon.coupon_uses_user.value) > parseInt(document.new_coupon.coupon_uses_coupon.value) &&  (document.new_coupon.coupon_uses_coupon.value != 0) && (document.new_coupon.coupon_uses_coupon.value!= '')){
		alert('<?php echo ERROR_USES_PER_USER;?>');
		document.new_coupon.coupon_uses_user.value = '';
		document.new_coupon.coupon_uses_user.focus();
	}
	return false;
}
function check_ccode(){
	var frm = document.new_coupon;
	var code = frm.coupon_code.value;
	var command ="";
	command = "<?php echo tep_href_link(FILENAME_COUPON_ADMIN,'command=check_ccode');?>&ccode="+code;
	do_get_command(command);
}
function do_close(){
	if(document.getElementById('newc') && document.getElementById('newc').style.display=='') document.getElementById('newc').style.display='none';
	document.getElementById('img_new').firstChild.src=img_src+ 'template/img_new.gif' ;
	display_flag=0;
	//document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('_hover.gif','.gif');
	return false;
}

</script>