/*
*****************************************************************************
**     This JavaScript code is developed by
**     Mostafa Sobhi Amin, ms.amin@gmail.com
**     Blog: http://adjuster.blogspot.com
**     Website: http://adjuster.somee.com
*****************************************************************************
*/

// Globals
var d = document;
//////////////////////////////////////////////////////////////////////////////
//determine which form i'm clicking..
//checking for empty fields..
function validateForm( formName , action , isFeatured , loadImgId ) {
    var error = false;
    par = d.getElementById(formName);
	
    if( isFeatured ){//submitting from featured cars form
        for( i=0 ; i < par.length-3 ; i++ ){
            if ( par.elements[i].value == "" ) {
                error = true;
            }
        }
    }else{//submitting from sell cars form
	
        //manual processing because the client requirements is endless here, so manual processing is the best solution..
        //processing country, city, make, model, year, price
        /*for( i=0 ; i <= 5 ; i++ ){
			alert(par.elements[i].value);
            if( par.elements[i].value == "" || par.elements[i].value == 0 ){
                error = true;
                //exit for
                break;
                //alert(par.elements[i].name + " is empty..");
            }
        }*/
		//processing...
		country = par.elements['sellcountry'];
		city = par.elements['selldistrict'];
		make = par.elements['sellmake'];
		make_other = par.elements['sell_make_otherbody'];
		model = par.elements['sellmodel'];
		model_other = par.elements['sell_model_otherbody'];
		year = par.elements['year'];
		price = par.elements['price'];
		
		if( country.value == "" || city.value == "" || year.value == "" || price.value == "" ) {
            error = true;
        }
		
		if( make.value == 0 ){ //if drop-down is set to zero
            error = true;
        }else if( make.value == "other" ){ //if drop-down is set to other
            if( make_other.value == "" ) { //if the hidden textfield is empty
                error = true;
            }
        }
		if( model.value == 0 ){ //if drop-down is set to zero
            error = true;
        }else if( model.value == "other" ){ //if drop-down is set to other
            if( model_other.value == "" ) { //if the hidden textfield is empty
                error = true;
            }
        }
		
        //processing currency
        sc = par.elements['sellcurrency'];
        sco = par.elements['sellcurrency_other'];
				
        if( sc.value == 0 ){ //if drop-down is set to zero
            error = true;
        }else if( sc.value == "other" ){ //if drop-down is set to other
            if( sco.value == "" ) { //if the hidden textfield is empty
                error = true;
            }
        }
		
        //processing car body
        cb = par.elements['carbody'];
        sob = par.elements['sell_otherbody'];
		
        if( cb.value == 0 ){ //if drop-down is set to zero
            error = true;
        }else if( cb.value == "other" ){ //if drop-down is set to other
            if( sob.value == "" ) {//if the hidden textfield is empty
                error = true;
            }
        }
		
        //color, image field
        clr = par.elements['sellcolor'];
        //img_fld = par.elements['sellimage'];// made optional, so, remove the check on it
		
        //if( clr.value == "" && img_fld.value == "" ) {
        if( clr.value == "" ) {
            error = true;
        }
		
        //contact
        phone = par.elements['phone'];
        mobile = par.elements['mobile'];
        email = par.elements['email'];
		
        if( phone.value == "" && mobile.value == "" && email.value == "" ){
            error = true;
        }
		
        //prefered
        prfrd = par.elements['prefered'];
		
        if( prfrd.value == 0 ) {
            error = true;
        }
    }
    redirectForm( formName , action , loadImgId , error );
}
//////////////////////////////////////////////////////////////////////////////
//continue working on data returned from validateForm()
//alert or concat variables values and fire ajax and submit forms
function redirectForm( frmName , aAction , lLoadImgId , eError ){
    par = d.getElementById(frmName);
    msg = 'Please fill all the required fields.';
	
    if ( eError ){ //if error = true
        if( aAction ){ //if coming from [featured cars] or [all cars] or [search] pages
            d.getElementById("error_featured" + lLoadImgId).innerHTML = msg;
            d.getElementById("error_featured" + lLoadImgId).style.display = 'block';
        }else{ //coming from sell form only
            d.getElementById("error").className = 'error';
            d.getElementById("error").innerHTML = msg;
            d.getElementById("error").style.display = 'block';
            //alert("Please fill all the fields marked with *");
        }
    }
    else{
        //concat form values
        var fValues = '';
        if( aAction ){//submitting from featured cars form
            for ( j=0 ; j < par.length-1 ; j++ ){
                fValues += '&' + par.elements[j].name + '=' + par.elements[j].value;
            }
            //alert(fValues);
            //disable buttons..dynamic featured form btns
            var btnID = par.elements[par.length-1].name;
            //i may use ["error_featured" + lLoadImgId] instead
            //of the next line to get the dynamic id
            var carID = par.elements["car_id"].value;
            ena_dis( btnID , true , 'Sending Data' , 'loading'+lLoadImgId , 'block' );
            d.getElementById("error_featured"+carID).style.display = "none";
            //fire ajax function
            sendReq( aAction , fValues , btnID , lLoadImgId );
			
        //}else if( frmName == 'login' ){
        //	par.submit();
        }else{//submitting from sell cars form
            d.getElementById("error").style.display = 'none';
            ena_dis( 'send' , true , 'Sending Data' , 'loading_sell' , 'block' );
            par.submit();
        }
    }
}
//////////////////////////////////////////////////////////////////////////////
//disable btn, change btn value, show loading img
function ena_dis( btnID , btnState , btnText , imgID , imgStyle , p ){
    var p = window;
    if(p) p = p.parent; //target correction, if coming from iFrame
    p.d.getElementById(btnID).disabled = btnState;
    p.d.getElementById(btnID).value = btnText;
    p.d.getElementById(imgID).style.display = imgStyle;
}
//////////////////////////////////////////////////////////////////////////////
//show the hidden textbox when we select other from the drop down menu homepage
function openOther( selectName , sItem ){
    var res = d.getElementById(selectName);
    if( res.options[res.selectedIndex].value == "other" ) 
        d.getElementById(sItem).style.display = 'inline';
    else
        d.getElementById(sItem).style.display = 'none';
}
//////////////////////////////////////////////////////////////////////////////
function CA( isO , noHL ){
  for (var i=0 ; i < d.getElementById('frm').elements.length ; i++){
    var e = d.getElementById('frm').elements[i];
    if ((e.name != 'allbox') && (e.type=='checkbox')){
      if (isO != 1){
        e.checked = d.getElementById('frm').allbox.checked;
        if (d.getElementById('frm').allbox.checked){
          if (!noHL)
            hL(e);
        }
        else{
          if (!noHL)
            dL(e);
        }
      }
      else{
        if (!noHL){
          if (e.checked)
            hL(e);
          else
            dL(e);
        }
      }
    }
  }
}
//********************************************************************************
//for only one checkbox
function CCA( CB , noHL ){
  if (!noHL){
    if (CB.checked)
      hL(CB);
    else
      dL(CB);
  }
  var TB = TO = 0;
  for (var i=0 ; i < d.getElementById('frm').elements.length ; i++){
    var e = d.getElementById('frm').elements[i];
    if ((e.name != 'allbox') && (e.type=='checkbox')){
      TB++;
      if (e.checked)
        TO++;
    }
  }
  d.getElementById('frm').allbox.checked = ( TO == TB )?true:false;
}
//***************************************************************
//go up..if parent element != TR -> go up.. to select the entire TR to color it with the class or remove it..
function hL(E){
  while (E.tagName != "TR"){
    E = E.parentNode;
  }
  E.className = "H";
}
//***************************************************************
function dL(E){
  while (E.tagName != "TR"){
    E = E.parentNode;
  }
  E.className = "";
}
//***************************************************************
//used in cp > featured
function viewDetails(x){
    if(d.getElementById('carbox'+x).style.display == "block")
        d.getElementById('carbox'+x).style.display = "none";
    else if(d.getElementById('carbox'+x).style.display == "none")
        d.getElementById('carbox'+x).style.display = "block";
}
//***************************************************************
//change the URL on [all cars view] & [search cars view]
//to dont submit each time we click on the link
function changeURL( linkID , show_or_hide ){
    if( show_or_hide == 0 ){
        d.getElementById('link'+linkID+'a').style.display = 'inline';
        d.getElementById('link'+linkID).style.display = 'none';
    }else if( show_or_hide == 1 ){
        d.getElementById('link'+linkID+'a').style.display = 'none';
        d.getElementById('link'+linkID).style.display = 'inline';
    }
}
//***************************************************************
//used in cp > featured
function cTR(id){
    d.getElementById(id).className = "H";
}
//***************************************************************
function createRequestObject() {
    var req;
    if(window.XMLHttpRequest){
        // Firefox, Safari, Opera...
        req = new XMLHttpRequest();
    } else if(window.ActiveXObject) {
        // Internet Explorer 5+
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        // There is an error creating the object,
        // just as an old browser is being used.
        alert('Problem creating the XMLHttpRequest object');
    }
    return req;
}

