var postSubject = '';
var postService = '';
var subject = '\
SE:AA:Accounting|\
SE:AB:Advertising|\
SE:AC:African-American Studies|\
SE:AD:Aging|\
SE:AE:Agricultural Studies|\
SE:AF:Anthropology|\
SE:AG:Archaeology|\
SE:AH:Architecture|\
SE:AI:Arts|\
SE:AJ:Asian Studies|\
SE:AK:Astronomy|\
SE:AL:Aviation|\
SE:AM:Aeronautics|\
SE:BA:Biology|\
SE:BB:Business|\
SE:CA:Canadian Studies|\
SE:CB:Chemistry|\
SE:CC:Child|\
SE:CD:Youth Issues|\
SE:CE:Communication|\
SE:CF:Computer and Technology|\
SE:CG:Criminology|\
SE:DA:Dance|\
SE:DB:Drama and Theater|\
SE:EA:East European Studies|\
SE:EB:Economics|\
SE:EC:Education|\
SE:ED:Electronics|\
SE:EE:Engineering|\
SE:EF:English|\
SE:EG:Environmental Studies|\
SE:EH:Ethics|\
SE:EI:Ethnic Studies|\
SE:FA:Film|\
SE:GA:Gender and Sexuality|\
SE:GB:Geography|\
SE:GC:Geology and Geophysics|\
SE:HA:History|\
SE:HB:Holocaust Studies|\
SE:HC:Hot Topics|\
SE:IA:International Relations|\
SE:LA:Labor Studies|\
SE:LB:Language|\
SE:LC:Latin-American Studies|\
SE:LD:Law|\
SE:LE:Linguistics|\
SE:LF:Literature|\
SE:MA:Mathematics|\
SE:MB:Medical and Health|\
SE:MC:Music Studies|\
SE:NA:Native-American Studies|\
SE:NB:Nutrition|\
SE:PA:Philosophy|\
SE:PB:Physics|\
SE:PC:Political Science|\
SE:PD:Psychology|\
SE:PE:Public Administration|\
SE:RA:Religion and Theology|\
SE:RB:Research Designs|\
SE:SA:Shakespeare|\
SE:SB:Sociology|\
SE:SC:Sport|\
SE:TA:Tourism|\
SE:WA:Women Studies|\
SE:OT:Other                                                  .|\
PS:AA:Actuarial Science|\
PS:AB:Advance Math|\
PS:AC:Algebra|\
PS:AD:Analytic Geometry|\
PS:CA:Chemistry|\
PS:CB:Civil Engineering|\
PS:DA:Differential Calculus|\
PS:DB:Differential Equations|\
PS:EA:Electrical Engineering|\
PS:EB:Electronics|\
PS:EC:Engineering Economics|\
PS:ED:Engineering Mechanics|\
PS:FA:Financial Accounting|\
PS:IA:Industrial Engineering|\
PS:IB:Integral Calculus|\
PS:MA:Management Science|\
PS:MB:Managerial Accounting|\
PS:MC:Mathematical Economics|\
PS:MD:Mathematical Statistics|\
PS:ME:Mechanical Engineering|\
PS:OA:Operations Management|\
PS:PA:Physics|\
PS:PB:Plane and Spherical Trigo|\
PS:PC:Plane Geometry|\
PS:SA:Solid Geometry|\
PS:SB:Strategic Management|\
PS:SC:Strength of Materials|\
PS:OT:Other                                                  .|\
CP:AA:ASP|\
CP:CA:C|\
CP:CB:C++|\
CP:CC:C#|\
CP:CD:Cobol|\
CP:CE:CSS|\
CP:HA:HTML|\
CP:JA:Java|\
CP:JB:Javascript|\
CP:MA:Matlab|\
CP:MB:Microsoft Access|\
CP:PA:PHP|\
CP:SA:SQL|\
CP:UA:Unix|\
CP:VA:Verilog|\
CP:VB:VHDL|\
CP:VC:Visual Basic|\
CP:XA:XML|\
CP:XB:.Net|\
CP:OT:Other                                                  .|\
LI:AA:Arabic|\
LI:CA:Chinese|\
LI:FA:French|\
LI:GA:German|\
LI:JA:Japanese|\
LI:KA:Korean|\
LI:MA:Malay|\
LI:SA:Spanish|\
LI:OT:Other                                                  .|\
DS:AA:Arts and Sciences|\
DS:MA:Mathematics|\
DS:EA:Engineering|\
DS:OT:Other                                                  .|\
RP:BA:Books|\
RP:EA:Essays|\
RP:MA:Magazines|\
RP:MB:Math Solutions|\
RP:PA:Programming Solutions|\
RP:OT:Other                                                  .|\
PE:BA:Books|\
PE:EA:Essays|\
PE:MA:Magazines|\
PE:MB:Math Solutions|\
PE:PA:Programming Solutions|\
PE:OT:Other                                                  .|\
';

