<?php
	
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this widget. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
}
if(!isset($_SESSION['root_folder'])){
	$query = "Select root_folder from storePrefs";
        $rt=mysql_query($query);     
        while($nt=mysql_fetch_array($rt)){
                //$_SESSION[timezone] = $nt[timezone];
                //date_default_timezone_set($_SESSION[timezone]);
                $_SESSION[root_folder] = $nt[root_folder];
        }
}
	
	
?>

<script type="text/javascript" src="js/jscolor/jscolor.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckfinder/ckfinder_v1.js"></script>

<?php

//code to save fields sequence in database

if (isset($_POST) && !empty($_POST)) {
	if (isset($_POST['updateArray']) && !empty($_POST['updateArray'])) {
		echo "<pre>";
		print_r($_POST['updateArray']);
		die;
		$i = 1;
		foreach ($_POST['updateArray'] as $index => $id) {

			$sql = "UPDATE widget_fields
					SET sequence = '".$index."'
					WHERE recordkey = '".mysql_real_escape_string($id)."'";
			mysql_query($sql) or die(mysql_error());
			$i++;
		}
		die('1');
	}
} 
//set variables, get request data
$action = $_GET["a"];
$editwidget = $_POST["widget"];
if(empty($editwidget)){
	$editwidget = $_GET["key"];
}
//in case form refreshed from widget iframe, may need to set current widget ID here
if(isset($_GET["widgetId"])){
	$editwidget = $_GET["widgetId"];
}

//$widgettype = $_POST["widgettype"];

//process any delete request
if (!empty($_POST["chkDelete"])) {
     $query = "delete from widgets WHERE recordKey = '" . $_GET['key'] ."'";
     mysql_query($query);
     echo "<font color=red>Widget deleted successfully.</font>";
     unset($action);
}
//process any new widget request	
if ($action == "add") {         
    $query = "Insert into widgets (title) values('" . mysql_real_escape_string($_POST["newwidget"]) . "')";
    mysql_query($query);	
    echo "<font color=red>Widget added successfully.</font>";
	$editwidget=mysql_insert_id();
}