// Make the XMLHttpRequest object
var http = createRequestObject();
//***************************************************************
function sendReq( a , fv , btnID , loadImgId ) {
    // Open PHP script for requests
    if( a == 'buy_featured' ){
        http.open('get', 'uc_buy.php?action=' + a + fv + '&btnid=' + btnID + '&loadingimg=' + loadImgId);
        http.onreadystatechange = handleBuyFeaturedResponse;
    }
    else if( a == 'load_img' ){
        http.open('get', 'get_image.php?action=' + a + '&id=' + fv );
        http.onreadystatechange = handleLoadImgResponse;
    }
	else if( a == 'update_models' ){//alert(d.getElementById('makes').value);
        http.open('get', 'ctrl_panel_process2.php?action=' + a + '&val=' + fv );
        http.onreadystatechange = handleUpdateModelsResponse;
    }
	else if( a == 'update_models1' ){//alert(d.getElementById('makes').value);
        http.open('get', 'ctrl_panel_process2.php?action=' + a + '&val=' + fv );
        http.onreadystatechange = handleUpdateModelsResponse1;
    }
	else if( a == 'update_models2' ){//alert(d.getElementById('makes').value);
        http.open('get', 'ctrl_panel_process2.php?action=' + a + '&val=' + fv );
        http.onreadystatechange = handleUpdateModelsResponse2;
    }
	//remove
	/*else if( a == 'um' ){//alert(d.getElementById('makes').value);
        http.open('get', '_cc.php?k=' + fv );
        http.onreadystatechange = handleUpdateModelsResponse_a;
    }*/
    http.send(null);
}
//***************************************************************
function handleBuyFeaturedResponse() {
    if(http.readyState == 4 && http.status == 200){
        // Text returned FROM the PHP script
        var response = http.responseText;
        //alert(response);
        if(response) {
            var mResponse = response.split("|");
            //update content, reverse the send effect
            ena_dis( mResponse[0] , false , 'Send to seller' , 'loading'+mResponse[1] , 'none' );
            d.getElementById("error_featured"+mResponse[1]).style.display = "block";
            d.getElementById("error_featured"+mResponse[1]).innerHTML = mResponse[2];
        }
    }
}
//***************************************************************
function handleLoadImgResponse() {
    if(http.readyState == 4 && http.status == 200){
        var response = http.responseText; //img.gif|1 (id)
        //alert(response);
        if(response) {
            var mResponse = response.split("|");
            //d.getElementById("dyn"+mResponse[1]).src = mResponse[0];
            d.getElementById("dyn"+mResponse[1]).innerHTML = "<img src=\""+mResponse[0]+"\" alt=\"Loading Image\" title=\"Loading Image\" style=\"height:133; width:350;\" />";
            d.getElementById("loading"+mResponse[1]+"a").style.display = "none";
        }
    }
}
//***************************************************************
function handleUpdateModelsResponse(){
	if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        //alert(response);
        if(response) {
			if( response == "no_records_found" ){
				d.getElementById("errorx").innerHTML = 'No records found.';
			}else{
				
				var mResponse = response.split("|");
				var header = "<table border='0' width='100%' cellspacing='0' cellpadding='0'><tr><td height='1' bgcolor='#006600' colspan='9'></td></tr>";
				var end = "</table>";
				
				d.getElementById('errorx').innerHTML = "";
				//d.getElementById('dyn_counter').value = "";
				var k = 1;
				
				//[  52-13|51-8|  ]
				for( i=0 ; i<mResponse.length-1 ; i++ ){
					
					var jResponse = mResponse[i].split("-");
					for( j=0 ; j<jResponse.length-1 ; j++ ){
					//alert(mResponse[i]);
					//write the textboxes here..
					d.getElementById('errorx').innerHTML += "<table border='0' width='100%' cellspacing='0' cellpadding='0'><tr><td height='1' bgcolor='#006600' colspan='3'></td></tr><tr bgcolor=''><td width='10'>&nbsp;</td><td width='200' height='25'><input type='text' id='e"+k+"' name='e"+k+"' value='"+jResponse[1]+"' style='height:15px;' /><input type='hidden' id='h"+k+"' name='h"+k+"' value='"+jResponse[0]+"' /></td><td width='120' align='center'><input type='checkbox' id='d"+k+"' name='d"+k+"' value='"+jResponse[0]+"' onclick='CCA(this);' /><br /></td></tr><tr><td height='1' bgcolor='#006600' colspan='3'></td></tr></table>";
					//alert(d.getElementById('errorx').innerHTML);
					k++;
					}
				}
				//alert(k);
				d.getElementById('dyn_counter').value = k-1;
			}
			d.getElementById("add").style.display = "block";
        }
    }
}
//***************************************************************
/*function handleUpdateModelsResponse_a(){
	if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        //alert(response);
		
		//get the browser type
		var uAgent = navigator.userAgent.toLowerCase();
		//document.write (uAgent+"<br /><br />");
		
        if(response) {
			//remove all select items
			d.getElementById("search_model").options.length = 0;
			
			var mResponse = response.split("|");
			//alert(mResponse.length);
			var element = document.getElementById('search_model');
			var newone;
			
			if ( uAgent.indexOf("firefox") != -1 || uAgent.indexOf("opera") != -1 ){

					//create a new option and append it to the select
					newone = new Option( mResponse[i] , mResponse[i] );
					element.add( newone , element.options[element.options.length] );
				
				}else if ( uAgent.indexOf("msie") != -1 ){
				
					//create a new option and append it to the select
					newone = new Option( mResponse[i] , mResponse[i] );
					element.add( newone , i );
					
				}
			}
			
			//add the [other] option
			other = new Option( 'Other' , 'other' );
			if ( uAgent.indexOf("firefox") != -1 || uAgent.indexOf("opera") != -1 ){
				element.add( other , element.options[element.options.length] );
			}else if ( uAgent.indexOf("msie") != -1 ){
				element.add( other , i );
			}
			
		}
	}
}*/
//***************************************************************
function handleUpdateModelsResponse1(){
	if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        //alert(response);
		
		//get the browser type
		var uAgent = navigator.userAgent.toLowerCase();
		//document.write (uAgent+"<br /><br />");
		
        if(response) {
			//remove all select items
			d.getElementById("search_model").options.length = 0;
			
			var mResponse = response.split("|");
			//alert(mResponse.length);
			var element = document.getElementById('search_model');
			var newone;
			
			
			/*kk = 0;
			if( kk == 0 ){
			    new_ = new Option( "" , "" );
				if ( uAgent.indexOf("firefox") != -1 || 
				 uAgent.indexOf("opera") != -1 || 
				 uAgent.indexOf("safari") != -1 
				){
					element.add( new_ , element.options[element.options.length] );
				}else if ( uAgent.indexOf("msie") != -1 ){
					element.add( new_ );
				}
			}*/
				newone = new Option( "" ,"" );
				if ( uAgent.indexOf("firefox") != -1 || 
					 uAgent.indexOf("opera") != -1 || 
					 uAgent.indexOf("safari") != -1 
					){

					//append it to the select
					element.add( newone , element.options[element.options.length] );
				
				}else if ( uAgent.indexOf("msie") != -1 ){
				
					//append it to the select
					element.add( newone , 0 );
					
				}
				
			for( i=0 ; i < mResponse.length-1 ; i++ ){
			//kk++;
				
				var jResponse = mResponse[i].split("-");
				
				//create a new option
				newone = new Option( jResponse[1] , jResponse[1] );
				
				if ( uAgent.indexOf("firefox") != -1 || 
					 uAgent.indexOf("opera") != -1 || 
					 uAgent.indexOf("safari") != -1 
					){

					//append it to the select
					element.add( newone , element.options[element.options.length] );
				
				}else if ( uAgent.indexOf("msie") != -1 ){
				
					//append it to the select
					element.add( newone , i );
					
				}
			}
			
			//add the [other] option
			//create a new option
			other = new Option( 'Other' , 'other' );
			
			if ( uAgent.indexOf("firefox") != -1 || 
					 uAgent.indexOf("opera") != -1 || 
					 uAgent.indexOf("safari") != -1 
				){
				
				//append it to the select
				element.add( other , element.options[element.options.length] );
				
			}else if ( uAgent.indexOf("msie") != -1 ){
				
				//append it to the select
				element.add( other , i );
				
			}
			
		}
	}
}
//***************************************************************
function handleUpdateModelsResponse2(){
	if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        //alert(response);
		
		//get the browser type
		var uAgent = navigator.userAgent.toLowerCase();
		//document.write (uAgent+"<br /><br />");
		
        if(response) {
			//remove all select items
			d.getElementById("sellmodel").options.length = 0;
			
			var mResponse = response.split("|");
			//alert(mResponse.length);
			var element = document.getElementById('sellmodel');
			var newone;
			
			for( i=0 ; i < mResponse.length-1 ; i++ ){
				
				var jResponse = mResponse[i].split("-");
				//create a new option
				newone = new Option( jResponse[1] , jResponse[1] );
				
				if ( uAgent.indexOf("firefox") != -1 || 
					 uAgent.indexOf("opera") != -1 || 
					 uAgent.indexOf("safari") != -1 
					){

					//append it to the select
					element.add( newone , element.options[element.options.length] );
				
				}else if ( uAgent.indexOf("msie") != -1 ){
				
					//append it to the select
					element.add( newone , i );
					
				}
			}
			
			//add the [other] option
			//create a new option
			other = new Option( 'Other' , 'other' );
			
			if ( uAgent.indexOf("firefox") != -1 || 
					 uAgent.indexOf("opera") != -1 || 
					 uAgent.indexOf("safari") != -1 
				){
				
				//append it to the select
				element.add( other , element.options[element.options.length] );
				
			}else if ( uAgent.indexOf("msie") != -1 ){
				
				//append it to the select
				element.add( other , i );
				
			}
			
		}
	}
}
//***************************************************************
//cp > models menu
//checks if the selected value is zero, it doesn't load AJAX functions..
function checkIfZero(){
	var gSelected = d.getElementById('makes').value;
	//alert(gSelected);
	if( gSelected != 0 ){//alert('not 0');
	//alert(d.getElementById('makes').value);
	//alert(d.getElementById('makes').value);
	    //alert("sendReq('update_models',"+d.getElementById('makes').value+")");
	    sendReq('update_models',d.getElementById('makes').value);
	}else{//alert('0');
	    d.getElementById("add").style.display = "none";
		d.getElementById("errorx").innerHTML = 'Select a car make first.';
	}
}
//***************************************************************
// home
//checks if the selected value is zero, it doesn't load AJAX functions..
function checkIfZero_a(){
	var gSelected = d.getElementById('search_make').value;
	//alert(gSelected);
	openOther('search_make','search_make_otherbody');
	if( gSelected == 0 ){
		d.getElementById('search_model_otherbody').style.display = 'none';
		//alert('0');
		//do nothing
	}else if( gSelected == "other" ){
		d.getElementById('search_model').innerHTML = '';
		d.getElementById('search_model').options[0] = new Option("Other", "other");
		d.getElementById('search_model').value = 'other';
		d.getElementById('search_model_otherbody').style.display = 'inline';
		//alert(d.getElementById('search_make').value);
	}else{
		d.getElementById('search_model_otherbody').style.display = 'none';
	    //alert('not 0');
	    sendReq('update_models1',d.getElementById('search_make').value);
	}
}
//***************************************************************
function checkIfZero1(){
	var gSelected = d.getElementById('search_make').value;
	//alert(gSelected);
	openOther('search_make','search_make_otherbody');
	if( gSelected == 0 ){
		d.getElementById('search_model_otherbody').style.display = 'none';
		//alert('0');
		//do nothing
	}else if( gSelected == "other" ){
		var element = document.getElementById('search_model');
		//get the browser type
		var uAgent = navigator.userAgent.toLowerCase();
		
		//remove all select items
		d.getElementById("search_model").options.length = 0;
		
		//add the [other] option
		//create a new option
		other = new Option( 'Other' , 'other' );
		//append it to the select
		if ( uAgent.indexOf("firefox") != -1 || 
			 uAgent.indexOf("opera") != -1 || 
			 uAgent.indexOf("safari") != -1 
			){
			
			element.add( other , element.options[element.options.length] );
		
		}else if ( uAgent.indexOf("msie") != -1 ){
			
			element.add( other , 0 );
			
		}
		
		d.getElementById('search_model_otherbody').style.display = 'inline';
		
	}else{
		
		d.getElementById('search_model_otherbody').style.display = 'none';
	    //alert('not 0');
	    sendReq('update_models1',d.getElementById('search_make').value);
		
	}
}
//***************************************************************
// home
//checks if the selected value is zero, it doesn't load AJAX functions..
function checkIfZero2(){
	var gSelected = d.getElementById('sellmake').value;
	//alert(gSelected);
	openOther('sellmake','sell_make_otherbody');
	
	if( gSelected == 0 ){
		d.getElementById('sell_model_otherbody').style.display = 'none';
		//alert('0');
		//do nothing
	}else if( gSelected == "other" ){
		
		var element = document.getElementById('sellmodel');
		//get the browser type
		var uAgent = navigator.userAgent.toLowerCase();
		
		//remove all select items
		d.getElementById("sellmodel").options.length = 0;
		
		//add the [other] option
		//create a new option
		other = new Option( 'Other' , 'other' );
		//append it to the select
		if ( uAgent.indexOf("firefox") != -1 || 
			 uAgent.indexOf("opera") != -1 || 
			 uAgent.indexOf("safari") != -1 
			){
			
			element.add( other , element.options[element.options.length] );
		
		}else if ( uAgent.indexOf("msie") != -1 ){
			
			element.add( other , 0 );
			
		}
		
		d.getElementById('sell_model_otherbody').style.display = 'inline';
		
	}else{
	    //alert('not 0');
		d.getElementById('sell_model_otherbody').style.display = 'none';
	    sendReq('update_models2',d.getElementById('sellmake').value);
	}
}
//***************************************************************
//document hash adjust - [all cars view]
function mHash(){
    setInterval("updateDocFromHash()" , 100); //1000 for 1 sec
}