var service = '\
SE:Standard Essay|\
DS:Dissertation/Thesis (30 days)|\
PS:Problem Solving|\
CP:Computer Programming|\
LI:Language Writing Interpretation|\
RP:Revision and Proofreading|\
PE:Plain Encoding                                      .|\
';

function TrimString(sInString) {
  if ( sInString ) {
     sInString = sInString.replace( /^\s+/g, "" );
     return sInString.replace( /\s+$/g, "" );
  }
}

function populateService(defaultService) {
  if ( postService != '' ) {
     defaultService = postService;
  }
  var serviceLineArray = service.split('|');
  var optionCtr = 0;
  var selObj = document.getElementById('serviceSelect');
 // selObj.options[0] = new Option('---------------- Please Select ---------------','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < serviceLineArray.length - 1; loop++) {
     lineArray = serviceLineArray[loop].split(':');
     serviceCode = TrimString(lineArray[0]);
     serviceName = TrimString(lineArray[1]);
     if ( serviceCode != '') {
        selObj.options[optionCtr] = new Option(serviceName, serviceCode);
     }
     if ( defaultService == serviceCode ) {
        selObj.selectedIndex = optionCtr;
     }
	 optionCtr++
   }
}

function populateSubject() {
  var selObj = document.getElementById('subjectSelect');
  var foundSubject = false;
  if ( selObj.type == 'select-one') {
     for (var i = 0; i < selObj.options.length; i++) {
         selObj.options[i] = null;
     }
     selObj.options.length=null;
//     selObj.options[0] = new Option('---------------- Please Select ---------------','');
     selObj.selectedIndex = 0;
  }
  var subjectLineArray = subject.split("|");
  var optionCntr = 0;
  for (var loop = 0; loop < subjectLineArray.length; loop++) {
     lineArray = subjectLineArray[loop].split(":");
     serviceCode = TrimString(lineArray[0]);
     subjectCode = TrimString(lineArray[1]);
     subjectName = TrimString(lineArray[2]);
  if (document.getElementById('serviceSelect').value == serviceCode && serviceCode != '') {
     if (selObj.type == 'text') {
        parentObj = document.getElementById('subjectSelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","subject");
        inputSel.setAttribute("id","subjectSelect");
        parentObj.appendChild(inputSel);
        selObj = document.getElementById('subjectSelect');
        selObj.options[0] = new Option('---------------- Please Select ---------------','');
        selObj.selectedIndex = 0;
     }
     if ( subjectCode != '') {
        selObj.options[optionCntr] = new Option(subjectName, subjectCode);
     }
     if ( subjectCode == postSubject && serviceCode == postService ) {
        selObj.selectedIndex = optionCntr;
     }
     foundSubject = true;
     optionCntr++
     }
   }
	var serviceSel = document.forms['orderform'].elements['serviceSelect'].value;
	price = new Array(9);
	if (serviceSel == 'SE') {
		price[0] = new Array(6.99, 8.99, 12.99, 15.99, 18.99, 23.99, 28.99, 34.99);
	}
	else if (serviceSel == 'LI') {
		price[0] = new Array(6.99, 8.99, 12.99, 15.99, 18.99, 23.99, 28.99, 34.99);
	}
	else if (serviceSel == 'DS') {
		document.forms['orderform'].elements['deliveryMode'].selectedIndex = 0;
		document.forms['orderform'].elements['numberPagesItem'].selectedIndex = 28;
		document.forms['orderform'].elements['totalAmount'].value = '869.70';
    	document.forms['orderform'].elements['costPerPageItem'].value = '28.99';   
	//	price[0] = new Array(6.99, 8.99, 12.99, 15.99, 18.99, 23.99, 28.99, 34.99);
	}
	else if (serviceSel == 'PS') {
		price[0] = new Array(8.99,10.99, 14.99, 17.99, 20.99, 25.99, 30.99, 36.99);
	}
	else if (serviceSel == 'CP') {
		price[0] = new Array(8.99,10.99, 14.99, 17.99, 20.99, 25.99, 30.99, 36.99);
	}
	else if (serviceSel == 'RP') {
		price[0] = new Array(4.99, 6.99, 10.99, 13.99, 16.99, 21.99, 26.99, 32.99);
	}
	else if (serviceSel == 'PE') {
		price[0] = new Array(2.99, 4.99, 6.99, 8.99, 9.99, 12.99, 14.99, 17.99);
	}	
    if (serviceSel != 'DS'){
		per_page = price[0][document.forms['orderform'].elements['deliveryMode'].value - 1];
		sum = Math.round(per_page*document.forms['orderform'].elements['numberPagesItem'].value*100)/100;
		document.forms['orderform'].elements['totalAmount'].value = sum;
		document.forms['orderform'].elements['costPerPageItem'].value = per_page;   
	}

}

function initService(service) {
   populateService(service);
   populateSubject();
}
