/*

Owner: Explore Consulting
Copyright (c) 2006, Explore Consulting
All rights reserved.


Agreement

Redistribution and use of this software in source and binary forms, with or 
without modification, are permitted provided that all of the following 
conditions are met:

(1) Redistributions of source code must retain:
	(i) the above copyright notice,
 	(ii) this list of redistribution conditions, and 
 	(iii) the disclaimer appearing below.

(2) Redistributions in binary form must retain: 
	(i) the above copyright notice, 
	(ii) this list of redistribution conditions, and 
	(iii) the disclaimer appearing below in the documentation and/or any other 
	materials provided with the distribution.
	
(3)	Neither the name of the Explore Consulting nor the names of its 
	contributors may be used to endorse or promote products derived from this 
	software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

// BEGIN SCRIPT DESCRIPTION BLOCK  ====================================
/*
Company Name: 		Explore Consulting - www.exploreconsulting.com

File Name:		addCalanderDate.js

Creation Date: 		9/30/06
	
Version:		1.00

Type of Script:		Client

Sub-Type:		Calculation

Categories:		Calendar

Description:
Takes current date and adds a pre-specified amount of days from pre-defined list.  Converts the date into a format that NetSuite will be able to use internally.

Business Problem Being Solved:
Client needs a invisible field to be populated on when to contact customer.  Depending on customer input, the calendar will be updated from anywhere of 1 month to 1 year from current date

Installation Instructions:
1. Create an online form with a drop down list of integers.  These integers will be the number of days to add to todays date.
2. Create a field with date type that will be automatically populated upon user input.  This will contain the future date.
3. On the field created in step 2, call the function createDate()
4. Adjust variable names as necessary

License
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
// END SCRIPT DESCRIPTION BLOCK  ======================================



function createDate(){
	
	//name of selectedDateList must correspond with the name of the field with the list values for the amount of days to add
	var selectedDate = document.main_form.selectedDateList.value;
	var currentDate = new Date();
	var modifiedDate = addDays(currentDate, selectedDate);
	
	var day = modifiedDate.getDate();
	var month = modifiedDate.getMonth()+1;
	var year = modifiedDate.getFullYear();
	
	//name of custentitypurchasebydate must correspond with the field in which you want the new added date to be entered into (will work with NetSuite date format)
	document.main_form.newDate.value = month +'/'+ day +'/'+ year;

}


function addDays(d, daystoadd){

             var d2 = new Date(d.getTime() + 86400 * daystoadd * 1000);
			 
             if (d2.getHours() != d.getHours()){
                        if ((d.getHours() > 0 && d2.getHours() < d.getHours()) || (d.getHours() == 0 && d2.getHours() == 23))
                                    d2.setTime(d2.getTime() + 3600*1000);
                        else
                                    d2.setTime(d2.getTime() - 3600*1000);
             }

             d.setTime(d2.getTime());

             return d;

}
