/*
Licensed under the Creative Commons GNU GPL
Copyright (C) 2007 Shawn Biddle


This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

A Human Readable version can be found at 
http://creativecommons.org/licenses/GPL/2.0/ 

Title:  Basic Webpage Generation Application

Author: Shawn Biddle

Date of Last Access: Feb 25, 2007

Description:
Takes user input for the Title, Heading and Body Text of the page.
All get functions are separate as well as the writePage so "modules"
can easily be added on later.
*/

#include <iostream>
#include <fstream>
#include <windows.h>
#include <ctype.h>
#include <string.h>
#include <iomanip>

using namespace std;

struct page {
       int tcolor;
       int bgcolor;
       char heading[50];
       char body[256];
       char title[50];
       char center;
       char bcenter;
       char bold;
       bool correct;
       } MyPage;
       
void getColors(page *ptrMyPage);
void getHeading(page *ptrMyPage);
void getTitle(page *ptrMyPage);
void getBody(page *ptrMyPage);
void writePage(page *ptrMyPage);
void verify(page *ptrMyPage);
ofstream output; //ate=put cursor at end of file, trunc=write over existing file data to start clean

int main(){
    int errorcode=0;
    MyPage.correct=true;
    SetConsoleTitleA("Basic Webpage Generation Application");
    cout<< "\xC9" << setfill('\xCD') << setw(54);
    cout<< "\xBB"<< endl;
    cout<<"\xBA Welcome to the Basic Webpage Generation Application \xBA"<< endl;
    cout<<"\xBA Created by Shawn Biddle (C) 2007                    \xBA"<< endl;
    cout<< "\xC8" << setfill('\xCD') << setw(54);
    cout<< "\xBC"<< endl;
    cout<<"\tPlease wait to be taken to the menu. . .";
    Sleep(5000);
    system("cls");
    getColors(&MyPage);
    system("cls");
    getTitle(&MyPage); 
    system("cls");
    getHeading(&MyPage);
    system("cls");
    getBody(&MyPage);   
    system("cls");
                                       
                         
    if (tolower(MyPage.center)!='y' && tolower(MyPage.center)!='n') {MyPage.correct=false; errorcode+=1;}
    if (tolower(MyPage.bcenter)!='y' && tolower(MyPage.bcenter)!='n'){MyPage.correct=false; errorcode+=2;}
    if (tolower(MyPage.bold)!='y' && tolower(MyPage.bold)!='n'){ MyPage.correct=false; errorcode+=3;}
    
    
    
    if (MyPage.correct!=false) {                           // correct value is written by the verify function
        verify(&MyPage);
        writePage(&MyPage);
        }
    else {
         cout<<"\aThe following Errors occured."<< endl;
         switch(errorcode){
                           case 1:
                                cout<<"  Error Code: 1 - Value input for Property (Center) not valid."<< endl;
                                break;
                           case 2:
                                cout<<"  Error Code: 2 - Value input for Property (Body Center) not valid."<< endl;
                                break;
                           case 3:
                                cout<<"  Error Code: 3 - Value input for Property (Bold) not valid."<< endl;
                                break;
                           case 4:
                                cout<<"  Error Code: 1 - Value input for Property (Center) not valid."<< endl;
                                cout<<"  Error Code: 3 - Value input for Property (Bold) not valid."<< endl;
                                break;
                           case 5:
                                cout<<"  Error Code: 2 - Value input for Property (Body Center) not valid."<< endl;
                                cout<<"  Error Code: 3 - Value input for Property (Bold) not valid."<< endl;
                                break;
                           case 6:
                                cout<<"  Error Code: 1 - Value input for Property (Center) not valid."<< endl;
                                cout<<"  Error Code: 2 - Value input for Property (Body Center) not valid."<< endl;
                                cout<<"  Error Code: 3 - Value input for Property (Bold) not valid."<< endl;
                                break;
                           default:
                                cout<<"  Error Code: 0 - Given input was not usable. File was not generated."<< endl;
                                break;
              }
         }
    if (output.is_open()){                          // if file is not closed by the writePage function
                          cout<<"File I/O Failed. Closing File."<< endl;
                          output.close();
                          }
    cout<<"Exiting. . ."<< endl;
    Sleep(5000);
    return 0;
}

void getTitle(page *ptrMyPage){
     cin.get();
     cout<<"What do you want the title of the webpage to be?";
     cin.getline(ptrMyPage->title, 50);
     }