//******************make any updates
if ($action == "update") {

		$query = "UPDATE widgets SET title = '" . mysql_real_escape_string($_POST['title']) . "', color = '". strtoupper($_POST['color']) . "'";

	//echo $query;
    if(isset($_POST['widgetbody']) && !empty($_POST['widgetbody'])){
    	$body_content = mysql_real_escape_string($_POST['widgetbody']);
    	$query .= ", body ='".$body_content."'";
    }
	//widget_type
    if(isset($_POST['widget_type']) && !empty($_POST['widget_type'])){
    	$query .= ", widget_type ='".$_POST['widget_type']."'";
    }else{
    	$query .= ", widget_type ='4'";
    }

    if(isset($_FILES['image_name']) && !empty($_FILES['image_name']["name"])){
	    //Upload File
	    $uploadok=0;
	    $check = getimagesize($_FILES["image_name"]["tmp_name"]);
    	if($check !== false) {
    		$target_dir = $_SERVER['DOCUMENT_ROOT']."/custom/images/";
    		$filename = strtotime("now").'_'.basename($_FILES["image_name"]["name"]);
			$target_file = $target_dir.$filename;
			$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
			if($imageFileType == "jpg" || $imageFileType ==  "png" || $imageFileType == "jpeg" || $imageFileType == "gif" ){
				$uploadedfile = $_FILES['image_name']['tmp_name'];
				if($imageFileType=="jpg" || $imageFileType=="jpeg" )
				{
					$src = imagecreatefromjpeg($uploadedfile);
				}else if($imageFileType=="png"){
					$src = imagecreatefrompng($uploadedfile);
				}else{
					$src = imagecreatefromgif($uploadedfile);
				}
 
				list($width,$height)=getimagesize($uploadedfile);

				$newwidth=350;
				$newheight=($height/$width)*$newwidth;
				$tmp=imagecreatetruecolor($newwidth,$newheight);

				imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

				imagejpeg($tmp,$target_file,100);

				imagedestroy($tmp);

			// if (move_uploaded_file($_FILES["image_name"]["tmp_name"], $target_file)) {
			//    } else {
			//        $uploadok=1;
			//        echo "<font color=red>Image not moved to server!!.</font><br/>";
			//    }
			}else{
				$uploadok=1;
				echo "<font color=red>uploaded file is not an image.</font><br/>";
			}		
    	}else{
				$uploadok=1;
				echo "<font color=red>Image size is zero.</font><br/>";
		}
    	if($uploadok==0){				
	    	$query .= ", image_name ='".$filename."'";
	    }else{
	    	echo "<font color=red>Image is not Uploaded.</font><br/>";
	    }
	}

    //link_url
    if(isset($_POST['link_url']) && !empty($_POST['link_url'])){
    	$query .= ", link_url ='".$_POST['link_url']."'";
    }

    /*//yelp_id
    if(isset($_POST['yelp_id']) && !empty($_POST['yelp_id'])){
    	$query .= ", yelp_id ='".$_POST['yelp_id']."'";
    }*/

/* yelp_field start here*/

    //yelp_consumer_key
    if(isset($_POST['yelp_consumer_key']) ){
    	$query .= ", yelp_consumer_key ='".$_POST['yelp_consumer_key']."'";
    }

    //yelp_consumer_secret
    if(isset($_POST['yelp_consumer_secret']) ){
    	$query .= ", yelp_consumer_secret ='".$_POST['yelp_consumer_secret']."'";
    }

    //yelp_token
    if(isset($_POST['yelp_token']) ){
    	$query .= ", yelp_token ='".$_POST['yelp_token']."'";
    }

    //yelp_token_secret
    if(isset($_POST['yelp_token_secret']) ){
    	$query .= ", yelp_token_secret ='".$_POST['yelp_token_secret']."'";
    }

    // yelp_business_id
    if(isset($_POST['yelp_business_id']) && !empty($_POST['yelp_business_id'])){
    	$query .= ", yelp_business_id ='".$_POST['yelp_business_id']."'";
    }

    // Yelp show review
    $yelpShowReview = isset($_POST['yelp_show_review']) && $_POST['yelp_show_review'] == 1 ? 1 : 0;
    $query .= ", yelp_show_review = '".$yelpShowReview."'";

/* yelp_field ends here*/

    //opentable_id
    if(isset($_POST['opentable_id']) && !empty($_POST['opentable_id'])){
    	$query .= ", opentable_id ='".$_POST['opentable_id']."'";
    }

    //youtube_url
    if(isset($_POST['youtube_url']) && !empty($_POST['youtube_url'])){
    	$query .= ", youtube_url ='".$_POST['youtube_url']."'";
    }

    if(!empty($_POST['chkActive'])){
      $query .= ", active ='1'";
    }else{
        $query .= ", active ='0'";
    }
	if(!empty($_POST['chknoborder'])){
      	$query .= ", noborder ='1'";
    }else{
    	$query .= ", noborder ='0'";
    }
	if($_POST['widget_padding']=='1'){
      	$query .= ", no_padding ='1'";
    }else{
        $query .= ", no_padding ='0'";
    }
	if(!empty($_POST['chknocolor'])){
      	$query .= ", nocolor ='1'";
    }else{
        $query .= ", nocolor ='0'";
    }
	if(!empty($_POST['formnumber'])){
		$query .= ", formNumber ='".$_POST['formnumber'] ."'";
	}else{
		$query .= ", formNumber ='0'";
	}
		$query = $query . " WHERE recordkey = '" . $_GET['key'] ."'" ;
		//echo $query;
		mysql_query($query) or die(mysql_error());   //execute query

		//confirmation message that update worked...
		echo "<font color=red>widget content updated successfully.</font>";

	}	 //*******************end of update query

		

	echo '<div align="left"><form action="' . $_SERVER['PHP_SELF'] . '?a=add&admin=widgets" method="post">
	Add new widget: <br><input type="text" name="newwidget" size=38>
	<input type="submit" name="save" id="save-button" value="Add widget">
	</form>';
	
	
	 //create selector to choose page to edit
	 echo '<form action="' . $_SERVER['PHP_SELF'] . '?a=select&admin=widgets" method="post">';
	 echo'Or choose widget to edit: <br><select name="widget">';
	 $pages="Select recordkey, title from widgets";
	 $pg=mysql_query($pages);
	 while($pnt=mysql_fetch_array($pg)){
	   echo '<option value='. $pnt[recordkey];
	   if($pnt[recordkey]==$editwidget){echo ' selected="selected"';}
	   echo'>Widget#'.$pnt[recordkey];
	   if (!empty($pnt[title])){echo " - " .$pnt[title];} else { echo " - Untitled";} 
	   echo '</option>';
	 }

