/* Created with PageBuilder version 4.04 on Fri Jan 31 09:38:00 2014 */

#include "AsExtern.h"

#if RomPagerServer

// type used for the temp and permanent structures
typedef struct _theDataCache
{
	char * theName;
	int theNameLen;
	char * theStreetAddress;
	int theAddressLen;
	char * theCity;
	int theCityLen;
	char * theState;
	int theStateLen;
	char * theZipCode;
	int theZipCodeLen;
} theDataCache;

// used before post form processing API is called
theDataCache * theTempDataCache;
// if warranted move data from temp to here
theDataCache * thePermDataCache;

char * whichSubmitButton; // holds the value of the pressed submit button
#define MAX_LENGTH_SUBMIT_VALUE 64 // max length for a submit button value
#define MAX_STRING_LEN 128 // max langth for all strings in structures

int mallocAllFieldsInStruct(theDataCache * theStruct); // at start mallocs all structures

// on submit this API moves values from the temp structure to the perm structure
int copyTempToPerm(theDataCache * localTempDataCache, theDataCache * localPermDataCache);

/* ********************************************************* */
/* *    Built from "html\web_page_multiple_submits.htm"    * */
/* ********************************************************* */

// return the current value for the name
extern char *getTheName(void);
char *getTheName(void) {
	//char * theResult;
	printf("In getTheName\n");

	return thePermDataCache->theName;
}

// set the name value in the temp structure only
extern void setTheName(char *theValuePtr);
void setTheName(char *theValuePtr) {

	    printf("In setTheName\n");
	    if(theTempDataCache->theName != NULL)
	    {

    	    memcpy(theTempDataCache->theName, theValuePtr, strlen(theValuePtr));
    	    theTempDataCache->theName[strlen(theValuePtr)] = 0;
    	    theTempDataCache->theNameLen = strlen(theTempDataCache->theName);
    	    printf("Got name %s length %d %s\n", theTempDataCache->theName, theTempDataCache->theNameLen, theValuePtr);
	    }



	return;
}

// get the current address value
extern char *getTheStreetAddress(void);
char *getTheStreetAddress(void) {
	//char * theResult;
	printf("In getTheStreetAddress\n");

	return thePermDataCache->theStreetAddress;
}


// set the address value to temp only
extern void setTheStreetAddress(char *theValuePtr);
void setTheStreetAddress(char *theValuePtr) {
	printf("In setTheStreetAddress\n");


        if(theTempDataCache->theStreetAddress != NULL)
        {
	        strcpy(theTempDataCache->theStreetAddress, theValuePtr);
	        theTempDataCache->theAddressLen = strlen(theTempDataCache->theStreetAddress);
	        printf("Got name %s length %d\n", theTempDataCache->theStreetAddress, theTempDataCache->theAddressLen);
        }

	return;
}


// get the current City value
extern char *getTheCity(void);
char *getTheCity(void) {
	//char * theResult;
	printf("In getTheCity\n");

	return thePermDataCache->theCity;
}

// set the City value to temp only
extern void setTheCity(char *theValuePtr);
void setTheCity(char *theValuePtr) {
	printf("in setTheCity\n");



    if(theTempDataCache->theCity != NULL)
    {
	    strcpy(theTempDataCache->theCity, theValuePtr);
	    theTempDataCache->theCityLen = strlen(theTempDataCache->theCity);
	    printf("Got name %s length %d\n", theTempDataCache->theCity, theTempDataCache->theCityLen);
    }

	return;
}

// get the current state value
extern char *getTheState(void);
char *getTheState(void) {
	//char * theResult;
	printf("in getTheState");

	return thePermDataCache->theState;
}


// Set the state value to temp only
extern void setTheState(char *theValuePtr);
void setTheState(char *theValuePtr) {
	printf("In setTheState\n");

    if(theTempDataCache->theState != NULL)
    {


	    strcpy(theTempDataCache->theState, theValuePtr);
	    theTempDataCache->theStateLen = strlen(theTempDataCache->theState);
	    printf("Got name %s length %d\n", theTempDataCache->theState, theTempDataCache->theStateLen);
    }


	return;
}

// get the current zip code value
extern char *getTheZipCode(void);
char *getTheZipCode(void) {
	//char * theResult;
	printf("in getTheZipCode\n");


	return thePermDataCache->theZipCode;
}


// set the zip code value to temp only
extern void setTheZipCode(char *theValuePtr);
void setTheZipCode(char *theValuePtr) {
	printf("In setTheZipCode\n");

    if(theTempDataCache->theZipCode != NULL)
    {

	    strcpy(theTempDataCache->theZipCode, theValuePtr);
	    theTempDataCache->theZipCodeLen = strlen(theTempDataCache->theZipCode);
	    printf("Got name %s length %d\n", theTempDataCache->theZipCode, theTempDataCache->theZipCodeLen);
    }


	return;
}