void getHeading(page *ptrMyPage){
     
     cout<<"What would you like the heading to be?";
     cin.getline(ptrMyPage->heading, 50);
     cout<<"Do you want the heading to be centered? Y or N:";
     cin>> ptrMyPage->center;
}

void getBody(page *ptrMyPage){
     
     cout<<"What would you like the body text to be? Type # to end";
     cout<<"\nNote that HTML tags are accepted    Ex. Text <br />\n";
     cin.getline(ptrMyPage->body, 256, '#');
     cout<<"Do you want the text to be centered? Y or N:";
     cin>> ptrMyPage->bcenter;
     cout<<"\nWould you like the text to be bold? Y or N:";
     cin>> ptrMyPage->bold;
     }
     
void writePage(page *ptrMyPage){
        string filename;
        cout<<"What do you want the file to be called? .html is added automatically"<< endl;
        cout<<"Note that the file can be found in your My Documents folder."<< endl;
        cout<<"-->";
        cin >> filename;
        filename = filename + ".html";
        filename = "C:/DOCUME~1/ADMINI~1/MYDOCU~1/" + filename;        
        output.open(filename.c_str(), ios::ate | ios::trunc);
        cout<<"Creating page based on input. . ."<< endl;
        Sleep(1000);
        output << "<head>\n<title>"<< ptrMyPage->title << "</title>";
        /* HTML Colors
    0 Black 	4 Gray 	  8 Silver 	 12 White
	1 Yellow 	5 Lime 	  9 Aqua 	 13	Fuchsia
	2 Red 		6 Green  10	Blue 	 14	Purple
    3 Maroon 	7 Olive  11	Navy 	 15	Teal
    */
        switch(ptrMyPage->bgcolor){
                                               case 0:
                                                    output << "<body bgcolor=\"Black\">"<< endl;
                                                    break;
                                               case 1:
                                                    output << "<body bgcolor=\"Yellow\">"<< endl;
                                                    break;
                                               case 2:
                                                    output << "<body bgcolor=\"Red\">"<< endl;
                                                    break;
                                               case 3:
                                                    output << "<body bgcolor=\"Maroon\">"<< endl;
                                                    break;
                                               case 4:
                                                    output << "<body bgcolor=\"Gray\">"<< endl;
                                                    break;
                                               case 5:
                                                    output << "<body bgcolor=\"Lime\">"<< endl;
                                                    break;
                                               case 6:
                                                    output << "<body bgcolor=\"Green\">"<< endl;
                                                    break;
                                               case 7:
                                                    output << "<body bgcolor=\"Olive\">"<< endl;
                                                    break;
                                               case 8:
                                                    output << "<body bgcolor=\"Silver\">"<< endl;
                                                    break;     
                                               case 9:
                                                    output << "<body bgcolor=\"Aqua\">"<< endl;
                                                    break;       
                                               case 10:
                                                    output << "<body bgcolor=\"Blue\">"<< endl;
                                                    break;                 
                                               case 11:
                                                    output << "<body bgcolor=\"Navy\">"<< endl;
                                                    break;
                                               case 12:
                                                    output << "<body bgcolor=\"White\">"<< endl;
                                                    break;
                                               case 13:
                                                    output << "<body bgcolor=\"Fuschia\">"<< endl;
                                                    break;
                                               case 14:
                                                    output << "<body bgcolor=\"Purple\">"<< endl;
                                                    break;
                                               case 15:
                                                    output << "<body bgcolor=\"Teal\">"<< endl;
                                                    break;
                                               default:
                                                    output << "<body>"<< endl;
                                                    break;
                                                    }
        output << "<!-- Created with the WebWriter Application © 2007 -->" << endl;
        switch(ptrMyPage->tcolor){
                                               case 0:
                                                    output << "<font color=\"Black\">"<< endl;
                                                    break;
                                               case 1:
                                                    output << "<font color=\"Yellow\">"<< endl;
                                                    break;
                                               case 2:
                                                    output << "<font color=\"Red\">"<< endl;
                                                    break;
                                               case 3:
                                                    output << "<font color=\"Maroon\">"<< endl;
                                                    break;
                                               case 4:
                                                    output << "<font color=\"Gray\">"<< endl;
                                                    break;
                                               case 5:
                                                    output << "<font color=\"Lime\">"<< endl;
                                                    break;
                                               case 6:
                                                    output << "<font color=\"Green\">"<< endl;
                                                    break;
                                               case 7:
                                                    output << "<font color=\"Olive\">"<< endl;
                                                    break;
                                               case 8:
                                                    output << "<font color=\"Silver\">"<< endl;
                                                    break;     
                                               case 9:
                                                    output << "<font color=\"Aqua\">"<< endl;
                                                    break;       
                                               case 10:
                                                    output << "<font color=\"Blue\">"<< endl;
                                                    break;                 
                                               case 11:
                                                    output << "<font color=\"Navy\">"<< endl;
                                                    break;
                                               case 12:
                                                    output << "<font color=\"White\">"<< endl;
                                                    break;
                                               case 13:
                                                    output << "<font color=\"Fuschia\">"<< endl;
                                                    break;
                                               case 14:
                                                    output << "<font color=\"Purple\">"<< endl;
                                                    break;
                                               case 15:
                                                    output << "<font color=\"Teal\">"<< endl;
                                                    break;
                                               default:
                                                    break;
                                                    }      
        if (tolower(ptrMyPage->center)=='n'){
                                  output <<"<b><h2>" << ptrMyPage->heading << "</b></h2>"<< endl;
                                  }
        else if (tolower(ptrMyPage->center)=='y'){
                                  output <<"<b><center><h3>" << ptrMyPage->heading << "</b></h3></center>"<< endl;
                                  }
        output <<"<br />\n"; //HTML Linebreak
        if (tolower(ptrMyPage->bcenter)=='y' && tolower(ptrMyPage->bold)=='y'){
                                   output <<"<b><center>" << ptrMyPage->body << "\n</b></center>\n"<< endl;
                                   }
        else if (tolower(ptrMyPage->bcenter)=='y' && tolower(ptrMyPage->bold)=='n'){
                                   output <<"<center>" << ptrMyPage->body << "\n</center>\n"<< endl;
                                   }
        else if (tolower(ptrMyPage->bcenter)=='n' && tolower(ptrMyPage->bold)=='y'){
                                   output <<"<b>" << ptrMyPage->body << "\n</b>\n"<< endl;
                                   }
        else if (tolower(ptrMyPage->bcenter)=='n' && tolower(ptrMyPage->bold)=='n'){
                                   output << ptrMyPage->body <<  endl;
                                   }
        
        output << "    </font>\n  </body>\n</head>";
        output.close();
        cout<<"File was generated successfully."<< endl;
        Sleep(1000);
        cout<<"Please wait while I open the generated file. . ."<< endl;
        system(filename.c_str());
        
}

