// This file is Part of the "Customer Added Products" Modifications to iDevCart
//
// The contents of this file, unless indicated otherwise, are copyright (C) 2011
// by Rick L. Bird (hereinafter the "developer").  All rights are reserved and
// any copying of this file is unlawful unless a valid, written license to copy
// it has been granted by the copyright holder or his authorized agent.  The
// copyright holder and his agents expressly reserve the right to issue a DMCA
// take-down notice and/or take legal action against any person or entity who
// makes a copy of this file or any part of it for any purpose without a valid,
// written license.  Installing this file in a production or commercial
// environment constitutes making a copy.
// 
// THE DEVELOPER EXPRESSLY DISCLAIMS ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR
// A PARTICULAR PURPOSE, OR ANY OTHER WARRANTY WHETHER EXPRESS OR IMPLIED.  THE
// DEVELOPER IS NOT LIABLE FOR ANY INJURY OR DAMAGES RESULTING FROM THE USE OF
// THE FILE OR ANY PRODUCT OF WHICH THIS FILE IS A PART.


// dynamic code blocks
var original_dync;
var header_content;

function checkKey(obj)
{
        var nums="1234567890@#&-_ qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
        o=obj
        if(o.value.length==0) return;
        lchar = o.value.substr(o.value.length-1,1)
        if(nums.indexOf(lchar)==-1) o.value = o.value.substr(0,o.value.length-1);
}


function dync_Add_Product(form, files) {
   var new_field_object = new Object();
   var $form_fields = $('#product_form :input');
// OK First Lets put a loader image in place
  $('#upload_header_text').html("Processing....");
  $('#upload-dynamic-content').html("<img src=\"/images/image-loader.gif\">");
  // Create our new form field
  $form_fields.each(function() {
	new_field_object[this.name] = $(this).val();
	});
   new_field_object["image_files"] = files;
   $.ajax({
			type: 'POST',
			url: "includes/dync_custom_product.php", 
			success: function(resp) {
						$('#upload-dynamic-content').html(resp);
					},
			data : $.param(new_field_object)
        });   
}


function dync_Refresh_Sidebar() {
 $.ajax({ url: "includes/dync_sidebar.php",
          success: function(resp) { 
			$('#sidebar').html(resp); 
		  }
	     });	
}

function dync_Uploadify() {
 original_dync = $('#upload-dynamic-content').html();
 $.ajax({ url: "includes/dync_uploadify.php",
          success: function(resp) { 
			$('#upload-dynamic-content').html(resp); 
		  }
	     });	
}

function dync_loginform() {
 $('#contentheader').html(""); // Clears the graphic for login.
 $.ajax({ url: "includes/upload_loginform.php",
          success: function(resp) { 
			$('#upload-dynamic-content').html(resp); 
		  }
	     });	
}

function dync_share_design(id, shared) {
   $.ajax({
			type: 'POST',
			url: "includes/share_product.php", 
			success: function(resp) {
						if(resp < 1 ) {
							alert("Could not complete request.  Error: "+ resp);
						} else if(shared) {
						$('#product_share').html("<input type=\"button\" name=\"share_button\" value=\"Unshare Design\" onClick=\"return dync_share_design("+ id +","+0+");\"/>");
						} else {
						$('#product_share').html("<input type=\"button\" name=\"share_button\" value=\"Share Design\" onClick=\"return dync_share_design("+id+","+1+");\"/>");
						}
					
					},
			data : {
					share: shared,
					id: id
				}
        });    
}

function nv_check_login(check) {
 if(check == 1) {
			   dync_Refresh_Login();
			   dync_reset_header(); 
			   dync_Uploadify();
			   dync_Refresh_Sidebar();
	} else {
		dync_loginform();
		// Give us a nice spiffy header.
		$('#contentheader').html("<h1 id=\"upload_header_text\">Invalid User or Password..</h1>");
	}
}

function dync_process_login(form) {
   $.ajax({
			type: 'POST',
			url: "includes/process_login.php", 
			success: function(resp) { nv_check_login(resp); },
			data : $('#loginform').serialize(true)
        });   
}

function dync_Refresh_Login() {
 $.ajax({url: "includes/dync_islogged.php", 
        success: function(resp) {
        $('.header-links').html(resp);
        }
	});
}

function reset_dync_box(myform) {
  $('#upload-dynamic-content').html(original_dync);
}


function dync_reset_header() {
	$('#contentheader').html("<img src=\"images/faq-header.jpg\" title=\"FAQ\"/>");
}


function dync_Register() {
 original_dync = $('#upload-dynamic-content').html();
 
 $.ajax({url: "includes/dync_register.php", 
        success: function(resp) {
		$('#contentheader').html("");
        $('#upload-dynamic-content').html("\n<br><button type=\"button\" onClick=\"dync_reset_header(); return reset_dync_box();\">Back</button><br>\n" + resp);
        }
		});
}

function dync_Process_Register() {
   $.ajax({
			type: 'POST',
			url: "includes/dync_process_register.php", 
			success: function(resp) {
						$('#upload-dynamic-content').html(resp);
					},
			data : $('#register_form').serialize(true)
        }); 
    return false;
}

// $(document).ready(function(){$('#button a').click(function(){var integer=$(this).attr('rel');$('#drumparent .cover').animate({left:-425*(parseInt(integer)-1)})
// $('#button a').each(function(){$(this).removeClass('active');if($(this).hasClass('button'+integer)){$(this).addClass('active')}});});});
