//to open new popup window
function openWindow(page,width,height) {
	windowFeatures = "";
	window_width = width;
	window_height = height;
	window_top = (screen.availHeight-window_height)/2;
	window_left = (screen.availWidth-window_width)/2;
	windowFeatures += "width=" + window_width + ",height=" + window_height + ",top=";
	windowFeatures += window_top;
	windowFeatures += ",left=";
	windowFeatures += window_left;
	windowFeatures += ',status=1,scrollbars=1';

	window.open(page,"mywindow", windowFeatures);
}

//to open new popup window without setting width and height
function openWindownoWH(page) {
        windowFeatures = "";
        window_width = 400;
        window_height = 250;
        window_top = (screen.availHeight-window_height)/2;
        window_left = (screen.availWidth-window_width)/2;
        windowFeatures += "width=" + window_width + ",height=" + window_height + ",top=";
        windowFeatures += window_top;
        windowFeatures += ",left=";
        windowFeatures += window_left;
        windowFeatures += ',status=1,scrollbars=1';

        window.open(page,"mywindow", windowFeatures);
}
function valShoppingSearch(doc){
	var model_year = doc.model_year.options[doc.model_year.selectedIndex].value;
	var vehicle_line = doc.vehicle_line.options[doc.vehicle_line.selectedIndex].value;
	var error_string = "Search was not completed due to the following reasons\n";
	var form_error = true;

	if(model_year == ""){
		error_string += "---You did not select the model year\n";
		form_error = false;
	}
	if(vehicle_line == ""){
                error_string += "---You did not select the vehicle line\n";
		form_error = false;
        }
	if(form_error == false){
		error_string += "Please check your entry and try again\n";
		alert(error_string);
	}
	return form_error;
}
//#########################################
//function for updating the cart
//########################################
function updateCart()
{
	if(allQuantitiesOk())
	{
		submitFormValue('updateCart.jsp','cart');	
	}
	else
	{
		alert('Enter a valid quantity.\nPlease avoid commas, decimals & spaces.');
	}
}

//##############################################
//Function for checkout the cart
//###############################################
function checkoutCart()
{
	if(allQuantitiesOk())
	{
          submitFormValue('ShippingDetails.jsp','cart');
	}
	else
	{
		alert('Enter a valid quantity.\nPlease avoid commas, decimals & spaces.');
	}

}
//###########################################################
//Function for checking the valid quantities in Shopping cart
//###########################################################
function allQuantitiesOk()
{
	var i;
	for(i = 0; i < document.cart.item_qty.length; i++)
	{
		if(!isNumericsOnly(document.cart.item_qty[i].value))
		{
			document.cart.item_qty[i].focus();
			document.cart.item_qty[i].select();
			break;
		}
	}
	if(i < document.cart.item_qty.length)
	{
		return false;
	}
	else
	{
		return true;
	}
}
//**********************************************************************************
// Check whether the provided data contain only numeric values.
//**********************************************************************************
function isNumericsOnly(string)
{
	var i;
	for(i = 0; i < string.length; i++)
	{
		if(!(string.charAt(i) >= '0' && string.charAt(i) <= '9'))
		{
			return false;
		}
	}
	return true;
}
//#################################################
//for submitting the form general function
//#################################################
function submitFormValue(actionValue,formName){
	  if(actionValue != ""){
		  eval('document.'+ formName +'.action="'+actionValue+'"');
	  }
        eval('document.'+ formName +'.submit()');
}

//#################################################
//for submitting the form with values
//#################################################
function submitFormShopping(actionValue,formName, elementName){
	  if(actionValue != ""){
   	       eval('document.'+ formName +'.action="'+actionValue+'"');
        	  if(eval('document.'+ formName +'.' + elementName + '.value !=""')){
        	          eval('document.'+ formName +'.submit()');
        	  }
           }
}

//#################################################
//for delete confirm 
//#################################################

function confirmDelete()
{
	var agree=confirm("Are you sure you want to delete this document?");
	if(agree){
		return true ;
	}
	else
	{
		return false ;
	}

}

//#################################################
//for delete confirm of addresses
//#################################################