mysql_close;
echo '</select> <input id="save-button" type="submit" name="select" value="Edit"></form></div><br>';
//end of selector

if(!empty($editwidget)){	//determine which record to display
	$query = "SELECT * from widgets where recordkey=$editwidget";
	$rt=mysql_query($query);          // query executed 
	//echo mysql_error();                    // if any error is there that will be printed to the screen
} else{
	$query= "Select min(recordkey) as recordkey from widgets";
	$rt=mysql_query($query);          // query executed
	while($nt=mysql_fetch_array($rt)){
		$editwidget=$nt[recordkey];
	}
	mysql_close;
	if(empty($editwidget)){$editwidget="0";}
	$query = "SELECT * from widgets where recordkey=$editwidget";
	$rt=mysql_query($query);          // query executed 
}		
//echo "query=" . $query;		
echo '<div align="left">';
if(mysql_num_rows($rt)!=0){
	while($nt=mysql_fetch_array($rt)){
		$clean_body = $nt['body'];
		//echo "<br>root_folder=" . $root_folder; //for testing

		//$clean_body = str_replace("///", "/", $clean_body);
		//$clean_body = str_replace("//", "/", $clean_body);
		echo '<input type="hidden" name="recordkey" value="' . $nt[recordkey] . '"><form enctype="multipart/form-data" action="' . $_SERVER['PHP_SELF'] . '?a=update&admin=widgets&key=' . $nt[recordkey] . '" method="post">';
		echo '<div align=left>
				<div style="width:300px; float:left;" align="left">Widget name: </div>
				<div style="width:300px; float:left;" align=left>Widget type:</div>
				</div>
			<div align=left>
				<div style="width:300px; float:left;" align="left">
					<input type="text" name="title" value="' .  stripslashes($nt[title]) . '">
				</div>
				<div style="width:300px; float:left;" align="left">
					<select onchange="this.form.submit();" name="widget_type">';
					if(empty($nt[widget_type])){
						$nt[widget_type]=4;
					}
					$typequery = "SELECT * from widget_types";
					$typert=mysql_query($typequery);
					while($typent=mysql_fetch_array($typert)){
						$selected='';
						if($nt[widget_type]==$typent['id']){
							$selected='selected="selected"';
						}
						echo '<option '.$selected.' value="'.$typent['id'].'">'.$typent['type'].'</option>';
					}						
		echo '</select>
				</div>
			</div>';

		echo '<div id="widget-wysiwyg-wrapper" >';
		//Content Editor
		if($nt[widget_type]==10){
			echo '<div align=left><a href="#" data-tooltip="Add your content in this wysiwyg.">Content:</a> </div>
			<div align=left style="width:390px;"><textarea name="widgetbody"  class="ckeditor" id="widgetbody" cols=30>' . $clean_body . '</textarea></div><br/><br/>';
		}
		if($nt[widget_type]==1){echo "<div>Use this widget to add a standard newsletter signup form.</div>";}
		if($nt[widget_type]==2){echo "<div>Use this widget to add a standard SMS/text signup form.</div>";}
		if($nt[widget_type]==3){echo "<div>Use this widget to add a blog navigation menu.</div>";}
		if($nt[widget_type]==4){echo "<div>Upload a single image to be displayed in this widget.</div>";}
		if($nt[widget_type]==5){echo "<div>Upload an image and enter the URL to link to the image.</div>";}
		if($nt[widget_type]==6){echo "<div>Select a pre-built form to be used for this widget.</div>";}
		if($nt[widget_type]==7){echo "<div>Please fill below fields for Yelp widget.<br></div>";}
		if($nt[widget_type]==8){echo "<div>Enter your OpenTable ID to include an OpenTable widget.</div>";}
		if($nt[widget_type]==9){echo "<div>Enter a YouTube video ID (usually a 10-11 character string) or the embed URL to include a video in this widget. <br><br>If you use a full Youtube URL, be sure it has the word 'embed' in the link.<br><br></div>";}
		if($nt[widget_type]==11){echo "<div>Drag fields from the list on the right to build your widget, then drop them in the order you want them.</div>";}

		//IMAGE UPLOAD
		if($nt[widget_type]==4 || $nt[widget_type]==5){
			echo '<div align=left><a href="#">Upload Image:</a> </div>
		<div align=left style="width:390px;"><input type="file" name="image_name" ></div><br/>';
			if(!empty($nt[image_name])){
				echo "<img width='150px' src='/custom/images/".$nt[image_name]."' alt='".$nt[image_name]."'/><br/>";
			}
			echo "<br/>";
		}

		//URL LINK
		if($nt[widget_type]==5){
			echo '<div align=left><a href="#">Link URL:</a> </div>
		<div align=left style="width:390px;"><input type="text" value="'.$nt[link_url].'" name="link_url" ></div><br/><br/>';
		}

		//YELP ID
		if($nt[widget_type]==7){

		/*echo '<div align=left><a href="#">Yelp Id:</a> </div>
		<div align=left style="width:390px;"><input type="text" value="'.$nt[yelp_id].'" name="yelp_id" ></div><br/><br/>';*/

		echo '<br><div align=left><label>Yelp ID of business to display <font color="red">*(required)</font></label> </div>
			  <div align=left style="width:390px;"><input type="text" value="'.$nt[yelp_business_id].'" name="yelp_business_id" size="40"></div><br/>';
			  
		
			  
		/*	  
		echo '<br><div align=left><label>You must provide the following credentials if you expect more than 1000 views per day.</label><br></div>';
			  
		echo '<br><div align=left><label>Yelp Consumer Key</label> </div>
			  <div align=left style="width:390px;"><input type="text" value="'.$nt[yelp_consumer_key].'" name="yelp_consumer_key" size="40"></div><br/>';

		echo '<div align=left><label>Yelp Consumer Secret</label> </div>
			  <div align=left style="width:390px;"><input type="text" value="'.$nt[yelp_consumer_secret].'" name="yelp_consumer_secret"  size="40"></div><br/>';

		echo '<div align=left><label>Yelp Token</label> </div>
			  <div align=left style="width:390px;"><input type="text" value="'.$nt[yelp_token].'" name="yelp_token"  size="40"></div><br/>';

		echo '<div align=left><label>Yelp Token Secret</label> </div>
			  <div align=left style="width:390px;"><input type="text" value="'.$nt[yelp_token_secret].'" name="yelp_token_secret"  size="40"></div><br/>';
		
		*/
		
		echo '<div align=left> </div>
			  <div align=left style="width:390px;">
			    <input id="yelp-show-review" type="checkbox" value="1" name="yelp_show_review" ' . ($nt['yelp_show_review'] == 1 ? 'checked' : '') .'>
			    <label for="yelp-show-review">Show latest customer review</label>
            </div><br/>';
		
		}		

		echo '<br><div align=left><label><b>Note: If you have a high volume of daily website traffic, you may be required to get your own yelp credentials. </b></label><br></div>';
		
		
		//YELP ID
		if($nt[widget_type]==8){
			echo '<div align=left><a href="#">Open Table Id:</a> </div>
		<div align=left style="width:390px;"><input type="text" name="opentable_id" value="'.$nt[opentable_id].'" /></div><br/><br/>';
		}

		//YELP ID
		if($nt[widget_type]==9){
			echo '<div align=left><a href="#">Youtube ID or URL:</a> </div>
		<div align=left style="width:390px;"><input type="text" name="youtube_url" size="50" value="'.$nt[youtube_url].'" /></div><br/><br/>';
		}

		if($nt[widget_type]==11){
			$select = "select * from widget_fields where widget_id = '$editwidget' order by sequence";
			$rt2=mysql_query($select);

			echo '<ul id="catsort" class="dd-list"><p class="widget-heading">Drop selected fields here</p><br>';
			$i=0;
			while($data=mysql_fetch_array($rt2)){
				// echo $data[field_type]."<br>";
               echo '<li class="dd-item" data-id="'.$data[recordkey].'" >
                    	<div class="dd-handle">'.$data[field_type].'
                    	<img class="removeField" src="../images/remove-icon.png"></img>
                    	<div class="popup gear-icon" data-popup-type="'.$data[field_type].'" data-popup-id="'.$i.'" 
                    	id="'.$data[field_type].'" data-popup-content="'.$data[content].'" data-key="'.$data[recordkey].'" 
                    	data-widget-id="'.$data[widget_id].'">
                    	<img src="images/gear-icon.png"></div>
                    	</div>
							<div class="toPopDiv menu-item" id="popup_' . $i . '"> 
							<div class="close"></div>
							<span class="ecs_tooltip">Esc to close <span class="arrow"></span></span>
							<iframe class="menu-item-iframe edit widget" id="iframe-'.$i.'" data-menu-id="' . $i. '" src=""></iframe>
							</div> <!--toPopup end-->
                	</li>';
                $i++;
			}
            echo '</ul>';
		}

		echo '</div>';
		echo '<div id="widget-options-wrapper">';
		if($nt[widget_type]==11){
			// echo "<li>Image";
			// echo "<li>Title";
			// echo "<li>Subtitle";
			// echo "<li>Text block";
			echo '<div class="dd" id="draggable">
			<p class="widget-heading">Available fields:</p><br>
            <ul class="dd-list">
                <li class="dd-item" data-id="100">
                    <div class="dd-handle">Image
                    <img class="removeField" src="../images/remove-icon.png"></img>
                    <div class="popup gear-icon" data-popup-type="image" data-popup-id="1"><img src="images/gear-icon.png"></div>
                    </div>
                </li>
                <li class="dd-item" data-id="200">
                    <div class="dd-handle">Title
                    <img class="removeField" src="../images/remove-icon.png"></img>
                    <div class="popup gear-icon" data-popup-type="title" data-popup-id="2"><img src="images/gear-icon.png"></div>
                    </div>
                </li>
                <li class="dd-item" data-id="300">
                    <div class="dd-handle">Subtitle
                    <img class="removeField" src="../images/remove-icon.png"></img>
                    <div class="popup gear-icon" data-popup-type="subtitle" data-popup-id="3"><img src="images/gear-icon.png"></div>	
                    </div>
                </li>
                 <li class="dd-item" data-id="400">
                    <div class="dd-handle">Text block
                    <img class="removeField" src="../images/remove-icon.png"></img>
                    <div class="popup gear-icon" data-popup-type="textblock" data-popup-id="4"><img src="images/gear-icon.png"></div>	
                    </div>
                </li>
            </ul>
            </div>';
		}

		if($nt[widget_type]==11){


		}
		echo'<h2>Widget Options<br /><hr /></h2><div align=left><a href="#" data-tooltip="This will change the default background color of this widget to the color you select">Background color:</a> </div><div align=left><input name="color" class="color" value="' . $nt[color] . '"><br /><span class="small-warning-text">(click to select from color chart)</span><br /><br /></div>';

		//add selector to choose padding option for this widget
			echo'<a href="#" data-tooltip="This removes all the padding from this widget - use if you have an image for a widget">Padding:</a><br /><select name="widget_padding"><option value="0"> - Standard - </option>';
			echo '<option value="1"';
			if($nt[no_padding]=='1'){echo ' selected="selected"';}
			echo'>No Padding</option>';
			echo '</select><br />';
		
		echo '<div class="widget-checkboxes" align=left><input type="checkbox" name="chkActive"';
		if(!empty($nt[active])) {
		  echo ' checked=checked';
		}
		echo '"> &nbsp; <a href="#" data-tooltip="If checked, this widget will be available to add to your pages.">Active</a></div>';

		echo '<div class="widget-checkboxes" align=left><input type="checkbox" name="chknoborder"';
		if(!empty($nt[noborder])) {
		  echo ' checked=checked';
		}
		echo '"> &nbsp; <a href="#" data-tooltip="If checked, removes border from this widget.">Hide border</a></div>';
		
		echo '<div class="widget-checkboxes" align=left><input type="checkbox" name="chknocolor"';
		if(!empty($nt[nocolor])) {
		  echo ' checked=checked';
		}
		echo '"> &nbsp; <a href="#" data-tooltip="If checked, removes background color for this widget.">Hide back color</a></div><br>';
		if($nt[widget_type]==10 || $nt[widget_type]==6){
	   //add form selector to include a form in this widget
			echo'Include form:<br /><select name="formnumber"><option value=""> - Select a form - </option>';
				$forms="Select recordKey, formName from formData where formID=0 AND formName>''";
				$frm=mysql_query($forms);
				while($fnt=mysql_fetch_array($frm)){
					echo '<option value='. $fnt[recordKey];
					if($fnt[recordKey]==$nt[formNumber]){echo ' selected="selected"';}
					echo'>' .$fnt[formName] . '</option>';
				}

				mysql_close;
				echo '</select>';
		}
	  
		echo '<div align=left><br /><input type="submit" id="save-button" name="save" value="Save Widget"><br /><br /></div>';
		

		//display delete checkbox if widget is not a locked, built-in widget
		if(empty($nt[locked])) {
		echo '<div align=left><input type="checkbox" name="chkDelete"> &nbsp; Delete<br><span class="small-warning-text">Be sure you mean to delete this widget if you check this box before saving. You will not be prompted again.</span><br /><br /><br /></div></div>';
	  }
	  
	  echo '</form>';
	
	//}
	}
}
echo '<br><br></div>';

	

	