void verify(page *ptrMyPage){
     char H_Verify, B_Verify, T_Verify;
     
     cout<<"Are you sure these are the correct values for the page title?"<< endl;
     cout<<"\nTitle: "<< ptrMyPage->title << endl;
    
     cout<<"\nEnter Y for correct and N if these are incorrect:";
     cin>> T_Verify;
     
     if (tolower(T_Verify)=='n'){
                                 cout<<"\a\nIncorrect Values. Ending Execution."<< endl;
                                 Sleep(1000);
                                 ptrMyPage->correct=false;
                                 system("cls");
                                 return; }
     else {
                                 cout<<"\nValues Correct. Press Enter to verify Heading values. . .";
                                 cin.get();
                                 cin.get();
                                 system("cls");}
                                 
                                 
     cout<<"Are you sure these are the correct values for the heading?"<< endl;
     cout<<"\nHeading: "<< ptrMyPage->heading << endl;
     if (tolower(ptrMyPage->center)=='y') cout<<"\nCentered=TRUE"<< endl;
     else if (tolower(ptrMyPage->center)=='n') cout<<"\nCentered=FALSE"<< endl;
     
     cout<<"Enter Y for correct and N if these are incorrect:";
     cin>> H_Verify;
     
     if (tolower(H_Verify)=='n'){
                                 cout<<"\a\nIncorrect Values. Ending Execution."<< endl;
                                 ptrMyPage->correct=false;
                                 system("cls");
                                 return; }
     else {
                                 cout<<"\nValues Correct. Press Enter to verify Body Text Values. . .";
                                 cin.get();
                                 cin.get();
                                 system("cls");}
     
     cout<<"Are you sure these are the correct values for the body text?"<< endl;
     cout<<"Body Text: "<< ptrMyPage->body << endl;
    
     if (tolower(ptrMyPage->bcenter)=='y') cout<<"\nCentered=TRUE"<< endl;
     else if (tolower(ptrMyPage->bcenter)=='n') cout<<"\nCentered=FALSE"<< endl;
     if (tolower(ptrMyPage->bold)=='y') cout<<"Bold=TRUE"<< endl;
     else if (tolower(ptrMyPage->bold)=='n') cout<<"Bold=FALSE"<< endl;
     
     cout<<"Enter Y for correct and N if these are incorrect:";
     cin>> B_Verify;
     if (tolower(B_Verify)=='n'){
                                 cout<<"Incorrect Values. Ending Execution."<< endl;
                                 ptrMyPage->correct=false;
                                 system("cls");
                                 return; }
     else {
                                 cout<<"Values Correct. Proceeding to File Generation.";
                                 cout<<"\nPress ENTER to continue. . .";
                                 cin.get();
                                 cin.get();
                                 system("cls");
                                 return;}   
}