function confirmDeleteItem(type)
{
	var agree=confirm("Are you sure you want to delete this " + type + "?");
	if(agree){
		return true ;
	}
	else
	{
		return false ;
	}

}
//##############################################
//to validate the subc admin page
//##############################################
function valSubcAdmin(doc){
        var error_string = "Subscription details was not updated due to the following reasons\n";
        var form_error = true;

        if(doc.sub_days[0].value == "" || doc.sub_days[1].value == "" || doc.sub_days[2].value == ""){
                error_string += "---You did not select the subscription days\n";
                form_error = false;
        }

        if(doc.sub_price[0].value == "" || doc.sub_price[1].value == "" || doc.sub_price[2].value == ""){
                error_string += "---You did not select the subscription price\n";
                form_error = false;
        }

        if(form_error == false){
                error_string += "Please check your entry and try again\n";
                alert(error_string);
        }
        return form_error;
}
//#####################################
//to validate the vehicle master screen
//#####################################
function valVehMaster(doc){
	var error_string = "Vehicle Master was not updated due to the following reasons\n";
        var form_error = true;
	
	if(doc.model_year.value == ""){
		error_string += "---You did not select the model year\n";
		form_error = false;
	}
	if(doc.vehicle_line_exi.options[doc.vehicle_line_exi.selectedIndex].value == "" && doc.vehicle_line_new.value == ""){
                error_string += "---You did not select the vehicle line\n";
		form_error = false;
        }
	if(form_error == false){
                error_string += "Please check your entry and try again\n";
                alert(error_string);
        }
        return form_error;
}


//#####################################
//to validate the vehicle details screen
//#####################################
function valVehDetails(doc){
        var error_string = "Vehicle Details was not updated due to the following reasons\n";
        var form_error = true;

        if(doc.engine_size.value == ""){
                error_string += "---You did not select the engine size\n";
                form_error = false;
        }
        if(doc.transmission.value == ""){
                error_string += "---You did not select the transmission\n";
                form_error = false;
        }
        if(doc.drive_train.value == ""){
                error_string += "---You did not select the drive train\n";
                form_error = false;
        }
        if(form_error == false){
                error_string += "Please check your entry and try again\n";
                alert(error_string);
        }
        return form_error;
}


//#####################################
//to validate publication type
//#####################################
function chkPub(menuelement){

        if(!isDataEntered("pub_search","publication_type","Publication Type")){
                return false;
        }        


//if (menuelement ==1 || menuelement ==2 || menuelement ==3 || menuelement ==4 || menuelement ==8|| menuelement ==9 || menuelement ==10) {
if (menuelement ==1 || menuelement ==2 || menuelement ==10) {
	        if(!isDataEntered("pub_search","model_year","Model Year")){
	             return false;
	        }
                if(!isDataEntered("pub_search","vehicle_line","Vehicle Line")){
                     return false;
                }
	}
/*
        if (menuelement ==3  || menuelement ==4 || menuelement ==8 || menuelement ==9) {
                if(!isDataEntered("pub_search","transmission","Transmission")){
                     return false;
                }
                if(!isDataEntered("pub_search","engine_size","Engine Size")){
                     return false;
                }
                if(!isDataEntered("pub_search","drive_train","Drive Train")){
                     return false;
                }
        }
*/
/*
        if (menuelement ==5) {
                if(!isDataEntered("pub_search","calendar_year","Calender Year")){
                     return false;
                }
                if(!isDataEntered("pub_search","calendar_month","Calender Month")){
                     return false;
                }
        }
*/

        return true;
}