?>
<script type="text/javascript" >
CKEDITOR.replace( 'widgetbody',
{
	filebrowserBrowseUrl : 'ckfinder/ckfinder.html',
	filebrowserImageBrowseUrl : 'ckfinder/ckfinder.html?type=Images',
	filebrowserFlashBrowseUrl : 'ckfinder/ckfinder.html?type=Flash',
	filebrowserUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
	filebrowserImageUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
	filebrowserFlashUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash',
	toolbarGroups: [
		{ name: 'clipboard',   groups: [ 'clipboard', 'undo' ] },
		{ name: 'editing',     groups: [ 'find', 'selection', 'spellchecker' ] },
		{ name: 'links' },
		{ name: 'insert' },
		
		{ name: 'others' },
		'/',
		{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },
		{ name: 'paragraph',   groups: [ 'list', 'indent', 'blocks', 'align' ] },
		{ name: 'styles' },
		{ name: 'colors' },
		{ name: 'mode' },

	],
    uiColor: '#499ED7'
});
</script>
<script type="text/javascript">
  $(function() {
var receive = false;
    $( "#draggable li" ).draggable({
	      connectToSortable: "#catsort",
     	  helper: "clone",
    }).disableSelection();

    $( "#catsort" ).sortable({
    	receive: function(event, ui) {
			receive = true;
    	},

    	update: function(event, ui) {

	if(receive == true){

    		var text = ui.item.text();
			fieldType =  $.trim(text);
			$.ajax({
					type: 'POST',
					url: 'save_fields_sequence.php',
					data: { 
							'fieldType' : fieldType,
							'widgetId' : $('[name="widget"]').val()
						},
				}).done(function(response) {
					if (response == '1') {
						alert('Error updating social networks order.');
					}else{
						receive = false;
						ui.item.attr('data-id',response); 
						updateSequence();
					}
				});
			}else{
				updateSequence();
			}

			
			},
    }).disableSelection();

  });

 $('#catsort').on( "click",'.popup.gear-icon', function() {
        var popupId = $(this).data('popup-id');
        var type = $(this).data('popup-type');
        var content = $(this).data('popup-content');
        var key = $(this).closest('li').attr('data-id');
        var widgetId = $('[name="widget"]').val();
        $('#iframe-'+popupId).attr('src', 'editwidgetsiframe.php?action=edit&type='+type+'&content='+content+'&key='+key+'&widgetId='+widgetId);
    });


 $('#catsort').on( "click",'.removeField', function() {
        var key = $(this).closest('li').attr('data-id');
        $.ajax({
			type: 'POST',
			url: 'save_fields_sequence.php',
			data: { 
					'deleteFieldKey' : key
				},
		}).done(function(response) {
			if (response == '0') {
				alert('Error updating social networks order.');
			}else{
				window.parent.location.href="editwidgets.php";
			}
		});

        //$('#iframe-'+popupId).attr('src', 'editwidgetsiframe.php?action=edit&type='+type+'&content='+content+'&key='+key);
    });


function updateSequence() 
{
	var idArray = new Array();
	$("#catsort li").each(function(i, tr) {
	var id = $(tr).data('id');
	var sort_order = i;
	idArray.push(id);
	});
	$.ajax({
		type: 'POST',
		url: 'save_fields_sequence.php',
		data: { 
				'updateArray' : idArray
			},
	}).done(function(response) {
		if (response == '0') {
			alert('Error updating social networks order.');
		}
	});
}


</script>