// This API is called on the clicking of one fo the submit buttons
extern void postProcessTheForm(void *theTaskDataPtr, Signed16Ptr theIndexValuesPtr);
void postProcessTheForm(void *theTaskDataPtr, Signed16Ptr theIndexValuesPtr) {

	printf("In postProcessTheForm\n");
	// which button was pressed?


	// get the value on the submit button presses
	if(theTaskDataPtr != NULL)
	{
		whichSubmitButton = RpGetSubmitButtonValue(theTaskDataPtr);
	}
	else
	{
		// it is unlikely this will ever be called. Itg is for defensive programming only
		printf("danger, AWS not running!\n");
		return;
	}
	printf("The %s submit button was pressed\n", whichSubmitButton);
	// move temp values to perm values as needed
	if(strcmp("update",whichSubmitButton) == 0)
	{
		copyTempToPerm(theTempDataCache, thePermDataCache);
	}

	// clear out the name value in perm structure so the get returns blank
	if(strcmp("clearName", whichSubmitButton) == 0)
	{
		memset(thePermDataCache->theName, '\0', MAX_STRING_LEN);
		thePermDataCache->theNameLen = 0;
	}

	// clear out the address value so the get returns blank
	if(strcmp("clearStreetAddress", whichSubmitButton) == 0)
	{
		memset(thePermDataCache->theStreetAddress, '\0', MAX_STRING_LEN);
		thePermDataCache->theAddressLen = 0;
	}

	// clear out the zip code value so the get returns blank
	if(strcmp("clearZipCode", whichSubmitButton) == 0)
	{
		memset(thePermDataCache->theZipCode, '\0', MAX_STRING_LEN);
		thePermDataCache->theZipCodeLen = 0;
	}

	return;
}


// This API is called by the engine when a page is started before we process any fields in the form
extern void prePageProcessing(void *theTaskDataPtr, Signed16Ptr theIndexValuesPtr);
void prePageProcessing(void *theTaskDataPtr, Signed16Ptr theIndexValuesPtr) {
	printf("In prePageProcessing\n");


	// only malloc if it has not been done yet (temp)
	if(theTempDataCache == NULL)
	{
		theTempDataCache = malloc(sizeof(theDataCache));
		if(theTempDataCache == NULL)
		{
			printf("Unable to malloc temp cache\n");
			return;
		}
		memset(theTempDataCache, '\0',sizeof(theDataCache));
		printf("mallocing temp fields\n");
		// malloc all fields
		mallocAllFieldsInStruct(theTempDataCache);
	}


    // only malloc if it has not been done yet (perm)
	if(thePermDataCache == NULL)	{
		thePermDataCache = malloc(sizeof(theDataCache));
		if(thePermDataCache == NULL)
		{
			printf("Unable to malloc perm cache\n");
			return;
		}
		else
		{
			// only clear perm cache on first attempt
			memset(thePermDataCache, '\0',sizeof(theDataCache));
			printf("Mallocing perm fields\n");
			// malloc all fields
			mallocAllFieldsInStruct(thePermDataCache);
		}
	}


	return;
}

// call this API to malloc all fields in a structure
int mallocAllFieldsInStruct(theDataCache * theStruct)
{
	printf("In mallocAllFieldsInStruct\n");
	if(theStruct == NULL)
	{
		printf("In mallocAllFieldsInStruct NULL struct error\n");
		return -1;
	}

	// malloc name of struct
    if(theStruct->theName == NULL)
    {
    	theStruct->theName = malloc(MAX_STRING_LEN);
    	if(theStruct->theName == NULL)
    	{
    		printf("Unable to malloc name\n");
    		return -1;
    	}
    	memset(theStruct->theName, '\0', MAX_STRING_LEN);
    }

    // malloc address of struct
    if(theStruct->theStreetAddress == NULL)
    {
    	theStruct->theStreetAddress = malloc(MAX_STRING_LEN);
    	if(theStruct->theStreetAddress == NULL)
    	{
    		printf("Unable to malloc theStreetAddress\n");
    		return -1;
    	}
    	memset(theStruct->theStreetAddress, '\0', MAX_STRING_LEN);
    }

    // malloc city of struct
    if(theStruct->theCity == NULL)
    {
    	theStruct->theCity = malloc(MAX_STRING_LEN);
    	if(theStruct->theCity == NULL)
    	{
    		printf("Unable to malloc theCity\n");
    		return -1;
    	}
    	memset(theStruct->theCity, '\0', MAX_STRING_LEN);
    }

    // malloc state of struct
    if(theStruct->theState == NULL)
    {
    	theStruct->theState = malloc(MAX_STRING_LEN);
    	if(theStruct->theState == NULL)
    	{
    		printf("Unable to malloc theState\n");
    		return-1 ;
    	}
    	memset(theStruct->theState, '\0', MAX_STRING_LEN);
    }

    // malloc zip of struct
    if(theStruct->theZipCode == NULL)
    {
    	theStruct->theZipCode = malloc(MAX_STRING_LEN);
    	if(theStruct->theZipCode == NULL)
    	{
    		printf("Unable to malloc zipcode\n");
    		return -1;
    	}
    	memset(theStruct->theZipCode, '\0', MAX_STRING_LEN);
    }
    return 0;

}

// call this API to copy all fields from temp storage to perm storage on click of an updte submit button
int copyTempToPerm(theDataCache * localTempDataCache, theDataCache * localPermDataCache)
{
	if((localTempDataCache == NULL) || localPermDataCache == NULL)
	{
		printf("NULL pointer violation - copyTempToPerm\n");
		return -1;
	}
	strcpy(localPermDataCache->theName, localTempDataCache->theName);
	localPermDataCache->theNameLen = localTempDataCache->theNameLen;
	strcpy(localPermDataCache->theStreetAddress, localTempDataCache->theStreetAddress);
	localPermDataCache->theAddressLen = localTempDataCache->theAddressLen;
	strcpy(localPermDataCache->theCity, localTempDataCache->theCity);
	localPermDataCache->theCityLen = localTempDataCache->theCityLen;
	strcpy(localPermDataCache->theState, localTempDataCache->theState);
	localPermDataCache->theStateLen = localTempDataCache->theStateLen;
	strcpy(localPermDataCache->theZipCode, localTempDataCache->theZipCode);
	localPermDataCache->theZipCodeLen = localTempDataCache->theZipCodeLen;

	return 0;
}

#endif	/* RomPagerServer */