function updateDocFromHash(){
    var docForms = d.forms;
    var docHash = location.hash; //get the hash
    docHash = docHash.replace('#',''); //remove #
	
    if(location.hash != ''){
        //hide all [buy now from] blocks from the document
        for( i=0 ; i < docForms.length ; i++ ){
            docForms[i].style.display = 'none';
        }
	
        //show specified [buy now from] obtained from the hash
        d.getElementById('car'+docHash+'a').style.display = 'block';
    }
}
//***************************************************************
function removeHash(){
    //Bug fix: when working with hash values, we need to empty its value
    //to stop setInterval function form executing and NOT open the specified table
//    location.hash = '#';
}
//***************************************************************
//not used
//cp > edit car types
//check if the input is a number
function isANumber( number ) {
    answer = true;
    if (!parseFloat(number)) {
        //the first digit wasn't numeric
        answer = false;
        //alert("Please enter a numeric value.");
    }
    else {
        //the first digit was numeric, so check the rest
        for (var i=0; i<number.length; i++) {
            if ((number.charAt(i) != "0") && (!parseFloat(number.charAt(i)))) {
                answer = false;
                //alert("Please enter a numeric value.");
                break;
            }
        }
    }
    return answer;
}
//***************************************************************
//cp > edit car types
//show/hide text fields
function addTextFields( num ){
    num = d.getElementById(num).selectedIndex;
    for( i=1 ; i <= 10 ; i++ ){
        if( i <= num )
            d.getElementById('t'+i).style.display = 'block';
        else
            d.getElementById('t'+i).style.display = 'none';
    }
}
//***************************************************************
//cp > add car types validation
function checkAdd(){
    selected = d.getElementById('numx').selectedIndex;
    error = false;
    //alert(selected);
    if( selected == 0 ) alert('Please select a value from the drop-down menu');
    else{
        //loop to check for empty fields
        for( i=1 ; i <= selected ; i++ ){
            if( d.getElementById('t'+i).value == '' ){
                alert('Some values are missing.');
                error = true;
                break;
            }
        }
        //if no errors [error=false], go here
        if(!error){
            //assign a value to [what]
            d.getElementById('what').value = 'add_type';
			//alert(d.getElementById('what').value);
            d.forms[0].submit();
        }
    }
}
//***************************************************************
function checkEdit(){
    counterValue = d.getElementById('counter').value;
	//alert(counterValue);
    error = false;
	
	//if( counterValue == 0 ) alert('You have no records to update!');
	//else{
        //loop to check for empty fields
        for( i=1 ; i <= counterValue ; i++ ){
            if( d.getElementById('e'+i).value == '' ){
                alert('Some values are missing.');
                error = true;
                break;
            }
        }
		
        //if no errors [error=false], go here
        if(!error){
            //assign a value to [what]
            d.getElementById('what').value = 'edit_type';
            d.forms[0].submit();
        }
	//}
}
//***************************************************************
function checkEdit1(){
    counterValue = d.getElementById('dyn_counter').value;
	//alert(counterValue);
    error = false;
	
	if( counterValue == 0 || counterValue == "" ){
		alert('You have no records to update!');
	}else{
        //loop to check for empty fields
        for( i=1 ; i <= counterValue ; i++ ){
            if( d.getElementById('e'+i).value == '' ){
                alert('Some values are missing.');
		        d.getElementById('dyn_counter').value = 0;
                error = true;
                break;
            }
        }
		
        //if no errors [error=false], go here
        if(!error){
            //assign a value to [what]
            d.getElementById('what').value = 'edit_type';
            d.forms[0].submit();
        }
	}
}
//***************************************************************
function checkDelete(){
    counterValue = d.getElementById('counter').value;
    j=0;
    //loop to check if there is checkboxes checked
    for( i=1 ; i <= counterValue ; i++ ){
        if( d.getElementById('d'+i).checked == false ){
            j++;
        }
    }
    //if no checkboxes checked
    if( counterValue == j )
        alert('You have to check at least 1 checkbox.');
    else{
        //assign a value to [what]
        d.getElementById('what').value = 'del_type';
        d.forms[0].submit();
    }
}
//***************************************************************
function checkDelete1(){
    counterValue = d.getElementById('dyn_counter').value;
    j=0;
    //loop to check if there is checkboxes checked
    for( i=1 ; i <= counterValue ; i++ ){
        if( d.getElementById('d'+i).checked == false ){
            j++;
        }
    }
    //if no checkboxes checked
    if( counterValue == j )
        alert('You have to check at least 1 checkbox.');
    else{
        //assign a value to [what]
        d.getElementById('what').value = 'del_type';
        d.forms[0].submit();
    }
}
//***************************************************************
//***************************************************************