var slotCount = 0;
var minimumSlots = 1;

function addPhotoId(element_id, photo_id){
	document.getElementById(element_id).value = photo_id;
}

function doAjaxSaveText(){

	var name = new Array();
	var caption = new Array();
	var month = new Array();
	var day = new Array();
	var year = new Array();
	var sex = new Array();
	var photoId = new Array();
	var myForm =  document.getElementById('hidden_form');
	
	for(var i = 0; i<slotCount; i++){
	    var nameStr = 'childname_'+(i+1).toString();
	    var captionStr = 'caption_'+(i+1).toString();
	    var monthStr = 'month_'+(i+1).toString();
	    var dayStr = 'day_'+(i+1).toString();
	    var yearStr = 'year_'+(i+1).toString();
	    var maleStr = 'male_'+(i+1).toString();
	    var femaleStr = 'female_'+(i+1).toString();
	    var photoIdStr = 'hidden_photo_id_'+(i+1).toString();
	  
	    name[i] = document.getElementById(nameStr).value;
	    caption[i] = document.getElementById(captionStr).value;
	    month[i] = document.getElementById(monthStr).value;
	    day[i] = document.getElementById(dayStr).value;
	    year[i] = document.getElementById(yearStr).value;
	    photoId[i] = document.getElementById(photoIdStr).value;
		if(document.getElementById(maleStr).checked)
		     sex[i] = 'M';
		else if(document.getElementById(femaleStr).checked)
			sex[i] = 'F';
		else
		    sex[i]='empty';
    }// for loop
       
    //trying to determine if any photo was uploaded
    var totalEmpty = 0;
    for(var i = 0; i < slotCount; i++){
    	if(slotCount != 0 && photoId[i] == 'empty')
    		totalEmpty++;
    	if(totalEmpty == slotCount){
    		alert('You have not uploaded any images yet');
    		return;
    	}
    }
    
    for(var i = 0; i < slotCount; i++){
    	//check DOB
    	if( (month[i]== 0 || day[i] == 0 || year[i] == 0) && (photoId[i] != 'empty') ){
    		alert('Date of Birth information is required for each uploaded image');
    		return;
    	}
        var birthday    = new Date(year[i],month[i]-1,day[i]);
	var today       = new Date();
	if(today < birthday )
	{       
		alert('The birthday for each uploaded image must be in the past'); 
		return;
	}
    	if( trim(name[i]).length <=0 && photoId[i] != 'empty' ){
    		alert('The name is required for each uploaded image');
    		return;
    	}
    	if(sex[i] == 'empty'  && (photoId[i] != 'empty') ){
    	    
    		alert('A gender is required for each uploaded image');
    		return;
    	}
    }
 
 
 	for(var i = 0; i < slotCount; i++){  
 	    var nameStr = 'childname_'+(i+1).toString();
	    var captionStr = 'caption_'+(i+1).toString();
	    var monthStr = 'month_'+(i+1).toString();
	    var dayStr = 'day_'+(i+1).toString();
	    var yearStr = 'year_'+(i+1).toString();
	    var maleStr = 'male_'+(i+1).toString();
	    var femaleStr = 'female_'+(i+1).toString();
	    var photoIdStr = 'hidden_photo_id_'+(i+1).toString();
	    var sexStr = 'sex_'+(i+1).toString();
 	
 		currentElement = document.createElement("input");
		currentElement.setAttribute("type", "hidden");
		currentElement.setAttribute("name", nameStr);
		currentElement.setAttribute("id", nameStr);
		currentElement.setAttribute("value", name[i]);
		myForm.appendChild(currentElement);
		
		currentElement = document.createElement("input");
		currentElement.setAttribute("type", "hidden");
		currentElement.setAttribute("name", captionStr);
		currentElement.setAttribute("id", captionStr);
		currentElement.setAttribute("value", caption[i]);
		myForm.appendChild(currentElement);
		
		currentElement = document.createElement("input");
		currentElement.setAttribute("type", "hidden");
		currentElement.setAttribute("name", monthStr);
		currentElement.setAttribute("id", monthStr);
		currentElement.setAttribute("value", month[i]);
		myForm.appendChild(currentElement);
		
		currentElement = document.createElement("input");
		currentElement.setAttribute("type", "hidden");
		currentElement.setAttribute("name", dayStr);
		currentElement.setAttribute("id", dayStr);
		currentElement.setAttribute("value", day[i]);
		myForm.appendChild(currentElement);
		
		currentElement = document.createElement("input");
		currentElement.setAttribute("type", "hidden");
		currentElement.setAttribute("name", yearStr);
		currentElement.setAttribute("id", yearStr);
		currentElement.setAttribute("value", year[i]);
		myForm.appendChild(currentElement);
		
		currentElement = document.createElement("input");
		currentElement.setAttribute("type", "hidden");
		currentElement.setAttribute("name", sexStr);
		currentElement.setAttribute("id", sexStr);
		currentElement.setAttribute("value", sex[i]);
		myForm.appendChild(currentElement);
		
		currentElement = document.createElement("input");
		currentElement.setAttribute("type", "hidden");
		currentElement.setAttribute("name", 'total');
		currentElement.setAttribute("id", 'total');
		currentElement.setAttribute("value", slotCount);
		myForm.appendChild(currentElement);
		
		currentElement = document.createElement("input");
		currentElement.setAttribute("type", "hidden");
		currentElement.setAttribute("name", photoIdStr);
		currentElement.setAttribute("id", photoIdStr);
		currentElement.setAttribute("value", photoId[i]);
		myForm.appendChild(currentElement);
 	}
    myForm.submit();
}
function doAjaxSubmit(formName,updateElementId){
    //document.forms[formName].setAttribute("target","hidden_iframe");
	document.forms[formName].submit();
}