void getColors(page *ptrMyPage){
     char preview;
     cout<<"    0 Black 	4 Gray 	  8 Silver 	 12 White"<< endl;
	 cout<<"    1 Yellow 	5 Lime 	  9 Aqua 	 13 Fuchsia"<< endl;
	 cout<<"    2 Red 	6 Green  10 Blue 	 14 Purple"<< endl;
     cout<<"    3 Maroon 	7-Olive  11 Navy 	 15 Teal"<< endl;
     cout<<"Colors with a - have no preview."<< endl;
     cout<<"Please type the number of the corresponding text color: ";
     cin>> ptrMyPage->tcolor;
     cout<<"\nPlease type the number of the corresponding background color. "<< endl;
     cout<<"TIP: Do not use the same background and text color.\n>";
     cin>> ptrMyPage->bgcolor;
     cout<<"If you would like a preview type P or anything else to continue on."<< endl;
     cin >> preview;
     if (tolower(preview)=='p'){
                                cout<<"Text Color Preview, Wait for Background Color Preview."<< endl;
                                switch(ptrMyPage->tcolor){
                                               case 0:
                                                    cout<<"Cannot Preview Black on Black"<< endl;
                                                    break;
                                               case 1:
                                                    system("color E");
                                                    break;
                                               case 2:
                                                    system("color C");
                                                    break;
                                               case 3:
                                                    system("color 4");
                                                    break;
                                               case 4:
                                                    cout<<"Current Color"<< endl;
                                                    break;
                                               case 5:
                                                    system("color A");
                                                    break;
                                               case 6:
                                                    system("color 2");
                                                    break;
                                               case 7:
                                                    cout<<"No Preview"<< endl;
                                                    break;
                                               case 8:
                                                    system("color 8");
                                                    break;     
                                               case 9:
                                                    system("color 3");
                                                    break;       
                                               case 10:
                                                    system("color 9");
                                                    break;                 
                                               case 11:
                                                    system("color 1");
                                                    break;
                                               case 12:
                                                    system("color F");
                                                    break;
                                               case 13:
                                                    system("color D");
                                                    break;
                                               case 14:
                                                    system("color 5");
                                                    break;
                                               case 15:
                                                    system("color B");
                                                    break;
                                               default:
                                                    cout<<"No Preview Available"<< endl;
                                                    }
                                Sleep(3000);
                                cout<<"Background Color Preview."<< endl;
                                switch(ptrMyPage->bgcolor){
                                               case 0:
                                                    cout<<"Current Background"<< endl;
                                                    break;
                                               case 1:
                                                    system("color E7");
                                                    break;
                                               case 2:
                                                    system("color C7");
                                                    break;
                                               case 3:
                                                    system("color 47");
                                                    break;
                                               case 4:
                                                    system("color 87");
                                                    break;
                                               case 5:
                                                    system("color A7");
                                                    break;
                                               case 6:
                                                    system("color 27");
                                                    break;
                                               case 7:
                                                    cout<<"No Preview"<< endl;
                                                    break;
                                               case 8:
                                                    system("color 87");
                                                    break;     
                                               case 9:
                                                    system("color 37");
                                                    break;       
                                               case 10:
                                                    system("color 97");
                                                    break;                 
                                               case 11:
                                                    system("color 17");
                                                    break;
                                               case 12:
                                                    system("color F7");
                                                    break;
                                               case 13:
                                                    system("color D7");
                                                    break;
                                               case 14:
                                                    system("color 57");
                                                    break;
                                               case 15:
                                                    system("color B7");
                                                    break;
                                               default:
                                                    cout<<"No Preview Available"<< endl;
                                                    }
                                Sleep(3000);
                                cout<<"Ending Preview. Returning to Default Colors."<< endl;
                                system("color 07");
                                cout<<"Please Wait. Proceeding with File Generation."<< endl;
                                Sleep(2000);
                                return;
                                }              
                                
     else {
          cout<<"Please Wait. Proceeding with File Generation."<< endl;
          Sleep(2000);
          return;
          }
}