//#####################################
//to validate online shopping search
//#####################################
function chkOnlineShopping(menuelement){

        if (menuelement ==1 || menuelement ==2 || menuelement ==3 || menuelement ==4 || menuelement ==8|| menuelement ==9 ||menuelement ==15) {
                if(!isDataEntered("shop_search","model_year_shopping","Model Year")){
                     return false;
                }
                if(!isDataEntered("shop_search","vehicle_line_shopping","Vehicle Line")){
                     return false;
                }
        }

        if (menuelement ==3  || menuelement ==4 || menuelement ==8 || menuelement ==9 || menuelement ==15) {
                if(!isDataEntered("shop_search","transmission_shopping","Transmission")){
                     return false;
                }
                if(!isDataEntered("shop_search","engine_size_shopping","Engine Size")){
                     return false;
                }
	}                
	if (menuelement ==3  || menuelement ==4 || menuelement ==8 || menuelement ==9) {
                if(!isDataEntered("shop_search","drive_train_shopping","Drive Train")){
                     return false;
                }
        }

        if (menuelement ==5) {
                if(!isDataEntered("shop_search","calendar_year_shopping","Calender Year")){
                     return false;
                }
                if(!isDataEntered("shop_search","calendar_month_shopping","Calender Month")){
                     return false;
                }
        }

        return true;
}

//#####################################
//to validate vechicle ID for search
//#####################################
function chkVIN(){
        if(!isDataEntered("vin_search","vin","Vehicle ID (VIN)")){
                return false;
        }
        return true;
}

//#####################################
//to validate Literature # for search
//#####################################
function chkLIT(){
        if(!isDataEntered("lit_search","lit_no","Literature #")){
                return false;
        }
        return true;
}

//**********************************************************************************
// Check whether the field is empty or not.
// Accepts the form name, field name and display name.
//**********************************************************************************
function isDataEntered(formname, fieldname, displayname) {

	 if (eval("document." + formname + "." + fieldname + ".type")== "text") {
             itemvalue = eval("document." + formname + "." + fieldname + ".value");
             if(itemvalue.length > 0) {
                return true;
           } else {
                alert("Please enter " + displayname + "");
                eval("document." + formname + "." + fieldname + ".focus()");
                return false;
           }
       }

         if (eval("document." + formname + "." + fieldname + ".type")== "select-one") {
              formElementChoice = eval("document." + formname + "." + fieldname + ".selectedIndex");
              if (formElementChoice != 0)
              {
		  return true;
              }  else {
                   alert("Please select " + displayname + "");
                   eval("document." + formname + "." + fieldname + ".focus()");
                   return false;
	      }
       }
}

//################################################################
//to check to make sure you selected an item for the shopping cart
//################################################################
function AddIt(form)
{
		var count;
		var emptyset = 0;
		var formOk = true;
		
		count = eval(form.lit_nbr.length);
				
		if(count == null)
		{
			emptyset = form.lit_nbr.checked ? 1 : 0;
			
			//check to see if not selected
			if(emptyset == 0)
			{
				alert("Please select an item to add to your cart");
				formOk = false;
			}
		}
		else if(count > 0)
		{
			for(i=0;i<count;++i)
			{ 
		  	    if(eval("form.lit_nbr[" + i + "].checked"))
			    {
					++emptyset;
			     }
			}
			
			if(emptyset == 0)
			{
				alert("Please select an item to add to your cart!");
				formOk = false;
			}
		}
		else
		{
			//no classes available
			alert("No products are available.\nPlease select another search criteria!");
			formOk = false;
		}	
		
		//return 
		return formOk;	
}

//#####################################
//to validate the add shopping item 
//#####################################
function addShopDetails(doc){
        var error_string = "Shopping material was not updated due to the following reasons\n";
        var form_error = true;

	if(doc.pub_type.options[doc.pub_type.selectedIndex].value == ""){
                error_string += "---You did not select the publication type\n";
                form_error = false;
        }
        if(doc.itn.value == ""){
                error_string += "---You did not enter the item name\n";
                form_error = false;
        }
        if(doc.pup.value == ""){
                error_string += "---You did not enter the public price\n";
                form_error = false;
        }
        if(doc.dlp.value == ""){
                error_string += "---You did not enter the dealer price\n";
                form_error = false;
        }
	if(doc.msr.value == ""){
                error_string += "---You did not enter the MSRP\n";
                form_error = false;
        }
	if(doc.fop.value == ""){
                error_string += "---You did not enter the FOB price\n";
                form_error = false;
        }
	if(doc.dip.value == ""){
                error_string += "---You did not enter the Distributor Price\n";
                form_error = false;
        }	
        if(form_error == false){
                error_string += "Please check your entry and try again\n";
                alert(error_string);
        }
        return form_error;
}