function addSlot(numSlot)
{	
	var main = document.getElementById('control');
	var newElement=document.createElement("div");
	slotCount++;

                var month = '';
		month = 
		"<option value='0'>Month</option>"+
		"<option value='1'>January</option><option value='2'>February</option>"+
		"<option value='3'>March</option><option value='4'>April</option>"+
		"<option value='5'>May</option><option value='6'>June</option>"+
		"<option value='7'>July</option><option value='8'>August</option>"+
		"<option value='9'>September</option><option value='10'>October</option>"+
		"<option value='11'>November</option><option value='12'>December</option>";

               var day = '';
               for(i = 1; i <=31; i++){
                  day += "<option value='"+i+"'>"+i+"</option>";	
		}

	var year = '';
	var myDate = new Date();
	var syear	= myDate.getFullYear() -18;
	var eyear	= myDate.getFullYear();
  for(i = eyear; i >= 1988; i--){
  	year += "<option value='"+i+"'>"+i+"</option>";	
		}

 //target='hidden_iframe'
 
           /*
            IMPORTANT NOTE 
            TEMPLATE_IMAGE_URL is set in the .php that is is being included in            
            */
		var code = '<div class="uploadphotobox" style="margin-top:3px">'+
        ' <form method="post" action="../galleryupload" id="form'+(slotCount)+'" enctype="multipart/form-data" target="hidden_iframe">'+
            "<input type='hidden' name='this_slot_num' value='"+(slotCount)+"'/>"+
            "<input type='hidden' name='gal_id' value='"+(galleryId)+"'/>"+
            "<input type='hidden' id='hidden_photo_id_"+(slotCount)+"' value='empty'/>"+
            '<div class="leftcolumnphoto"> Photo<br />'+
              '<span class="uploadphotofield">'+
             " <input onchange='doAjaxSubmit(\"form"+(slotCount)+"\",\"image_holder"+(slotCount)+"\")' name='photo1' type='file' size='15' />"+
            '  </span><br />'+
             ' <br />'+
             ' <div id="image_cont_'+(slotCount)+'" style="padding-left:20px;"><img src="'+TEMPLATE_IMAGE_URL+'content/photohere.jpg" /></div><br />'+
            ' <!--<span id="remove'+(slotCount)+'" onclick="removeSlotIndex('+(slotCount)+');">   <a class="uploads" href="javascript:void(0);">Remove</a></span> --> </div>'+
          '  <span class="rightcolumninfo">First Name:<br />'+
          '  <input size="25" type="text" id="childname_'+(slotCount)+'" name="childname_'+(slotCount)+'"/>'+
          '  </span><br />'+
           ' <br />'+
          '  Child\'s Gender<br />'+
          '  <input  name="sex" id="male_'+(slotCount)+'" type="radio" />'+
          '  <span class="contentbody"> Boy'+
           '  <input  name="sex" id="female_'+(slotCount)+'" type="radio" />'+
          '  Girl</span><br />'+
          '  <br />'+
          '  Child\'s Date of Birth<br />'+
          '  <select name="month" id="month_'+(slotCount)+'">'+
          month +
          '  </select>'+
          '  &nbsp;&nbsp;'+
          '  <select name="day" id="day_'+(slotCount)+'">'+
          '    <option>Day</option>'+day+
          '  </select>'+
          '  &nbsp;&nbsp;'+
          '  <select name="year" id="year_'+(slotCount)+'"><option>Year</option>'+year+
          '  </select>'+
          '  <span class="rightcolumninfo"> <span class="contentbody"><br />'+
          '  <br />'+
          '  Caption <i>(Optional)</i></span><br />'+
          '  <input size="25" type="text" name="caption" id="caption_'+(slotCount)+'"/>'+
          '  </span>'+
          '</form></div>';

	newElement.innerHTML=code;
	newElement.setAttribute('id','choiceContainer'+slotCount.toString());
	main.appendChild(newElement);
	//show remove next to input
	if(slotCount>minimumSlots)
	{
		for(var i=0;i<minimumSlots;i++)
		{
			var tag=document.getElementById('remove'+(i+1).toString());
			tag.innerHTML='<a href="javascript:void();" class="link2">Remove</a>';
		}
	}
}
function update(removedIndex)
{
	var totalLoops=(slotCount+1)-removedIndex;
	//next index to be updated
	var updateIndex=removedIndex+1;
	for(var i=0;i<totalLoops;i++)
	{
		
		var tag=document.getElementById('choicetext'+(updateIndex).toString());
		tag.innerHTML='Choice '+(updateIndex-1).toString()+' ';
		tag.setAttribute('id','choicetext'+(updateIndex-1).toString());
		
		var tag=document.getElementById('remove'+(updateIndex).toString());
		tag.setAttribute('id','remove'+(updateIndex-1).toString());
		tag.setAttribute('onclick', 'removeSlotIndex(' +  (updateIndex-1).toString()+ ')' );
		
		var tag=document.getElementById('choice'+(updateIndex).toString());
		tag.setAttribute('id','choice'+(updateIndex-1).toString());
		tag.setAttribute('name','choice'+(updateIndex-1).toString());
		
		var tag=document.getElementById('choiceContainer'+(updateIndex).toString());
		tag.setAttribute('id','choiceContainer'+(updateIndex-1).toString());
		
		updateIndex++;
	}
}
function removeSlotIndex(index)
{
	if(slotCount>minimumSlots)
	{
		//get node to remove
		var thisNode=document.getElementById('choiceContainer'+index.toString());
		//remove the node
		document.getElementById('control').removeChild(thisNode);
		//one less element
		slotCount--;
	}
	
	update(index);
	
	if(slotCount<=minimumSlots)
	{
		for(var i=0;i<2;i++)
		{
			var tag=document.getElementById('remove'+(i+1).toString());
			tag.innerHTML='';
		}
	}
}
function checks()
{
	var error='';
	if(trim(document.polldata['question'].value)=='')
	{
		error+='Poll Question is empty\n';
	}
	if(trim(document.polldata['pollname'].value)=='')
	{
		error+='Poll Name is empty\n';
	}
	for(var i=0;i<slotCount;i++)
	{
		if(trim(document.polldata['choice'+(i+1).toString()].value)=='')
		{
			error+='Choice '+(i+1).toString()+' is empty\n';
		}
	}
	if(error=='')
	{
		var main = document.getElementById('hidden');
		var newElement=document.createElement("input");
		newElement.setAttribute('name','hiddenval');
		newElement.setAttribute('type','hidden');
		newElement.setAttribute('value',slotCount.toString());
		main.appendChild(newElement);
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

function change()
{
	generatepreview();
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
window.document.onload = function(){
	//alert('ok 2');
	//addSlot(1);
	//addSlot(2);
	//addSlot(3);
}


function test(){
	addSlot(1);
}
function voteChecks(voteids){
    voteids = trim(voteids);
    voteids_arr = voteids.split(',');
    var boxesChecked = false;
    var checkedIds = new Array();
    var index = 0;
    for(var i =0; i < voteids_arr.length; i++){
        if($('voteforimage_'+voteids_arr[i]) && $('voteforimage_'+voteids_arr[i]).checked){
            boxesChecked = true;
            index = checkedIds.length;
            checkedIds[index] = voteids_arr[i];
        }//if
    }//for
    if(boxesChecked == false){
        alert('Please select photos you would like to vote for!');
        return false;    
    }//if

    var vote_str = checkedIds.join(',');
    $('voteIds').value=vote_str;
    return true;
}

